Results 1 to 22 of 22

Thread: Winsock question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    150

    Winsock question

    I got this code from a previous thread.

    But it does not seem to work for me it gets stuck in Do Unitl Winsock.state = 7 loop. Any ideas?

    VB Code:
    1. Private DLFile As String
    2.  
    3. Private Sub Command1_Click()
    4. Dim txtServer As String
    5. Dim txtFile As String
    6.  
    7. txtFile = "/operational/pibs/pib1.shtml"
    8. txtServer = "www.nats.co.uk"
    9.  
    10.     Winsock1.RemoteHost = txtServer
    11.     Winsock1.RemotePort = 80
    12.     Winsock1.Connect
    13.     Do Until Winsock1.State = 7
    14.         DoEvents
    15.     Loop
    16.     Winsock1.SendData "GET " & txtFile & " HTTP/1.1" & vbCrLf
    17.     DoEvents
    18.     Winsock1.SendData "Host: " & txtServer & vbCrLf
    19.     DoEvents
    20.     Winsock1.SendData vbCrLf
    21. End Sub
    22.  
    23. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    24.     Dim NewData As String
    25.     Winsock1.GetData NewData
    26.     DLFile = DLFile & NewData
    27.    
    28.     Debug.Print DLFile
    29. End Sub
    Last edited by Granger9; Mar 20th, 2006 at 04:50 PM.

  2. #2

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    150

    Re: Winsock question

    I see, I take it this process should not take very long at all??

    My interent is through a wireless router would that be affecting it?

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    150

    Re: Winsock question

    My other solution was what i posted in my other thread but no-one seemed to help?

    Can anyone help now please?

  5. #5
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Winsock question

    I dont know who wrote that example but its very bad to have it loop until the socket is state 7 (connected)

    its looping over and over because the socket is not connecting, meaning the server is not responding to the connection or it doesnt exist.

    it is either a problem with the way you are connecting, which you should try like this..

    VB Code:
    1. Winsock1.Connect txtHost.Text, 80

    what are you typing into the text field to connect to? you should not be putting:

    http://www.google.com or
    www.google.com

    it should be

    google.com

    remove that awful loop. The winsock has a connect event (which fires when the socket connects successfully, so put all the processing which is below the loop into the connect event.

    the winsock also has an error event, which fires if the socket errors or fails to connect, put a msgbox in there, there is a 'Description' variable, so something like...

    VB Code:
    1. 'winsock error event
    2. MsgBox "Error: " & Description
    3. Winsock1.Close

    There is also a close event (this fires when the remote host terminates the connection), where you should have

    VB Code:
    1. Winsock.Close

    and possibly something telling you that the connection was dropped by remote side.

    in your DLfile variable which holds the file contents, it will also contain the headers for the HTTP request which you will need to cut off because if you dont, and you save the data as a file, it will be corrupted.

    Also.....post winsock questions in the Network Programming forum, they will get lost quickly in this forum
    Chris

  6. #6
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Winsock question

    I just noticed a problem with yout HTTP request for the file, instead of sending it as 3 packets, just send it as one packet, and if I remember right I think it is terminated by two vbCrLF

    I also just noticed this

    VB Code:
    1. txtServer = "www.nats.co.uk"

    when you connect a winsock you specify either the IP address of the server or the domain name, www doesnt resolve to an IP, only the

    nats.co.uk

    will resolve to the IP, that is why its not connecting
    Last edited by the182guy; Mar 20th, 2006 at 06:03 PM.
    Chris

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    150

    Re: Winsock question

    Thanks for the reply I have put the following code in. Now i get a winsock error: Authoratative Answer: Host Not Found.
    VB Code:
    1. Private DLFile As String
    2.  
    3. Private Sub Command1_Click()
    4. Dim txtServer As String
    5. Dim txtFile As String
    6.  
    7. txtFile = "/operational/pibs/pib1.shtml"
    8. txtServer = "nats.co.uk"
    9.  
    10. 'Winsock1.Connect txtHost.Text, 80
    11. Winsock1.Connect txtServer, 80
    12.  
    13. End Sub
    14.  
    15. Private Sub Winsock1_Connect()
    16.     Winsock1.SendData "GET " & txtFile & " HTTP/1.1" & vbCrLf
    17.     DoEvents
    18.     Winsock1.SendData "Host: " & txtServer & vbCrLf
    19.     DoEvents
    20.     Winsock1.SendData vbCrLf
    21. End Sub
    22.  
    23. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    24.     Dim NewData As String
    25.     Winsock1.GetData NewData
    26.     DLFile = DLFile & NewData
    27.    
    28.     Debug.Print DLFile
    29. End Sub
    30.  
    31. Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
    32. 'winsock error event
    33. MsgBox "Error: " & Description
    34. Winsock1.Close
    35.  
    36. End Sub

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    150

    Re: Winsock question

    Im extreamly new to all of this winsock stuff, how would i send it as one packet

  9. #9
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Winsock question

    change to

    VB Code:
    1. Winsock1.SendData "GET " & txtFile & " HTTP/1.1" & vbCrLf & "Host: " & txtServer & vbCrLf & vbCrLf

    the error means the socket could not find 'nats.co.uk' that is not a problem with the code, it means the site is not up right now, which it isnt because if you try to go to nats.co.uk in your web browser you get the same error
    Chris

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    150

    Re: Winsock question

    Thanks. This is my code so far but I am still getting the same error even though I can access the site via my browser and download the file using an INET control!?

    VB Code:
    1. Private DLFile As String
    2.  
    3. Private Sub Command1_Click()
    4. Dim txtServer As String
    5. Dim txtFile As String
    6.  
    7. txtFile = "/operational/pibs/pib1.shtml"
    8. txtServer = "nats.co.uk"
    9.  
    10. 'Winsock1.Connect txtHost.Text, 80
    11. Winsock1.Connect txtServer, 80
    12.  
    13. End Sub
    14.  
    15. Private Sub Winsock1_Connect()
    16. Winsock1.SendData "GET " & txtFile & " HTTP/1.1" & vbCrLf & "Host: " & txtServer & vbCrLf & vbCrLf
    17. End Sub
    18.  
    19. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    20.     Dim NewData As String
    21.     Winsock1.GetData NewData
    22.     DLFile = DLFile & NewData
    23.    
    24.     Debug.Print DLFile
    25. End Sub
    26.  
    27. Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
    28. 'winsock error event
    29. MsgBox "Error: " & Description
    30. Winsock1.Close
    31.  
    32. End Sub

  11. #11
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Winsock question

    that is strange, i cant access nats.co.uk/operational/pibs/pib1.shtml or nats.co.uk with my web browser, it says the site is down, which is why the winsock errors
    Chris

  12. #12
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Winsock question

    ah it seems it should infact be www.nats.co.uk

    This is weird because google.com connects as does google.co.uk but forsome reason your site requires the www
    Chris

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    150

    Re: Winsock question

    Thanks.

    How would I now make it so its just the HTML in the DLFile. Also is there anyway I can check the progress of it so I can make a progress bar. (as I couldn't seem to do it when i was using INET)

  14. #14
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Winsock question

    DLFile = mid(DLFile, instr(DLFile, vbcrlf & vbcrlf)+1)

    i havent tested, this may cut off the first char of the file so test it, if so remove the +1 if not leave it, if it leaves the two vbcrlf in then use +2 etc

    as for the progress bar its difficult because you have to read the content-length header to see how long the file is and monitor when downloading, if its a web page it will download almost instantly anyway
    Chris

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    150

    Re: Winsock question

    The DLFile does not appear to contain anything. I put a stop on DLFile = DLFile & NewData and it never occured once!?!

    VB Code:
    1. Private DLFile As String
    2.  
    3. Private Sub Command1_Click()
    4. Dim txtServer As String
    5. Dim txtFile As String
    6.  
    7. txtFile = "/operational/pibs/pib1.shtml"
    8. txtServer = "www.nats.co.uk"
    9.  
    10. 'Winsock1.Connect txtHost.Text, 80
    11. Winsock1.Connect txtServer, 80
    12. DLFile = Mid(DLFile, InStr(DLFile, vbCrLf & vbCrLf) + 1)
    13.  
    14. MsgBox "WOOP WOOP"
    15.     Debug.Print DLFile
    16. End Sub
    17.  
    18.  
    19. Private Sub Winsock1_Connect()
    20. Winsock1.SendData "GET " & txtFile & " HTTP/1.1" & vbCrLf & "Host: " & txtServer & vbCrLf & vbCrLf
    21. End Sub
    22.  
    23. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    24.     Dim NewData As String
    25.     Winsock1.GetData NewData
    26.     DLFile = DLFile & NewData
    27.  
    28. End Sub
    29.  
    30. Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
    31. 'winsock error event
    32. MsgBox "Error: " & Description
    33. Winsock1.Close
    34.  
    35. End Sub

  16. #16
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Winsock question

    thats in the wrong place, put it in the winsock_close event but before you put the code, put Winsock.Close first

    why there? because this event will fire when the server has finished sending the file and it will close the connection, you may need to put the Connection: Close header next to the Host: part, but try without first.

    just so its clear, this is the part which needs to be moved:
    VB Code:
    1. DLFile = Mid(DLFile, InStr(DLFile, vbCrLf & vbCrLf) + 1)
    2.  
    3. MsgBox "WOOP WOOP"
    4.     Debug.Print DLFile
    Chris

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    150

    Re: Winsock question

    In DLFile it says

    Code:
    Your browser sent a request that this server could not understand.<P>
    Invalid URI in request GET  HTTP/1.1
    also the msgbox seems to be in an infinte loop as it keeps appear once its in the close event

  18. #18
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Winsock question

    what have you changed in the request?

    in the close event try putting winsock.close before the msgbox, if it still loops, remove the msgbox and change a labels caption instead
    Chris

  19. #19
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 2003
    Location
    Utah
    Posts
    1,068

    Re: Winsock question

    Try changing txtServer = "www.nats.co.uk" to txtServer = "http://www.nats.co.uk"

    Sometimes they like the full path.

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    150

    Re: Winsock question

    Thank you for the code. It works exaclty how I wanted it to. How would I find out the file size before I start downloading it so I can create a progress bar etc.

    VB Code:
    1. Public EntireFile As String
    2.  
    3. Private Sub Command1_Click()
    4. EntireFile = ""
    5. Winsock1.Connect "www.nats.co.uk", 80
    6. Label1.Caption = "connecting..."
    7. End Sub
    8.  
    9. Private Sub Winsock1_Close()
    10. 'complete
    11. Label1.Caption = "complete"
    12. Winsock1.Close
    13.  
    14. EntireFile = Mid(EntireFile, InStr(EntireFile, "<html>"))
    15.  
    16. text1.Text = EntireFile
    17. End Sub
    18.  
    19. Private Sub Winsock1_Connect()
    20. Label1.Caption = "connected, sending data...."
    21. Winsock1.SendData "GET /operational/pibs/pib1.shtml HTTP/1.1" & vbCrLf & "Host: www.nats.co.uk" & vbCrLf & "Connection: Close" & vbCrLf & vbCrLf
    22. End Sub
    23.  
    24. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    25. Dim NewData As String
    26.  
    27. Winsock1.GetData NewData
    28.  
    29. Label1.Caption = "receiving data..."
    30. Debug.Print NewData
    31.  
    32. EntireFile = EntireFile & NewData
    33. End Sub
    34.  
    35. Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
    36. Label1.Caption = Description
    37. Winsock1.Close
    38. End Sub

  21. #21
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 2003
    Location
    Utah
    Posts
    1,068

    Re: Winsock question

    You can get the size of a file from *Most* servers with the HEAD command. Use it instead of GET before you download the file and it will return the file size in Bytes.

  22. #22

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    150

    Re: Winsock question

    I think this is the date I got from the head. I guess as there is no file size that I can not get the file size?

    Code:
    HTTP/1.1 501 Method Not Implemented
    Date: Wed, 22 Mar 2006 19:38:23 GMT
    Server: Apache/1.3.27
    Allow: GET, HEAD, OPTIONS, TRACE
    Connection: close
    Transfer-Encoding: chunked
    Content-Type: text/html; charset=iso-8859-1
    
    173

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width