0
votes

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 ?

3
To paraphrase Obi-Wan: "Use the events, Luke! This isn't the QBasic you're looking for."Bob77

3 Answers

0
votes

can you try the following code ?

'1 form with :
'    1 winsock control : name=Winsock1
'    1 command button  : name=Command1
Option Explicit

Private Sub Command1_Click()
  With Winsock1
    .Connect "<your server>", <your port>
    Do
      DoEvents
    Loop Until .State = sckConnected
    .SendData <your command>
  End With 'Winsock1
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
  Dim strData As String
  Winsock1.GetData strData
  ProcessData strData
End Sub

Private Sub ProcessData(strData As String)
  Static strTotal As String
  strTotal = strTotal & strData
  If IsComplete(strTotal) Then 'your check to see if data is complete
    Winsock1.Close
  End If
End Sub

i just ran it a couple of times, and never missed any data while retreiving large chunks of data (covering several dataarrival events before the data was complete)

also be sure to do your other things somewhere in the ProcessData procedure, and of course empty strTotal after doing your thing :)

0
votes

Your question exhibits several misconceptions.

  1. Winsock never closes the socket unless you close it. If you are encountering a closed socket, you closed it.

  2. If the peer closes his socket, that constitutes closing of the connection. However all data that the peer has queued to send will still be sent, and you will still receive it, prior to receiving whatever the EOS indication is in VB. You will then receive the EOS, on which you should close your socket.

0
votes

Close socket in one side, then call recv from the other side and examine the return value.