C# .NET Socket Programming
Here is the situation:
I am communicating with an internal company Market Data Server. I am creating a client socket using AddressFamily.Internetwork, SocketType.Stream and ProtocolType.TCP. I am firing off multiple requests to the this server using asynchronous socket I/O. Everything is working fine, however there is one scenario that I am not sure how to handle.
First, here is a description of how the data looks, once received in my byte array:
There is a 4 byte header that contains the message length and then the message. Once converted to a string, the message has a header section that contains a user supplied RequestID (from the request that was sent to the server) a LastMessage Flag to notify the client whether there is no more data to receive for that paricular response and then the message content.
The maximum amount of data that the server can send back at one time is 8K of data. The buffer I am sending in is 32K.
When the bytes come back in the response, there can be many server messages contained in the byte array. It can start at the 4 byte header, or if the bytes are at a message boundary, the bytes received can start in the middle of a message.
When I fire off my requests to the server, I am holding a StringBuilder object in a Dictionary using the RequestID as the key. When the data gets returned, I am converting the bytes into a string and then parsing the string and extracting each message within it (starting with a |SOH=| flag and ending with a |EOM=| flag. Once I extract a message, I get the RequestID and then use that to access the appropriate StringBuilder within the dictionary and append the newly arrived message to the StringBuilder.
Here is where a problem can be. There can potentially be a situation where only a few bytes are received from the server (because of bandwith issues or the like) and once converted to a string there is no RequestID because the chunk of data is past the point of the RequestID. I don't know in this scenario where this newly received amount of data should be routed to. Potentially, I can have several outstanding messages waiting to be appended to in the Dictionary and if I don't have a RequestID, I cannot route them to the proper place and therfore my data would be fragmented.
How can I handle this issue? Is there some way to inspect the IP packet for some sort of identifying information? Is there some other way to know which byte array belongs to which message?