private static void ReceiveCallback(IAsyncResult ar) { ClientStateObject state = (ClientStateObject)ar.AsyncState; TcpClient t = state.c; Socket s = t.GetSocket(); if (s == null) { return; } try { int read = s.EndReceive(ar); if (read > 0) { t.onReceive(state.buffer, read); s.BeginReceive(state.buffer, 0, state.buffer.Length, 0, new AsyncCallback(ReceiveCallback), state); } else { t.onClose(s); } } catch (System.Exception ex) { t.onClose(s); Log.Instance().WriteLog(ex.Message); Log.Instance().WriteLog(ex.StackTrace); } }
public void SendData(byte[] data) { if (!mSocket.Connected) { return; } ClientStateObject state = new ClientStateObject(); state.c = this; state.s = this.mSocket; mSocket.BeginSend(data, 0, data.Length, 0, new AsyncCallback(SendCallback), state); }
public static void SendCallback(IAsyncResult ar) { ClientStateObject state = (ClientStateObject)ar.AsyncState; try { int retlen = state.s.EndSend(ar); } catch (System.Exception ex) { Console.WriteLine(ex.Message); state.c.onClose(state.s); } }
private static void ConnectCallback(IAsyncResult ar) { TcpClient t = (TcpClient)ar.AsyncState; try { Socket s = t.GetSocket(); s.EndConnect(ar); t.onConnect(true); ClientStateObject state = new ClientStateObject(); state.s = s; state.c = t; s.BeginReceive(state.buffer, 0, state.buffer.Length, 0, new AsyncCallback(ReceiveCallback), state); } catch (System.Exception ex) { t.onConnect(false); //Log.Instance().WriteLog(ex.Message); //Log.Instance().WriteLog(ex.StackTrace); } }
public void SendData(byte[] data) { if (!mSocket.Connected) return; ClientStateObject state = new ClientStateObject(); state.c = this; state.s = this.mSocket; mSocket.BeginSend(data,0,data.Length,0,new AsyncCallback(SendCallback), state); }