|
-
Mar 20th, 2006, 04:46 PM
#1
Thread Starter
Addicted Member
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:
Private DLFile As String
Private Sub Command1_Click()
Dim txtServer As String
Dim txtFile As String
txtFile = "/operational/pibs/pib1.shtml"
txtServer = "www.nats.co.uk"
Winsock1.RemoteHost = txtServer
Winsock1.RemotePort = 80
Winsock1.Connect
Do Until Winsock1.State = 7
DoEvents
Loop
Winsock1.SendData "GET " & txtFile & " HTTP/1.1" & vbCrLf
DoEvents
Winsock1.SendData "Host: " & txtServer & vbCrLf
DoEvents
Winsock1.SendData vbCrLf
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim NewData As String
Winsock1.GetData NewData
DLFile = DLFile & NewData
Debug.Print DLFile
End Sub
Last edited by Granger9; Mar 20th, 2006 at 04:50 PM.
-
Mar 20th, 2006, 04:53 PM
#2
Re: Winsock question
That loop will be active until Winsock finally connects (State = 7 is sckConnected) so if "it gets stuck" then it cannot connect for some reason.
-
Mar 20th, 2006, 04:54 PM
#3
Thread Starter
Addicted Member
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?
-
Mar 20th, 2006, 05:20 PM
#4
Thread Starter
Addicted Member
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?
-
Mar 20th, 2006, 05:56 PM
#5
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:
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:
'winsock error event
MsgBox "Error: " & Description
Winsock1.Close
There is also a close event (this fires when the remote host terminates the connection), where you should have
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
-
Mar 20th, 2006, 05:58 PM
#6
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:
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
-
Mar 20th, 2006, 06:09 PM
#7
Thread Starter
Addicted Member
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:
Private DLFile As String
Private Sub Command1_Click()
Dim txtServer As String
Dim txtFile As String
txtFile = "/operational/pibs/pib1.shtml"
txtServer = "nats.co.uk"
'Winsock1.Connect txtHost.Text, 80
Winsock1.Connect txtServer, 80
End Sub
Private Sub Winsock1_Connect()
Winsock1.SendData "GET " & txtFile & " HTTP/1.1" & vbCrLf
DoEvents
Winsock1.SendData "Host: " & txtServer & vbCrLf
DoEvents
Winsock1.SendData vbCrLf
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim NewData As String
Winsock1.GetData NewData
DLFile = DLFile & NewData
Debug.Print DLFile
End Sub
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)
'winsock error event
MsgBox "Error: " & Description
Winsock1.Close
End Sub
-
Mar 20th, 2006, 06:10 PM
#8
Thread Starter
Addicted Member
Re: Winsock question
Im extreamly new to all of this winsock stuff, how would i send it as one packet
-
Mar 20th, 2006, 06:20 PM
#9
Re: Winsock question
change to
VB Code:
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
-
Mar 20th, 2006, 06:31 PM
#10
Thread Starter
Addicted Member
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:
Private DLFile As String
Private Sub Command1_Click()
Dim txtServer As String
Dim txtFile As String
txtFile = "/operational/pibs/pib1.shtml"
txtServer = "nats.co.uk"
'Winsock1.Connect txtHost.Text, 80
Winsock1.Connect txtServer, 80
End Sub
Private Sub Winsock1_Connect()
Winsock1.SendData "GET " & txtFile & " HTTP/1.1" & vbCrLf & "Host: " & txtServer & vbCrLf & vbCrLf
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim NewData As String
Winsock1.GetData NewData
DLFile = DLFile & NewData
Debug.Print DLFile
End Sub
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)
'winsock error event
MsgBox "Error: " & Description
Winsock1.Close
End Sub
-
Mar 20th, 2006, 06:38 PM
#11
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
-
Mar 20th, 2006, 06:42 PM
#12
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
-
Mar 20th, 2006, 06:50 PM
#13
Thread Starter
Addicted Member
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)
-
Mar 20th, 2006, 06:57 PM
#14
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
-
Mar 20th, 2006, 07:09 PM
#15
Thread Starter
Addicted Member
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:
Private DLFile As String
Private Sub Command1_Click()
Dim txtServer As String
Dim txtFile As String
txtFile = "/operational/pibs/pib1.shtml"
txtServer = "www.nats.co.uk"
'Winsock1.Connect txtHost.Text, 80
Winsock1.Connect txtServer, 80
DLFile = Mid(DLFile, InStr(DLFile, vbCrLf & vbCrLf) + 1)
MsgBox "WOOP WOOP"
Debug.Print DLFile
End Sub
Private Sub Winsock1_Connect()
Winsock1.SendData "GET " & txtFile & " HTTP/1.1" & vbCrLf & "Host: " & txtServer & vbCrLf & vbCrLf
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim NewData As String
Winsock1.GetData NewData
DLFile = DLFile & NewData
End Sub
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)
'winsock error event
MsgBox "Error: " & Description
Winsock1.Close
End Sub
-
Mar 20th, 2006, 07:29 PM
#16
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:
DLFile = Mid(DLFile, InStr(DLFile, vbCrLf & vbCrLf) + 1)
MsgBox "WOOP WOOP"
Debug.Print DLFile
-
Mar 20th, 2006, 07:45 PM
#17
Thread Starter
Addicted Member
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
-
Mar 20th, 2006, 08:09 PM
#18
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
-
Mar 21st, 2006, 06:17 AM
#19
Frenzied Member
Re: Winsock question
Try changing txtServer = "www.nats.co.uk" to txtServer = "http://www.nats.co.uk"
Sometimes they like the full path.
-
Mar 21st, 2006, 01:31 PM
#20
Thread Starter
Addicted Member
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:
Public EntireFile As String
Private Sub Command1_Click()
EntireFile = ""
Winsock1.Connect "www.nats.co.uk", 80
Label1.Caption = "connecting..."
End Sub
Private Sub Winsock1_Close()
'complete
Label1.Caption = "complete"
Winsock1.Close
EntireFile = Mid(EntireFile, InStr(EntireFile, "<html>"))
text1.Text = EntireFile
End Sub
Private Sub Winsock1_Connect()
Label1.Caption = "connected, sending data...."
Winsock1.SendData "GET /operational/pibs/pib1.shtml HTTP/1.1" & vbCrLf & "Host: www.nats.co.uk" & vbCrLf & "Connection: Close" & vbCrLf & vbCrLf
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim NewData As String
Winsock1.GetData NewData
Label1.Caption = "receiving data..."
Debug.Print NewData
EntireFile = EntireFile & NewData
End Sub
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)
Label1.Caption = Description
Winsock1.Close
End Sub
-
Mar 21st, 2006, 09:13 PM
#21
Frenzied Member
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.
-
Mar 22nd, 2006, 02:39 PM
#22
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|