How do I “flush” a TCP Client Buffer in C# socket server ? RRS feed

  • Question

  • I've pulled from several examples to setup a C# TCP Client server application. I am using asynchronous communication. The basic concept is that the client and server both send/receive messages (commands and events). Therefore, I've developed a class that exposes a Send method to send messages to the server and a event handler to receive messages from the server. Everything works except I can't seem to flush the buffer of messages received from the server. Each time the client  sends a new message, the buffer in my socket contains all the old messages (already read) and the new message(s) . I want only latest one . Please tell me how to flush buffer after receiving a message and receive next message.
    Friday, January 10, 2014 12:29 PM


  • You're calling OnMessageReceived even if the buffer has not been filled (in case bytesRead < count). Consider switching to await for the async part of the application. That gets rid of the hideous callback recursion.
    Wednesday, January 15, 2014 10:26 AM