Using Visual basic 6.0, I'm making a Winsock HTTP connection to a webserver, that sends data and closes right after. (Connection: keep-alive, doesn't do any good with this server)
Here's a piece of my code:
sck.SendData "GET /? HTTP/1.1" & vbNewLine & _
"Host: example.org" & vbNewLine & _
"Connection: keep-alive" & vbNewLine & vbNewLine
'// Wait for full Data:
Dim DATA As String
While (Not isEOF(DATA)) And sck.State = sckConnected
If sck.State = sckConnected Then sck.PeekData DATA
Sleep 10: DoEvents
Wend
If sck.State <> sckConnected Then
'// When this Fires, my data is missing the 25% of it's rest.
'// It is received corectly acording to Wireshark, but Winsock
'// closes the socket befor I can get the rest to the DATA variable.
MsgBox "This happends randomly 10% of the times.", vbInformation
End If
Clipboard.Clear
Clipboard.SetText DATA
Msgbox DATA
sck.Close
The isEOF() function does some checking stuff to see if data is full and ready (reading content-length, < /html> tag, or null chars). No need to post it here, because it's big and it's not the problem, trust me.
When you use: sck.GetData
or sck.PeekData
to a socket that was closed, you get an error. So how can I get the data from the socket after it's closed ?
The point here, is that 10% of the times, Winsock closes the socket befor I can get the rest of the DATA. I tried everything to fix the issue (using event handler, GetData instead of PeekData, etc) but I still get randomly the same error with the web-server i'm connecting to.
What is the correct way of using Winsock to corectly GET and wait for data ? What am I doing wrong in this code ?