tags:

views:

27

answers:

1

I have a sender, a message forwarder which sends fix sizes of byte data at a rate of 5 milliseconds per message to my receiving program written in vb6, when I run the message fowarder and my receiving program on one machine, there's no issue but when they run on separate machines, the receiving program starts to experience some abnormalities.

e.g:

private sub socket_DataArrival(index as integer, ByVal dataTotal as Long)

Dim Data() as Byte
Length.Text = dataTotal

socket.GetData byteData, vbArray + vbByte

If Length.Text = "100" Then
txtOutput.Text = "Message1"

ElseIf Length.Text = "150" Then
txtOutput.text = "Message2"

End Sub

I will sometimes receive "2 in 1" message as in it comes in as 250 bytes or a non-recognizeable byte size when I should be receiving either 100 or 150 only but if I reduce the sending rate to a slower speed say 50 milliseconds per message then it will be fine.

Can any advice? Thanks.

A: 

When sending data over a network you have to get used to the fact that the packets may arrive out of order, not promptly, not at all, etc.

You need to improve your message protocol to include a header that states which type of message follows. If order is important include a sequence number (I'm assuming you're using UDP). At present you are relying on timing to separate messages, which you cannot rely on over a network.

Buffer all your arriving data and handle it in chunks - the header allows you to tell what chunk size to use. Separate your input buffering from your message handling - use the DataArrival event to add data to the buffer, use a Timer or some other means of polling the buffer to check if it has messages ready to parse. Alas, this is VB6 so threading is not so easy. Take a look at The Common Controls Replacement Project timer object DLL if you need a Timer class that doesn't rely on a UI element being present.

Adrian
TCP data will arrive in order. Instead of a header a delimiter can also be used following a "message." I can't see any reason for a Timer of any sort however. In DataArrival you just append the new data to the buffer then parse out and handle any complete messages.
Bob Riemersma
Thanks for the advices, I using TCP for the data transmission, I am not sure if I can use a delimiter because the incoming bytes aren't string messages but bytes of arrays which will be process later by shifting out the bytes accordingly. From the above example, if I received 250 bytes, there may be a way to split it but sometimes it arrives as some irregular size like 180...etc Correct me if I am wrong. Thanks.
For binary data you pretty much want a length header as already suggested above.
Bob Riemersma