nzxtnemesis
01-29-2010, 12:00 AM
This is a basic Winsock tutorial using Visual Basic 6. The example i will be using will be a proxy checker. You will learn how to look for good data in the Winsock return. Let begin.
What you will need.
Text box named txtData
Command Button (caption = TEST)
Text box named Port
Text box named Proxy
Winsock named S
Label named Status
First open visual basic and create a new .EXE project. Add the winsock component by hitting CTRL+T or Project>Components (Microsfot Winsock Control 6). This Winsock is found in the SYSTEM 32 folder of your C Drive. If you dont have the winsock ocx you can find it in the downloads-> dump section of this site( MSWINSCK.OCX). Double click or drag the winsock to your form. for the sake of the tutorial and making it easier rename the winsock1 to S. Now lets Code!
Double click on your command button and it will bring you into the code form of your project. In this button is where we will be starting the process of connecting the socket and testing the proxy to see if it is good.
This is what should be inside your button now. Tell the user that the socket is connecting and then connect the socket. I use Port and Proxy instead of Port.Text, Proxy.Text either way it doesnt matter. VB will see it.
Status.Caption = "Testing" ' tells the user that the proxy is being tested
S.Close ' closes the socket incase its open
S.RemoteHost = Proxy ' sets proxy-host
S.RemotePort = Port ' sets port
S.Connect ' connect the socket
Lets code the socket connect now. Go back to you design form and double click on your S icon and it will bring you back to the code form. Now you are in the form notice that you are in S_Error. You need to change this to S_Connect. You can change this by hitting the menu at the top right of the code form. Select Connect from that menu then you will be in S_Connect. This is where we will be coding our Packet to send the request. This is the hard part. You need to get the packet from a packet sniffer. For this tutorial i will be using Yahoo.com as are baseline tester to see if out proxy is good.
On Error Resume Next ' If there is an error it will goto the next procedure
Dim Data As String ' makes are Data a string
Status.Caption = "Connecting"
Data = "GET " & "http://www.yahoo.com" & " HTTP/1.0" & vbCrLf ' connects to yahoo.com, vbCrLf just means new line. you can also use vbNewLine
Data = Data & "Accept: */*" & vbCrLf ' tells the socket what to accept, some use */* to accept everything, some use text/html
Data = Data & "Accept-Language: en-us" & vbCrLf
Data = Data & "Host: www.yahoo.com" & vbCrLf ' our host. because we are using a proxy. our winsock has no site host
Data = Data & "Proxy-Connection: Close" & vbCrLf & vbCrLf
S.SendData Data ' send the above packet
ow this will send the packet data with our winsock. Lets get to the DataArrival of our winsock. In this function we will see if our proxy is good or bad. Make sure your mouse if clicked inside your Connect function. Go to the top right and where you selected CONNECT now select DataArrival. This is the heart of the socket. The do or die fuction.
Once our socket is connected it will send data back and we need to get that data in the form of TXT. when you run the program you will see this returned data in the txtData texbox. In DataArrival we will be looking for specific text in our return data. This specific text will tell us if your proxy is good or bad. The text we want to find is the good return of a page, 200 OK. We will use the INSTR function, Function InStr([Start], [String1], [String2], [Compare As VbCompareMethod = vbBinaryCompare])
On Error Resume Next
Dim tmp As String
Status.Caption = "retrieving data"
S.GetData tmp
txtData = tmp
If InStr(1, tmp, UCase$("200 OK")) Then
Status.Caption = "Good Proxy!"
MsgBox "GOOD PROXY", vbInformation ' this will pop up a box saying GOOD PROXY
S.Close ' close our socket were done
Else
Status.Caption = "Bad Proxy!"
MsgBox "BAD PROXY", vbCritical
S.Close ' close our socket were done
End If
Now that we have our return data if it found 200 OK in the data then our proxy was good and we can close our socket. If it didnt find 200 OK then its bad, close the socket. Else is a function that is like. well if its not this then its this. If the data is good then OK else, its bad. You could also yse, IfNot InStr(1, tmp, UCase$("200 OK")) Then but thats just too much work, the first IF took care of our data that we needed. And thats its. Test good proxies. If you want you can go back to the S_Error and put in
S.Close
Status.Caption = "Error " & Description
What you will need.
Text box named txtData
Command Button (caption = TEST)
Text box named Port
Text box named Proxy
Winsock named S
Label named Status
First open visual basic and create a new .EXE project. Add the winsock component by hitting CTRL+T or Project>Components (Microsfot Winsock Control 6). This Winsock is found in the SYSTEM 32 folder of your C Drive. If you dont have the winsock ocx you can find it in the downloads-> dump section of this site( MSWINSCK.OCX). Double click or drag the winsock to your form. for the sake of the tutorial and making it easier rename the winsock1 to S. Now lets Code!
Double click on your command button and it will bring you into the code form of your project. In this button is where we will be starting the process of connecting the socket and testing the proxy to see if it is good.
This is what should be inside your button now. Tell the user that the socket is connecting and then connect the socket. I use Port and Proxy instead of Port.Text, Proxy.Text either way it doesnt matter. VB will see it.
Status.Caption = "Testing" ' tells the user that the proxy is being tested
S.Close ' closes the socket incase its open
S.RemoteHost = Proxy ' sets proxy-host
S.RemotePort = Port ' sets port
S.Connect ' connect the socket
Lets code the socket connect now. Go back to you design form and double click on your S icon and it will bring you back to the code form. Now you are in the form notice that you are in S_Error. You need to change this to S_Connect. You can change this by hitting the menu at the top right of the code form. Select Connect from that menu then you will be in S_Connect. This is where we will be coding our Packet to send the request. This is the hard part. You need to get the packet from a packet sniffer. For this tutorial i will be using Yahoo.com as are baseline tester to see if out proxy is good.
On Error Resume Next ' If there is an error it will goto the next procedure
Dim Data As String ' makes are Data a string
Status.Caption = "Connecting"
Data = "GET " & "http://www.yahoo.com" & " HTTP/1.0" & vbCrLf ' connects to yahoo.com, vbCrLf just means new line. you can also use vbNewLine
Data = Data & "Accept: */*" & vbCrLf ' tells the socket what to accept, some use */* to accept everything, some use text/html
Data = Data & "Accept-Language: en-us" & vbCrLf
Data = Data & "Host: www.yahoo.com" & vbCrLf ' our host. because we are using a proxy. our winsock has no site host
Data = Data & "Proxy-Connection: Close" & vbCrLf & vbCrLf
S.SendData Data ' send the above packet
ow this will send the packet data with our winsock. Lets get to the DataArrival of our winsock. In this function we will see if our proxy is good or bad. Make sure your mouse if clicked inside your Connect function. Go to the top right and where you selected CONNECT now select DataArrival. This is the heart of the socket. The do or die fuction.
Once our socket is connected it will send data back and we need to get that data in the form of TXT. when you run the program you will see this returned data in the txtData texbox. In DataArrival we will be looking for specific text in our return data. This specific text will tell us if your proxy is good or bad. The text we want to find is the good return of a page, 200 OK. We will use the INSTR function, Function InStr([Start], [String1], [String2], [Compare As VbCompareMethod = vbBinaryCompare])
On Error Resume Next
Dim tmp As String
Status.Caption = "retrieving data"
S.GetData tmp
txtData = tmp
If InStr(1, tmp, UCase$("200 OK")) Then
Status.Caption = "Good Proxy!"
MsgBox "GOOD PROXY", vbInformation ' this will pop up a box saying GOOD PROXY
S.Close ' close our socket were done
Else
Status.Caption = "Bad Proxy!"
MsgBox "BAD PROXY", vbCritical
S.Close ' close our socket were done
End If
Now that we have our return data if it found 200 OK in the data then our proxy was good and we can close our socket. If it didnt find 200 OK then its bad, close the socket. Else is a function that is like. well if its not this then its this. If the data is good then OK else, its bad. You could also yse, IfNot InStr(1, tmp, UCase$("200 OK")) Then but thats just too much work, the first IF took care of our data that we needed. And thats its. Test good proxies. If you want you can go back to the S_Error and put in
S.Close
Status.Caption = "Error " & Description