public void SendLoginMsg(ClientMsgModel msg, User user) { //IPAddress ip = IPAddress.Parse(user.client.Client.RemoteEndPoint.ToString()); OnlineUser __onlineUser = new OnlineUser(); __onlineUser.IP = (user.client.Client.RemoteEndPoint as IPEndPoint).Address.ToString(); __onlineUser.Port = (user.client.Client.RemoteEndPoint as IPEndPoint).Port.ToString(); __onlineUser.UserName = msg.UserName; if (!this.onlineUser.Contains(__onlineUser)) { this.onlineUser.Add(__onlineUser); } SetListBox(string.Format("登录IP:{0},端口:{1}", msg.IP, msg.Port)); ServerMsgModel __serMsgModel = new ServerMsgModel(); __serMsgModel.SendIP = msg.IP; __serMsgModel.SendPort = msg.Port; __serMsgModel.SendUserName = msg.UserName; __serMsgModel.SendType = msg.Type; __serMsgModel.OnlineUser = this.onlineUser; string __sendSerMsg = JsonConvert.SerializeObject(__serMsgModel); for (int i = 0; i < this.userList.Count; i++) { SendToClient(userList[i], __sendSerMsg); } }
private void ReceiveData() { while (this.isExit == false) { string receiveString = null; try { //从网络流中读出字符串 //此方法会自动判断字符串长度前缀,并根据长度前缀读出字符串 receiveString = br.ReadString(); } catch { //底层套接字不存在时会出现异常 //提示数据接收失败 //TcpInfo.AppendText("接收数据失败"); this.AppendTcpInfo("接收数据失败"); } if (receiveString == null) { if (isExit == false) { MessageBox.Show("与服务器失去联系!"); } break; } uw = true; getMess = receiveString; //提示接收到的数据 //TcpInfo.AppendText(Environment.NewLine + "接收服务器数据:" + Environment.NewLine + receiveString); ServerMsgModel __serverMsg = (ServerMsgModel)JsonConvert.DeserializeObject(receiveString, typeof(ServerMsgModel)); switch (__serverMsg.SendType) { case "1": this._onlineUser = __serverMsg.OnlineUser; for (int i = 0; i < __serverMsg.OnlineUser.Count; i++) { this.AddListBoxItem(__serverMsg.OnlineUser[i].UserName); } break; case "2": this.AppendChatMsgText(__serverMsg.SendMsg); break; default: break; } } }
/// <summary> /// 接收、处理客户端信息,每客户1个线程,参数用于区分是哪个客户 /// </summary> /// <param name="obj"></param> public void ReceiveData(object obj) { User user = obj as User; TcpClient client = user.client; //是否正常退出接收线程 bool normalExit = false; //用于控制是否退出循环 bool exitWhile = false; while (exitWhile == false) { string receiveString = null; try { //从网络流中读出字符串 //此方法会自动判断字符串长度前缀,并根据长度前缀读出字符串 receiveString = user.br.ReadString(); } catch (Exception) { //底层套接字不存在时会出现异常 SetListBox("接收数据失败"); } if (receiveString == null) { if (normalExit == false) { //如果停止了监听,Connected为false if (client.Connected == true) { SetListBox(string.Format("与[{0}]失去联系,已终止接收该用户信息", client.Client.RemoteEndPoint)); } } break; } SetListBox(string.Format("来自[{0}]:{1}", user.client.Client.RemoteEndPoint, receiveString)); ClientMsgModel msg = new ClientMsgModel(); msg = (ClientMsgModel)JsonConvert.DeserializeObject(receiveString, typeof(ClientMsgModel)); switch (msg.Type) { case "1": this.SendLoginMsg(msg, user); break; case "2": User userReceive = this.userList.Where(delegate(User u) { return(u.client.Client.RemoteEndPoint.ToString() == msg.ReceiveIP + ":" + msg.ReceivePort); }).FirstOrDefault(); ServerMsgModel __serMsgModel = new ServerMsgModel(); __serMsgModel.SendIP = (user.client.Client.RemoteEndPoint as IPEndPoint).Address.ToString(); __serMsgModel.SendPort = (user.client.Client.RemoteEndPoint as IPEndPoint).Port.ToString(); __serMsgModel.SendUserName = msg.UserName; __serMsgModel.SendType = msg.Type; __serMsgModel.SendMsg = msg.Msg; string __sendSerMsg = JsonConvert.SerializeObject(__serMsgModel); SendToClient(userReceive, __sendSerMsg); break; default: break; } } userList.Remove(user); client.Close(); SetListBox(string.Format("当前连接用户数:{0}", userList.Count)); }