Asynchronous socket question
-
Hi all, I have a continuous stream of packet data going through my asynchronous socket connection. My question is how can I tell in the EndReceive callback method where one package of data ends and another begins? I coded the socket class in C# 1.1. Thanks in an advance. :) Ken
-
Hi all, I have a continuous stream of packet data going through my asynchronous socket connection. My question is how can I tell in the EndReceive callback method where one package of data ends and another begins? I coded the socket class in C# 1.1. Thanks in an advance. :) Ken
You have to have a delimiter in the data. And once you read in the delimiter in the callback, you'll know you got to the end of one message/stream and you're now reading for the next message/stream. So a very simple protocol is needed to delimit your messages. You have to make sure you read all the message as it may/may not (certainly not guaranteed) to be send in a single socket.send.
-
You have to have a delimiter in the data. And once you read in the delimiter in the callback, you'll know you got to the end of one message/stream and you're now reading for the next message/stream. So a very simple protocol is needed to delimit your messages. You have to make sure you read all the message as it may/may not (certainly not guaranteed) to be send in a single socket.send.
I like to prefix my messages with a length rather than delimit them.
A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane
-
Hi all, I have a continuous stream of packet data going through my asynchronous socket connection. My question is how can I tell in the EndReceive callback method where one package of data ends and another begins? I coded the socket class in C# 1.1. Thanks in an advance. :) Ken
The more appealing and real world solution for all types of data transfer is to prefix each messaeg with its length. Procedure is as follow. Sending Part: 1) Calculate the Number of bytes produced by a message. 2) Store length as an integer value. 3) Convert the Length (integer) into bytes using BitConverter.GetBytes(messageLength) method. 4) Send these bytes on stream. 5) Send the original message. Receiving Part: 1) Read 4 bytes from the stream Asynchronously. 2) Convert these bytes into integer value using BitConverter.ToInt32(buffer,0) 3) Now creat a Memory Stream and loop on it untill that message size is met. Its the best technique in my knowledge and supports in Network delay situation for other type of data (Not String) If you have further queries, feel free to conact me ;) rizwansharp@hotmail.com Best Regards, Rizwan