1

I have been working on a project where I send strings from a client to a server in C#. My questions are the following, if some of you have the time to answer:

  • I send my string as following:

    clientStream.Write(buffer, 0, buffer.Length);
    clientStream.Flush();
    

where clientStream is a NetworkStream. But it becomes a problem if the functions are in a while loop as following:

while(true)
{
        clientStream.Write(buffer, 0, buffer.Length);
        clientStream.Flush();
}

The messages seems to sometimes be intertwined and corrupt at the server part. But not if I add a Thread.Sleep (with perhaps 30 as an in parameter) call in the while loop. I wonder why the messages gets intertwined if it's an TCP socket connection? How does the function calls:

clientStream.Write(buffer, 0, buffer.Length);
clientStream.Flush();

work?

  • How many messages can you send per second via a TCP socket connection? What does the speed depend on? Thank you for your time!
4
  • 3
    TCP is stream-based, not message-based. Do not think in terms of messages. Two calls to Write will not necessarily result in two calls to Read on the server. The data may be split or combined at any point. It will always be sequential, however. Commented Jul 24, 2012 at 16:45
  • It's very, very unlikely the messages are corrupt because of TCP. My guess is you're just writing garbage to the socket somehow without the sleep. Is your application writing to the same socket from multiple threads? (Usually a good guess when the problem is "garbage output".) Commented Jul 24, 2012 at 16:47
  • It would also be interesting to know how you're creating and initialising buffer, and how you're reassembling the messages on the server. (And how you determine when a message starts and ends.) Commented Jul 24, 2012 at 17:02
  • Are you connecting to your own machine? Then it is very important that your receiving code keeps on reading, or the sender will block. Also check if all bytes are witten, read the return value of Write. Commented Jul 24, 2012 at 17:06

1 Answer 1

1

Don't flush your client stream always, you only want to flush at the end of your write cycle otherwise let .NET take care of it for you. Flushing every time you send something is inefficient.

Also remember that TCP streams can arrive in a different order and are re-assembled on the recipient side to make sense using TCP Serials, this is done transparently but if your looking at the streams via wireshark then you may be seeing this.

In theory you can send as much data through a socket as your bandwidth supports. But you should never reply on Thread.Sleeps as this will blow up on you one day. Relying on timing for ANYTHING like this is considered a "dirty hack" and will cause issues.

Sign up to request clarification or add additional context in comments.

2 Comments

It should be pointed out that unless the server is for some reason using raw sockets instead of TCP, the reassembly would be done transparently and correctly from the point of view of the application.
@millimoose thats completely true. I'll try and clarify the answer to reflect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.