Skip to content

Instantly share code, notes, and snippets.

@fdeitelhoff
Created February 4, 2014 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fdeitelhoff/8804272 to your computer and use it in GitHub Desktop.
Save fdeitelhoff/8804272 to your computer and use it in GitHub Desktop.
Manipulate the received data in a high performance c# socket server before it get's echoed back.
private void ProcessReceive(SocketAsyncEventArgs e)
{
if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
{
var token = (AsyncUserToken)e.UserToken;
//echo the data received back to the client
var data = System.Text.Encoding.UTF8.GetString(e.Buffer, e.Offset, e.BytesTransferred);
data = string.Format("{0}: {1}", DateTime.Now, data.ToUpper());
var buffer = System.Text.Encoding.UTF8.GetBytes(data);
e.SetBuffer(buffer, 0, buffer.Length);
if (!token.Socket.SendAsync(e))
{
ProcessSend(e);
}
}
else
{
CloseClientSocket(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment