Created
February 4, 2014 14:10
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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