I'm busy writing an application for work which needs to connect to a list of our servers and check some ports to ensure the correct ones we require are open and ones we don't use are closed.

I'm using winsock and checking for a connection and the results are being written out to a csv file so we can then filter various settings.

if I use a single ip i am fine the program works without a problem, the issues arise when I incorperate a loop to cycle through and only if i use them same sockets repeatedly.

ie: I'm looking at 10 ports on 50 servers

i've set my portlist up and loadup the sockets, then assign the relevant values to winsock then connect. If I connect or not I then close the socket so I can then use on next ip. But it only seems to pull through the last ip in the list, and not the others.

How can I delay the loop long enough for it to do each ip correctly, instead of skipping them

I don't use winsock. It's functionality is limited in the realm of sockets, and to make matters worse, I find the code you need to work with it... well, leaves a lot to be desired. I prefer the use of a control that is free from http://www.catalyst.com, known as socketwrench. It makes working with sockets 100 times easier, and it gives a whole list of properties and methods that make winsock look like a joke. Anyhow, I have written a port-scanner in VB6, and you can download the source from this thread:

Let me know if this helps! If you really MUST use winsock, maybe the concept behind how my port scanner runs will help give you the same idea to use with your winsock app.


looked at TTPScan and gets error loading cswsk32.ocx on loading into vb.

I still loaded at had a look, seems to be doing exactly same as I'm trying, but if I add under

/* If We Are NOT Scanning Just Well Known Hosts */

the following

For myloop = 1 To ScannedList.Count

Text1.Text = ScannedList.Item(myloop)

and place a Next before the End Sub, I get exactly the same error I get in my code

Run-time error '40020':
Invalid operation at current state

If I close the port after connecting or not then when goes to next ip in list, it doesn't display info for the first ip.

As we're going to put this on one of servers, which we use to update our customers with, I need it to go through a list, of ip's and connect to each, check 10 ports, and then create a report to be sent to the managers listing any problems found.

looked at TTPScan and gets error loading cswsk32.ocx on loading into vb.

You don't need it.... it uses winsock because it's on everyone's computer, the other is there just for the ping sweeper, and it's not needed for the port scanner.

I'll be willing to bet, the error that you are recieving however, is because the socket is still connected. With winsock, you have to use .state (socketwrench has a .connected property) to test if the socket is connected before trying to reconnect. So, you do a loop with a doevents in the middle, and wait for the socket's state to become closed. Another thing to take into consideration, is maybe instead of a for loop, try using a variable that keeps track of which server you are scanning, and when the list of scanned ports completes, if it's the last port to scan for that IP, on the socket's error or connect event, call a socket close (disconnect), and start on the next IP.
The problem with the for loop, is that it doesn't wait. It loops each iteration until it's done, and the only way to get it to stop is to break out of it or use a sleep. The sleep will pause your entire app though, not just the loop. So, we need to look at working with event driven looping. On Error, On Connect, On Close, etc. The socket is a control array, so it's not that difficult to check if we are on a our final port by using the passed "index". Also, I don't know if it makes any difference, but you start your for loop at 1, and i'm wondering if the first IP is at 0?

thanks for the info I will give that a try and let you know how it works out

i had set the loop count to 1 just for routine for testing as noticed the combobox list actually pulls the scanlist.txt file in at start index of 1 and not 0.

I did also notice that unlike delphi you can't use combobox1.clear which resets a combobox and then allows you to load data starting ar point 0.

Ah! Very good catch. Let me know how the project turns out, or if the methods that I suggested above worked... It'll be fancy to hear what happens. Also, If you encounter any other problems with it, don't hesitate to ask.

Hi Comatose,

Sorry for delay in responding to this but been very busy.

But first thanks for the help :cool:

Well the news is I've managed to do what I need it to do.

I've used the socketwrench control due to the timeout parameter.

I store a list of ports to be scanned in an ini file which is loaded everytime the program is run. You can add or remove ports and this will also change the listview box settings.

It then asks for a file with the list of ip's to scan, and loads those into an array. Then goes about checking the ports required on each ip, and writes the results to a csv file, as well as displaying on the screen