Problem : How to reset VB winsock receive buffer flag or clear buffer through code?

Problem : How to reset VB winsock receive buffer flag or clear buffer through code?

Hi,
My  application (VB6)  uses winsock with TCP/IP protocol to transfer large amount of data (1MBytes and up) to another system.
When receiving data using slow computer speed (like 1.7GHz with windows XP) – data is lost.
I payed attention that one DataArrival event “chases” another and the operation doesn’t return to calling function (from DoEvents command) by program debugging.
When program runs on 3GHz PC the problem is less serious (1 of ~180 data receiving operation fails).
The goal is to create code which will work on any platform, no matter what is the computer speed.
I suspect that winsock is forced to handle with next 8KBytes (size of receive buffer) chunk before it resets internal receive buffer flag or clears buffer (be prepared for next packet).
If i insert DoEvents command in the end of DataArrival event sub the winsock succeed to perform these actions (the data receiving completed successfully),  but the next event is fired from this command and so on and nesting is created. For large files “Stack out of space error” occurs, which terminates the transfer.
Am i right about understanding the socket receive process?
There is VB method or API  commad of winsock to do this through code at the end of DataArrival event?
I appreciate any help.
Thanks.


 

Solution : How to reset VB winsock receive buffer flag or clear buffer through code?

When transferring data, break the data into small chunks. Send the chunks one at a time, and when you receive them, send back an acknowledgment. Wait until you receive an acknowledgment for one chunk before sending the next.

Simple example follows

Sample client:
Option Explicit
Private mstrMyStrings(3) As String
Private intPos As Integer

Private Sub Command1_Click()
Winsock1.RemoteHost = “127.0.0.1 ”
Winsock1.RemotePort = 5000
Winsock1.Connect

End Sub

Private Sub Form_Load()
mstrMyStrings(0) = “This is a test ” & vbCrLf
mstrMyStrings(1) = “of the emergency broadcast system ” & vbCrLf
mstrMyStrings(2) = “I will understand ” & vbCrLf
mstrMyStrings(3) = “if you choose to ignore it. ” & vbCrLf
End Sub

Private Sub Winsock1_Connect()
Winsock1.SendData mstrMyStrings(0)

End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Debug.Print “DataArrival ”
Static strReceive As String
Dim strTemp As String
Winsock1.GetData strTemp
strReceive = strReceive & strTemp
Debug.Print strReceive
If CInt(strReceive) = Len(mstrMyStrings(intPos)) Then
strReceive = ” ”
intPos = intPos + 1
If intPos = UBound(mstrMyStrings) + 1 Then
Winsock1.Close
Exit Sub
End If
Debug.Print mstrMyStrings(intPos)
Winsock1.SendData mstrMyStrings(intPos)
End If
End Sub

Server example:
Option Explicit
Private strArrive As String
Private Sub Form_Load()

Winsock1.LocalPort = 5000
Winsock1.Listen
End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State < > sckClosed Then
Winsock1.Close
End If
Winsock1.Accept requestID

End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Debug.Print “DataArrival ”
Dim strTemp As String
Dim strEnd As String
strEnd = vbCrLf
Winsock1.GetData strTemp
strArrive = strArrive & strTemp
If InStr(1, strArrive, strEnd) Then
Winsock1.SendData CStr(Len(strArrive)) ‘Send length of string as acknowledgment
Debug.Print strArrive
strArrive = ” ”
End If
End Sub