protected void ReadCallBack(IAsyncResult result) { try { TCPClientBase tcpClientBase = (TCPClientBase)result.AsyncState; Int32 count = tcpClientBase.NetworkStream.EndRead(result); if (count > 0) { // } else { // } if (IsRunning) { tcpClientBase.NetworkStream.Flush(); tcpClientBase.NetworkStream.BeginRead(tcpClientBase.CacheSync, 0, tcpClientBase.CacheSync.Length, ReadCallBack, tcpClientBase); } } catch (Exception ex) { } finally { } }
protected void AcceptedClientCallBack(IAsyncResult result) { try { TcpListener listener = (TcpListener)result.AsyncState; TcpClient client = listener.EndAcceptTcpClient(result); TCPClientBase tcpClientBase = TCPClientBase.GetInstance(client, this.BufferSize); lock (_lockObject) { if (this.Clients == null) { this.Clients = new List <TCPClientBase>(); } if (!this.Clients.Contains(tcpClientBase)) { this.Clients.Add(tcpClientBase); } } tcpClientBase.NetworkStream.Flush(); tcpClientBase.NetworkStream.BeginRead(tcpClientBase.CacheSync, 0, tcpClientBase.CacheSync.Length, ReadCallBack, tcpClientBase); if (IsRunning) { listener.BeginAcceptTcpClient(AcceptedClientCallBack, result.AsyncState); } } catch (Exception ex) { throw ex; } finally { } }
public static TCPClientBase GetInstance(TcpClient client, int buffSize) { if (client != null && client.Connected && buffSize > 0) { TCPClientBase tcp = new TCPClientBase(); tcp.Client = client; tcp.NetworkStream = tcp.Client.GetStream(); tcp.CacheSync = new byte[buffSize]; tcp.CacheAsync = new byte[buffSize * 10]; tcp.RemoteEndPoint = client.Client.RemoteEndPoint; return(tcp); } else { throw new ArgumentNullException(client.GetType().ToString()); } }
void ConnectCallBack(IAsyncResult result) { TcpClient client = (TcpClient)result.AsyncState; try { _client = TCPClientBase.GetInstance(client, this.BuffSize); client.EndConnect(result); IsConnected = true; //处理连接事件 未完成 //client.GetStream().BeginRead(_client.CacheAsync } catch (Exception ex) { } finally { } }
public void Send(byte[] bytes, TCPClientBase client) { if (client != null && client.Client.Connected && client.NetworkStream != null) { } }