///// <summary> ///// 接收服务端发来的信息 ///// </summary> //public void ReceiveMsg() //{ // byte[] partialBuffer = null; // while (true) // { // try // { // byte[] result = new byte[2048]; // ////通过clientSocket接收数据 // //int receiveNumber = connection.Receive(result); // ////把接受的数据从字节类型转化为字符类型 // //string recStr = Encoding.Unicode.GetString(result, 0, receiveNumber); // int receiveNumber = clientSocket.Receive(result); // byte[] buffer = new byte[receiveNumber]; // Buffer.BlockCopy(result, 0, buffer, 0, receiveNumber); // if (partialBuffer != null) // { // receiveNumber += partialBuffer.Length; // buffer = partialBuffer.Concat(buffer).ToArray(); // } // ProtocolHelper.MMO_MemoryStream ms = new ProtocolHelper.MMO_MemoryStream(buffer); // int length = ms.ReadUShort(); // ms.Close(); // if (length < buffer.Length) // { // partialBuffer = buffer; // continue; // } // partialBuffer = null; // byte[] unpackedMsg = ProtocolHelper.UnpackData(buffer); // string recStr = Encoding.Unicode.GetString(unpackedMsg, 0, unpackedMsg.Length); // //获取当前客户端的ip地址 // IPAddress clientIP = (clientSocket.RemoteEndPoint as IPEndPoint).Address; // //获取客户端端口 // int clientPort = (clientSocket.RemoteEndPoint as IPEndPoint).Port; // string sendStr = clientIP + ":" + clientPort.ToString() + "--->" + recStr; // //显示内容 // txtboxInfo.Dispatcher.BeginInvoke( // new Action(() => { txtboxInfo.Text = $"【接收信息】 {sendStr}\r\n" + txtboxInfo.Text; }), null); // } // catch (Exception ex) // { // // 出现异常,关闭连接 // clientSocket.Shutdown(SocketShutdown.Both); // clientSocket.Close(); // txtboxInfo.Dispatcher.BeginInvoke( // new Action(() => { txtboxInfo.Text = "\r\n" + $"【信息接收异常】 {ex.Message}\r\n" + txtboxInfo.Text; }), null); // break; // } // } //} /// <summary> /// 连接服务端 /// </summary> private void BtnConnect_Click(object sender, RoutedEventArgs e) { try { // 如果当前已经存在Socket,则先关闭当前Socket if (ClientSocket != null) { ClientSocket.Close(); } ClientSocket = new TSocketClient(txtboxIP.Text, Convert.ToInt32(txtboxPort.Text)); txtboxInfo.Text = $"【连接成功】 我方端口 {ClientSocket._Socket.LocalEndPoint.ToString()}\r\n" + txtboxInfo.Text; //Thread th = new Thread(ClientSocket.RecvMsg) //{ // IsBackground = true //}; //th.Start(); } catch (Exception ex) { MessageBox.Show("【连接失败】 " + ex.Message); return; } }
public void ReceiveMsg(object clientSocket) { TSocketClient connection = (TSocketClient)clientSocket; while (true) { try { if (!this.IsDispose && connection._Socket.Connected) { ReceiveSize = connection._Socket.Receive(this.Buffers); if (ReceiveSize > 0) { byte[] rbuff = new byte[ReceiveSize]; Array.Copy(this.Buffers, rbuff, ReceiveSize); var msgs = PHelper.UnpackData(rbuff, ReceiveSize); foreach (var msg in msgs) { connection.Receive(msg); } } } } catch { connection.Close(); break; } } }
/// <summary> /// 连接服务端 /// </summary> private void BtnConnect_Click(object sender, RoutedEventArgs e) { try { string ip = txtboxIP.Text; int port = Convert.ToInt32(txtboxPort.Text); // 如果当前已经存在Socket,则先关闭当前Socket if (clientSocket != null) { clientSocket.Close(); } clientSocket = new TSocketClient(ip, port); //clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //clientSocket.Connect(new IPEndPoint(ip, port)); //txtboxInfo.Text = $"【连接成功】 我方端口 {clientSocket.LocalEndPoint.ToString()}\r\n" + txtboxInfo.Text; //Thread th = new Thread(ReceiveMsg) //{ // IsBackground = true //}; //th.Start(); } catch (Exception ex) { MessageBox.Show("【连接失败】 " + ex.Message); return; } }
public void ListenClientConnect() { while (true) { TSocketBase clientSocket = new TSocketClient(_Listeners.Accept()); //连接到服务端的客户端Sokcet // 客户端字典中添加连接上的客户端 sockets.Add(clientSocket); //// 新开线程时刻更新客户端列表 //Thread appendIpThread = new Thread(RefreshIpList) //{ // IsBackground = true //}; //appendIpThread.Start(); // 新开线程监听连接上的客户端的信息传输 Thread receivedThread = new Thread(ReceiveMsg) { IsBackground = true }; receivedThread.Start(clientSocket); } }