As you see, I wrote myScoket.Recieve twice because if i use it once, i get only the first byte of the message, and if i use it twice the message become complete, but it’s a problem because what if the message had only one byte? The code gets stuck…
@ Reinhard Ostermeier - The first byte isn’t the length info, for example I want to send a message: "ABCDEFG"
When I read the the message, I get “A” for the first time I call “receive” and at the next time I get “BCDEFG”.
I want to use receive once to get the whole "ABCDEFG"
What do u say?
What triggers your readings? Socket is highly asynchronous, it may accept new data while still in .Receive() function! what does myScoket.Available property says before, between and after your reads?
Normally if you send it from the other side with a single Send command, you should receive it as a single packet.
But receiving is an asynchronous process you never can be sure.
There for a good protocol design includes some kind of framing to know when a message is complete.
This could be the length info at the beginning, or if you have ASCII messages some special start and end characters (Look at an ASCII table, and you will find them).
Sending just plain messages without a defined beginning and end is (please apologize) “Stupid protocol design”.
Another method would be to make a minimum break between messages on the sender side, but in my opinion this is not a good deign as well, and it’s harder to read.
There are several methods/properties on a socket you can use to check if data is available for reading, like Poll() or Available.
@ Reinhard Ostermeier - Can I assume that when I call mySocket.receive, I will get one byte ?? if the message’s length is 10 bytes? I will get just “1” as the size of the message.
One of the parameters of read is the number of bytes you want to get. It will block until at least 1 byte is available.
The return value of read tells you how many bytes you have got, and have been written into your buffer. This will never be more then you wanted. The remaining bytes stays in the driver until you read it.
In the case you want 1 byte, the return value is always 1 when it returns with one exception.
If the socket gets closed (from the other side or a different thread) it return 0.
The code I posted above uses these features of the read method in the while loop