public void leave_OUT(Client_information p) { if (p.socket != null) { p.socket.Close(); } p.socket = null; }
public void SendCommand(Client_information user, byte[] txt) //发送函数 txt为发送内容 { if (user.Sock != null && user.Sock.Connected) { SocketAsyncEventArgs sendArg = new SocketAsyncEventArgs(); sendArg.SetBuffer(txt, 0, txt.Length); sendArg.Completed += (objSender, mArg) => { if (mArg.SocketError == SocketError.Success) { // this.BeginInvoke(new Action<string>(this.Show_Message2), "发送成功"); } else { // this.BeginInvoke(new Action<string>(this.Show_Message2), "发送失败,错误:" + mArg.SocketError.ToString()); } }; user.Sock.SendAsync(sendArg); } }
public void ReceiveCallback(IAsyncResult ia) //接收回调 msg为接收数据 { StateObject _so = ia.AsyncState as StateObject; _socket = _so.theSocket; try { //接收到的字节数 int n = _socket.EndReceive(ia); string msg = Encoding.UTF8.GetString(_so.Buffer, 0, n); string[] tokens = msg.Split(new Char[] { '|' }); IPEndPoint m = (IPEndPoint)_socket.RemoteEndPoint; if (tokens[0] == "CONN") { Client_information New_User = new Client_information(tokens[1], m.Address.ToString(), _socket); information New_User_INFOR = new information(tokens[1], m.Address.ToString()); New_User_information(tokens[1], m.Address.ToString()); thread = new Thread(new ParameterizedThreadStart(setItems)); thread.Start(); MemoryStream User_Stream = new MemoryStream(); newUser_list.Clear(); newUser_list.Add(New_User_INFOR); foreach (Client_information p in User) { // Client_information cl = (Client_information)User[i]; string js = "Client|" + JsonConvert.SerializeObject(newUser_list); byte[] buffer = System.Text.Encoding.UTF8.GetBytes(js); SendCommand(p, buffer); } /*for (int i = 0;i<User.Count; i++) * { * Client_information cl = (Client_information)User[i]; * string js = JsonConvert.SerializeObject(newUser_list); * byte[] buffer = System.Text.Encoding.UTF8.GetBytes(js); * SendCommand(cl, buffer); * * }*/ string json = "Client|" + JsonConvert.SerializeObject(User_information); byte[] bf = System.Text.Encoding.UTF8.GetBytes(json); SendCommand(New_User, bf); // MessageBox.Show("ok"); User.Add(New_User); User_information.Add(New_User_INFOR); // MessageBox.Show(User_information[0].ip+"OK"); //MessageBox.Show(User[0].getName()); } if (tokens[0] == "Signout") { string js = null; byte[] buffer = null; for (int i = 0; i < User_information.Count; i++) { if (User_information[i].ip.Equals(tokens[1])) { js = "Quit|" + JsonConvert.SerializeObject(User_information[i]); buffer = System.Text.Encoding.UTF8.GetBytes(js); ii = i; New_Leave(User_information[i].name, User_information[i].ip); th = new Thread(new ParameterizedThreadStart(longshao)); th.Start(); leave_OUT(User[i]); User_information.RemoveAt(i); User.RemoveAt(i); } } for (int i = 0; i < User.Count; i++) { buffer = System.Text.Encoding.UTF8.GetBytes(js); SendCommand(User[i], buffer); } } } catch (Exception e) { //MessageBox.Show(e.Message); } //继续接收 _so = new StateObject(); _so.theSocket = _socket; try { _socket.BeginReceive(_so.Buffer, 0, _so.Buffer.Length, SocketFlags.None, new AsyncCallback(this.ReceiveCallback), _so); } catch (Exception e) { } }