/// <summary> /// 监听客户端发来的请求 /// </summary> private void WatchConnecting() { //持续不断监听客户端发来的请求 while (true) { try { Socket clientSocket = socketWatch.Accept(); byte[] recMsg = new byte[con_msg_length]; if (clientSocket != null && clientSocket.Connected) { int length = clientSocket.Receive(recMsg); string msg = ""; if (length > 0) { //将机器接受到的字节数组转换为人可以读懂的字符串 msg = Encoding.UTF8.GetString(recMsg, 0, length); } AppendText("==[" + msg + "]==" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n"); SocketMsg smsg = new SocketMsg(); try { smsg.setAllParametersJsonStr(msg); } catch { } Hashtable param = smsg.getAllParameters(); ClientUser cu = new ClientUser(); cu.ClientSocket = clientSocket; IPEndPoint ip = (IPEndPoint)clientSocket.RemoteEndPoint; cu.CliIp = ip.Address.ToString(); cu.CliPort = ip.Port.ToString(); cu.LoginTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); AppendText("客户端连接!" + cu.CliIp + ":" + cu.CliPort + "\r\n"); string userno = ""; if (param != null && param.Count > 0) { userno = param["UserNo"].ToString(); cu.UserNo = userno; cu.UserName = param["UserName"].ToString(); } if (!clientList.ContainsKey(userno)) { clientList.Add(userno, cu); if (ClientAdd != null) { ClientAdd(cu, clientList); } } else { if (ClientRemove != null) { ClientRemove(clientList[userno], clientList); } clientList.Remove(userno); clientList.Add(userno, cu); if (ClientAdd != null) { ClientAdd(cu, clientList); } } AppendText("客户端连接成功!" + msg + "\r\n"); // 创建一个通信线程 ParameterizedThreadStart pts = new ParameterizedThreadStart(acceptMsg); Thread thr = new Thread(pts); thr.IsBackground = true; thr.Start(clientSocket); } } catch (Exception ex) { WriteTxtLog(ex.Message); } } }
/// <summary> /// 接收客户端发来的消息 /// </summary> /// <param name="socket">客户端套接字对象</param> private void acceptMsg(object socket) { Socket socketServer = socket as Socket; while (true) { try { //创建一个内存缓冲区 其大小为1024*1024字节 即1M byte[] recMsg = new byte[con_msg_length]; //将接收到的信息存入到内存缓冲区,并返回其字节数组的长度 int length = socketServer.Receive(recMsg); //将机器接受到的字节数组转换为人可以读懂的字符串 if (length > 0) { try { string msg = Encoding.UTF8.GetString(recMsg, 0, length); AppendText("==[" + msg + "]==" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n"); SocketMsg smsg = new SocketMsg(); try { smsg.setAllParametersJsonStr(msg); } catch { } Hashtable param = smsg.getAllParameters(); if (param != null && param.Count > 0) { AppendText("客户端(" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "):" + smsg.ConvertJson2Str(param["msgContent"]) + "\r\n"); string usernos = param["UserNo"] == null ? "" : param["UserNo"].ToString(); string[] nos = usernos.Split(','); if (nos != null && nos.Length > 0) { //转发消息到目标客户端 foreach (string no in nos) { if (clientList.ContainsKey(no) && clientList[no].ClientSocket != socketServer) { serverSendMsg(clientList[no].ClientSocket, smsg.ConvertJson2Str(param["msgContent"])); } } } } } catch (Exception exx) { AppendText("" + exx.Message + "(" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "):\r\n"); } } else { string clin = ""; foreach (string key in clientList.Keys) { if (clientList[key].ClientSocket == socketServer) { clin = "userno:" + clientList[key].UserNo + " name:" + clientList[key].UserName + " ip:" + clientList[key].CliIp; if (ClientRemove != null) { ClientRemove(clientList[key], clientList); } clientList.Remove(key); break; } } AppendText("客户端[" + clin + "]断开(" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "):\r\n"); socketServer.Disconnect(true); socketServer.Close(); break; } } catch (Exception ex) { string clin = ""; foreach (string key in clientList.Keys) { if (clientList[key].ClientSocket == socketServer) { clin = "userno:" + clientList[key].UserNo + " name:" + clientList[key].UserName + " ip:" + clientList[key].CliIp; if (ClientRemove != null) { ClientRemove(clientList[key], clientList); } clientList.Remove(key); break; } } AppendText("客户端[" + clin + "]断开(" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "):\r\n"); socketServer.Disconnect(true); socketServer.Close(); break; } } }