private void AcceptCallback(IAsyncResult ar) { TcpClient client = m_listener.EndAcceptTcpClient(ar); m_listener.BeginAcceptTcpClient(m_acceptCallback, null); #region 用线程池接收 //ThreadPool.QueueUserWorkItem(ReceiveCallback, client); #endregion #region 直接接收 try { NetworkStream stream = client.GetStream(); byte[] buffer = new byte[sizeof(int)]; stream.Read(buffer, 0, buffer.Length); int count = BitConverter.ToInt32(buffer, 0); while (count > 0) { ExcuteAction action = new ExcuteAction(); action.Deserialize(stream); OnNotify(action); count--; } client.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); } #endregion }
private void ReceiveCallback(IAsyncResult ar) { try { Byte[] buffer = m_udp.EndReceive(ar, ref m_receivePoint); MemoryStream ms = new MemoryStream(buffer); ExcuteAction action = new ExcuteAction(); action.Deserialize(ms); OnNotify(action); m_udp.BeginReceive(m_receiveCallback, null); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private void ReceiveCallback(Object threadContext) { try { TcpClient client = threadContext as TcpClient; NetworkStream stream = client.GetStream(); while (stream.DataAvailable) { ExcuteAction action = new ExcuteAction(); action.Deserialize(stream); OnNotify(action); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }