private void ReadCallback(IAsyncResult ar) { NetUtil.Log("Reading data"); // Get the socket input buffer from the callback result SocketDataBuffer state = (SocketDataBuffer)ar.AsyncState; Socket socket = state.TargetSocket; // Read data from the client int bytesRead = socket.EndReceive(ar); if (bytesRead > 0) { state.AppendData(Encoding.UTF8.GetString(state.Buffer, 0, bytesRead)); // Pop a message if one is available if (state.HasMessage) { string message = state.PopMessage(); NetUtil.Log("Message: " + message); callback(message); } //Keep reading socket.BeginReceive(state.Buffer, 0, SocketDataBuffer.BufferSize, 0, new AsyncCallback(ReadCallback), state); } }
private static void SendCallback(IAsyncResult ar) { try { // Retrieve the socket from the state object. Socket handler = (Socket)ar.AsyncState; // Complete sending the data to the remote device. int bytesSent = handler.EndSend(ar); NetUtil.Log("Sent " + bytesSent + " bytes to client."); } catch (Exception e) { NetUtil.Log(e.ToString()); } }
private static void SocketSend(Socket handler, String data, string symkey) { //Encrypt if (symkey != null) { // data = Convert.ToBase64String(TDESHandler.Encrypt(symkey, data)); } NetUtil.Log("About to send: " + data); //Add message ending data += SocketDataBuffer.EOF; // Convert the string data to byte data using UTF8 encoding. byte[] byteData = Encoding.UTF8.GetBytes(data); // Begin sending the data to the remote device. handler.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), handler); }