/// <summary> /// Listen to the message sent by the server监听服务端发来的消息 /// </summary> void ReceiveMsg()//0 is an ordinary message, 1 is a user list 0是普通消息,1是用户列表 { while (true) { //Define a receive byte array buffer (2M size) //定义一个接收消息用的字节数组缓冲区(2M大小) byte[] arrMsgRev = new byte[1024 * 1024 * 2]; //Save the received data to arrMsgRev and return the length of the data actually received //将接收到的数据存入arrMsgRev,并返回真正接收到数据的长度 int length = -1; try { length = socketClient.Receive(arrMsgRev); } catch (SocketException ex) { ShowMsg("SocketException:" + ex.Message); break; } catch (Exception ex) { ShowMsg("Exception:" + ex.Message); break; } if (arrMsgRev[0] == 0) //Judge the client sent the first element of the data is 0, on behalf of the text 判断客户端发送过来数据的第一位元素是0,代表文字 { //At this point all the elements of the array (each byte) are converted to a string, and really received only the server sent a few characters //此时是将数组的所有元素(每个字节)都转成字符串,而真正接收到只有服务端发来的几个字符 //string strMsgReceive = Encoding.UTF8.GetString(arrMsgRev, 1, length - 1); byte[] strMsgRev = new byte[arrMsgRev.Length - 1]; Buffer.BlockCopy(arrMsgRev, 1, strMsgRev, 0, arrMsgRev.Length - 1); StrMessage strMsgReceive = (StrMessage)SerObject.DeserializeObject(strMsgRev); ShowMsg(string.Format("#{0}# {1} speak to me {2}", strMsgReceive.ToTopic, strMsgReceive.ToName, strMsgReceive.ToMessage)); } else if (arrMsgRev[0] == 1)//1 is the online user list 1是在线用户列表 { byte[] strMsgRev = new byte[arrMsgRev.Length - 1]; Buffer.BlockCopy(arrMsgRev, 1, strMsgRev, 0, arrMsgRev.Length - 1); List <string> listname = (List <String>)SerObject.DeserializeObject(strMsgRev); listMember.Items.Clear(); //Empty the text 清空文本 foreach (string name in listname) { listMember.Items.Add(name); } } } }
/// Server-side monitoring client-side sends data 服务端监听客户端发来的数据 /// </summary> void ReceiveMsg(object socketClientPara) //0 is normal message, 1 is the user's nickname 0普通消息,1是用户昵称 { Socket socketClient = socketClientPara as Socket; while (true) { //Define a receive byte array buffer (2M size) //定义一个接收消息用的字节数组缓冲区(2M大小) byte[] arrMsgRev = new byte[1024 * 1024 * 2]; //Save the received data to arrMsgRev and return the length of the data actually received //将接收到的数据存入arrMsgRev,并返回真正接收到数据的长度 int length = -1; try { length = socketClient.Receive(arrMsgRev); } catch (SocketException ex) { ShowMsg("Exception:" + ex.Message + "RemoteEndPoint: " + socketClient.RemoteEndPoint.ToString()); clientlist.Remove(socketClient.RemoteEndPoint.ToString()); sendNewClientList(); //Removes the communication socket that was disconnected from the communication socket combination //从通信套接字结合中删除被中断连接的通信套接字 dict.Remove(socketClient.RemoteEndPoint.ToString()); //Removes the disconnected communication thread object from the set of communication threads //从通信线程集合中删除被中断连接的通信线程对象 dictThread.Remove(socketClient.RemoteEndPoint.ToString()); //Remove the disconnected IP: Port from the display list //从显示列表中移除被中断连接的IP:Port listMember.Items.Remove(socketClient.RemoteEndPoint.ToString()); break; } catch (Exception ex) { ShowMsg("Exception:" + ex.Message); break; } if (arrMsgRev[0] == 0) //Judge the client sent the first element of the data is 0, on behalf of the text 判断客户端发送过来数据的第一位元素是0,代表文字 { //At this point all the elements of the array (each byte) are converted to a string, and really received only the server sent a few characters //此时是将数组的所有元素(每个字节)都转成字符串,而真正接收到只有服务端发来的几个字符 //string strMsgReceive = Encoding.UTF8.GetString(arrMsgRev, 1, length - 1); byte[] strMsgRev = new byte[arrMsgRev.Length - 1]; Buffer.BlockCopy(arrMsgRev, 1, strMsgRev, 0, arrMsgRev.Length - 1); StrMessage strMsgReceive = (StrMessage)SerObject.DeserializeObject(strMsgRev); if (strMsgReceive.ToName == "The server end") { ShowMsg(string.Format("#{0}# {1} speak to me :{2}", strMsgReceive.ToTopic, clientlist[socketClient.RemoteEndPoint.ToString()], strMsgReceive.ToMessage)); } else { StrMessage strMsg = new StrMessage(); strMsg.ToMessage = strMsgReceive.ToMessage; strMsg.ToTopic = strMsgReceive.ToTopic; strMsg.ToName = clientlist[socketClient.RemoteEndPoint.ToString()]; //set the string which to be sent into UTF-8 corresponding byte array //将要发送的字符串转成UTF-8对应的字节数组 byte[] arrMsg = SerObject.SerializeObject(strMsg); byte[] arrMsgSend = new byte[arrMsg.Length + 1]; arrMsgSend[0] = 0;//Set the flag, 0 on behalf of the text sent 设置标识位,0代表发送的是文字 Buffer.BlockCopy(arrMsg, 0, arrMsgSend, 1, arrMsg.Length); //Get the list of selected remote IP Key //获得列表中选中的远程IP的Key String strCli = strMsgReceive.ToName; string strClientKey = clientlist.toK(strCli); try { //Through the key to find the dictionary set corresponding to a client communication socket Send method to send data to each other //通过key找到字典集合中对应的某个客户端通信的套接字,用Send方法发送数据给对方 Socket s = dict[strClientKey]; s.Send(arrMsgSend); ShowMsg(string.Format("#{0}# {1} speak to {2} :{3}", strMsgReceive.ToTopic, strMsgReceive.ToName, clientlist[socketClient.RemoteEndPoint.ToString()], strMsgReceive.ToMessage)); } catch (SocketException ex) { ShowMsg("SocketException:" + ex.Message); } catch (Exception ex) { ShowMsg("Exception:" + ex.Message); } } } else if (arrMsgRev[0] == 1) //1 for the nickname 1代表昵称 { String nickname = Encoding.UTF8.GetString(arrMsgRev, 1, length - 1); clientlist[socketClient.RemoteEndPoint.ToString()] = nickname; listMember.Items.Clear(); //Empty the text 清空文本 for (int i = 0; i < clientlist.Size(); i++) { listMember.Items.Add(clientlist[i]); } sendNewClientList(); } } }