Will TCPStream.Read be blocked until all data has been received in the send message?

I wrote a multi-threaded simple server that uses:

clientStream.Read(message, 0, 4096); 

Block until the client sends a message. The code then processes this message.

So far I have only used it to send short commands

 ex. "#login[username][login]" 

but I'm worried that when I send huge table data line by line, the code may continue until all of them are received.

It will be so. Read the block until all the sent message is received, or is it unlocked when any data is received?

+4
source share
1 answer

Providing a package to complete to delimit messages is bad. If not for the simplest reasons. I can connect to your server and easily collapse it.

You will need to create a wired protocol. You have several options.

  • Always expect the message size as a 32-bit unsigned integer as the first 4 bytes, and then read it until you fill this amount of data.

  • Specify a separator for the end of the message. This means that you will maintain a buffer. You fill it with data from the wire, quickly scan it to complete the message. After you find the message, you drop it into the message processor, and then continue with the rest of the bytes in the buffer, repeating step 1.

You can look at STOMP for a simple text protocol.

Do not rely on your packages being sent in good, well-defined fragments as a message separator.

+7
source

All Articles