I have a problem with winsock communication. I hope somebody can help.

I have a client server application running over the internet. The client request data by sending a command over the socket. The server gets the command and responds by sending a bunch of data back (like 10KB or so).


'Server send data code
public tcpServerSendComplete as boolean

For i = 1 To 50
MyString = "0004;" & Item(i).Text & ";" & Item(i).Key & ";"
tcpServerSendComplete = False 'this is a global
tcpServer.SendData MyString

Do While frmServer.tcpServerSendComplete = False
DoEvents
Loop
Next i

Private Sub tcpServer_SendComplete()
tcpServerSendComplete = True
End Sub



Although I painstakenly send one command at a time to the client from the server, and I'm carefully to wait for the data to be sent before proceeding with the next string of data, all of these commands/strings get merged together into one big data stream.


Here is my client code which should handle this sent data


Private Sub tcpClient_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
Dim MYstrData As String
tcpClient.GetData strData

List1.AddItem (bytesTotal) 'take note of packet size

If (Mid(strData, 1, 4) = "0004") Then
MYstrData = Mid(strData, 1, 33000)
Call FillTree(MYstrData)
'DoEvents 'tried with and without DoEvents
End If
End Sub


The sub FillTree is this big ugly thing. The important part is it has a static string which acts as a rotating buffer as I strip off commands from the beginning and add commands to the end.

It goes through lots of effort to handle cases where the first half of a command is sent in one packet and the second half is sent in the subsequent packet.

Regardless, the sub strips off the usable data. This function is not the problem.

OK, enough setup. Here is the problem...


Sometimes a few bytes dissapear into never never land. They just go missing. So i took a closer look and found that the server sends the stream in a packet which should have about 4064 bytes. But the client only receives 4060 bytes.

Specifically, in the getdata call after the data arrival event, the data is missing. It is not obviously missing because it is perfectly reasonable for the remaining data to show up in the next packet. The next packet show up with good data, but missing the end of the previous data.

Well it turns out 4060 is one of theose magic numbers. It refers to the MSS or Maximum Segment Size, which is apparently the largest packet size which can be sent over an Eithernet. And furthermore, this MSS is variable depending on WAN, LAN, router, header, and maybe even wind speed.

So the question. How do I handle this? it seems that i should find the smallest possible MSS (around 530) and call that the maximum MSS allowable. Then, the sever has to insert some sort of interuption to allow the client to getdata. I've seen others user a timer for this, but that is problematic in itself.

Keep in mind I have one server with 10,000 clients; so any bubble could potentially have an impact on all users. Also, I can make system or router changes to the server only, Client machine are out of my control.