/// <summary> /// 接收回调 /// </summary> /// <param name="ar"></param> protected void ReceiveCallback(IAsyncResult ar) { StateObject state = (StateObject) ar.AsyncState; Socket handler = state.WorkSocket; int recLen; try { recLen = handler.EndReceive(ar); } catch (Exception) { return; } if (recLen <= 0) { handler.Shutdown(SocketShutdown.Both); handler.Close(); return; } byte[] buffer = new byte[recLen]; Array.Copy(state.Buffer, 0, buffer, 0, recLen); bool readState = AnalyticReceiveData(buffer, state.ReceiveBytes, state.Contents); if (readState && state.ReceiveBytes.Count == 0) { //Event Sender sender = new Sender(handler); SocketReceiveProcess(new SocketReceiveEventArgs(sender, state.Contents)); state.Contents.Clear(); } try { handler.BeginReceive(state.Buffer, 0, StateObject.BufferSize, SocketFlags.None, ReceiveCallback, state); } catch (Exception) { handler.Shutdown(SocketShutdown.Both); handler.Close(); } }
public SocketReceiveEventArgs(Sender dataSender, List<string> contents) { DataSender = dataSender; Contents = contents; }
/// <summary> /// 发送数据 /// </summary> /// <param name="data"></param> public void Send(string data) { Sender sender = new Sender(_sender); sender.Send(data); }
public SocketReceiveEventArgs(Sender dataSender, List <string> contents) { DataSender = dataSender; Contents = contents; }