看了在线聊天室的视频,我想实现客户端向指定客户端发送消息,但是在推送在线列表的时候遇到了问题,大家帮忙看看。
打开第一个客户端,第二个客户端都没有问题,
但是到第三个客户端就会出现这样的问题
有时第三个客户端出现 ,有时第四个时出现,有时连续7,8个客户端都不会出现此问题,真是见鬼了
这是服务端代码- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Net.Sockets;
- using System.Net;
- using System.Threading;
- using System.IO;
- namespace Server
- {
- public partial class FormSrv : Form
- {
- public FormSrv()
- {
- InitializeComponent();
- TextBox.CheckForIllegalCrossThreadCalls = false;
- }
- Socket watchSocket = null;
- Socket connSocket = null;
- Dictionary<string, Socket> connDictionary = new Dictionary<string, Socket>();
- Dictionary<string, Thread> recThreadDic = new Dictionary<string, Thread>();
- private void buttonSrvStart_Click(object sender, EventArgs e)
- {
- watchSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);;
- IPAddress IP = IPAddress.Parse(textBoxIP.Text.Trim());
- IPEndPoint port = new IPEndPoint(IP, int.Parse(textBoxPort.Text.Trim()));
- watchSocket.Bind(port);
- watchSocket.Listen(10);
- Thread watchThread = new Thread(StartSrv);
- watchThread.Start();
- showMsg("服务成功启动,等待客户端连接………………\n");
- }
- private void showMsg(string Msg)
- {
- textBoxMsg.AppendText(Msg);
- }
- private void StartSrv()
- {
- connSocket = watchSocket.Accept();
- connDictionary.Add(connSocket.RemoteEndPoint.ToString(), connSocket);
- listBoxClient.Items.Add(connSocket.RemoteEndPoint);
- showMsg("连接成功!\n");
- Thread threadRecieveMsg = new Thread(RecieveMsg);
- threadRecieveMsg.Start(connSocket);
- recThreadDic.Add(connSocket.RemoteEndPoint.ToString().Trim(), threadRecieveMsg);
-
- //推送在线列表
- foreach (Socket s in connDictionary.Values)
- {
- foreach (string item in connDictionary.Keys)
- {
- if (item != s.RemoteEndPoint.ToString())
- {
- byte[] byteListBox = Encoding.Default.GetBytes(item);
- byte[] newByteListBox = new byte[byteListBox.Length + 1];
- newByteListBox[0] = 2;
- Buffer.BlockCopy(byteListBox, 0, newByteListBox, 1, byteListBox.Length);
- s.Send(newByteListBox);
- }
- }
- }
- //递归
- StartSrv();
-
- }
- private void buttonSend_Click(object sender, EventArgs e)
- {
- string strMsg = textBoxSendMsg.Text.Trim();
- byte[] bytearr = System.Text.Encoding.Default.GetBytes(strMsg);
- connDictionary[listBoxClient.SelectedItem.ToString()].Send(bytearr);
- showMsg("发送了消息:" + strMsg + "\r\n");
- }
- private void RecieveMsg(object socket)
- {
- while (true)
- {
- Socket socket1 = socket as Socket;
- byte[] byteMsg = new byte[1024 * 1024 * 2];
- int RecieveLength = socket1.Receive(byteMsg);
- if (byteMsg[0] == 0)
- {
- string strMsg = Encoding.Default.GetString(byteMsg, 0, RecieveLength);
- showMsg(strMsg + "\r\n");
- }
- else if(byteMsg[0]==1)
- {
- SaveFileDialog sfd = new SaveFileDialog();
- if (sfd.ShowDialog() == DialogResult.OK)
- {
- using (FileStream fs = new FileStream(sfd.FileName, FileMode.Create))
- {
- fs.Write(byteMsg, 1, RecieveLength - 1);
- showMsg("保存成功:" + sfd.FileName);
- }
- }
- }
- }
-
- }
- private void textBoxSendMsg_TextChanged(object sender, EventArgs e)
- {
- }
- private void buttonSends_Click(object sender, EventArgs e)
- {
- string strMsg = textBoxSendMsg.Text.Trim();
- byte[] byteMsg = Encoding.Default.GetBytes(strMsg);
- foreach (Socket s in connDictionary.Values)
- {
- s.Send(byteMsg);
- showMsg("群发消息:" + strMsg + "\r\n");
- }
- }
-
- }
- }
复制代码 这是客户端- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Net.Sockets;
- using System.Net;
- using System.Threading;
- using System.IO;
- namespace Client
- {
- public partial class FormClient : Form
- {
- public FormClient()
- {
- InitializeComponent();
- TextBox.CheckForIllegalCrossThreadCalls = false;
- }
- Socket clientSocket = null;
- IPAddress ip = null;
- IPEndPoint port = null;
- private void buttonConn_Click(object sender, EventArgs e)
- {
- clientSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
- ip = IPAddress.Parse(textBoxIP.Text.Trim());
- port = new IPEndPoint(ip,int.Parse(textBoxPort.Text.Trim()));
- clientSocket.Connect(port);
- showMsg("与服务器连接成功"+"\r\n");
- Thread receiveThread = new Thread(ReceiveMsg);
- receiveThread.Start();
- }
- private void showMsg(string msg)
- {
- textBoxMsg.AppendText(msg);
- }
- private void ReceiveMsg()
- {
- while (true)
- {
- byte[] arrMsg = new byte[1024 * 1024 * 2];
- int msgLength = clientSocket.Receive(arrMsg);
- if (arrMsg[0] == 2)//如果传过来第一个字符是2就是在线列表
- {
- string item = Encoding.Default.GetString(arrMsg, 1, msgLength - 1);
- listBoxOnline.Items.Add(item);
- }
- else
- {
- string strMsg = Encoding.Default.GetString(arrMsg, 0, msgLength);
- textBoxMsg.AppendText(strMsg + "\r\n");
- }
- }
- }
- private void buttonSend_Click(object sender, EventArgs e)
- {
- string strMsg = textBoxSendMsg.Text.Trim();
- byte[] byteMsg = Encoding.Default.GetBytes(strMsg);
- clientSocket.Send(byteMsg);
- showMsg("发送消息:" + strMsg + "\r\n");
- }
- private void buttonFilePath_Click(object sender, EventArgs e)
- {
- OpenFileDialog ofd = new OpenFileDialog();
- if (ofd.ShowDialog() != DialogResult.OK)
- {
- return;
- }
- textBoxFilePath.Text = ofd.FileName;
-
- }
-
- private void buttonFileSend_Click(object sender, EventArgs e)
- {
- using (FileStream fs = new FileStream(textBoxFilePath.Text, FileMode.Open))
- {
- byte[] byteMsg = new byte[1024 * 1024 * 2];
- byteMsg[0] = 1;
- int fileLength = fs.Read(byteMsg, 1, byteMsg.Length - 1);
- clientSocket.Send(byteMsg);
- showMsg("发送成功");
- }
- }
-
- }
- }
复制代码 |