public void ReadCallback(IAsyncResult ar) { String content = String.Empty; // Retrieve the state object and the handler socket // from the asynchronous state object. SocketCommunicationMessage state = (SocketCommunicationMessage)ar.AsyncState; Socket handler = state.workSocket; // Read data from the client socket. try { int bytesRead = handler.EndReceive(ar); if (bytesRead > 0) { state.BytesRead += (ulong)bytesRead; // There might be more data, so store the data received so far. // Check for end-of-file tag. If it is not there, read // more data. state.ReadMessage(); // Not all data received. Get more. handler.BeginReceive(state.Buffer, state.Offset, CommunicationMessage.BufferSize - state.Offset, 0, new AsyncCallback(ReadCallback), state); } } catch (SocketException e) { Console.WriteLine(e.Message); } }
public SocketCommunicationClient(Socket soket) { Socket = soket; SocketCommunicationMessage state = new SocketCommunicationMessage(); state.MessageDecoded += State_MessageDecoded; state.workSocket = Socket; Socket.BeginReceive(state.Buffer, state.Offset, CommunicationMessage.BufferSize - state.Offset, 0, new AsyncCallback(ReadCallback), state); }