port scanner

soelknight

Member
Joined
Jul 6, 2007
Messages
8
Programming Experience
Beginner
So what about checking the status of ports on these hosts. I was attempting to use the Net.Sockets.TcpClient connect and got it to work but only in a blocking type fashion instead of an async fashion is there a way to get that to go async as well?
 
Thread splitted, different topics.

TcpClient have the BeginConnect/EndConnect async methods.
 
Thanks for the split I wasn't positive if I should or not. So do I handle the return events the same way as I would an async ping? Because I just want to know if it is up and running on the device not neccessarily envoke an entire session with the device. I hope that makes sense.
 
Not exactly, Ping class is a component that manages the async call interally and posts a nice UI event when finished, the TcpClient async call works like a regular async callback (delegate.BeginInvoke) which means you have to "EndInvoke" yourself. This also means the callback runs on the async thread and not UI thread, which will give you cross-thread issues if not dealing with it. All standard multi-threading. The documentation doesn't have VB.Net examples but they pretty much translates to this:
VB.NET:
    Sub connectasync()
        Dim t As New Net.Sockets.TcpClient
        t.BeginConnect("host", port, AddressOf connectcallback, t)
    End Sub

    Sub connectcallback(ByVal ar As IAsyncResult)
        Dim t As Net.Sockets.TcpClient = ar.AsyncState
        t.EndConnect(ar)
    End Sub
The EndConnect raises an exception if connection wasn't possible.
Here's an example that display the thread-safe pattern: http://www.vbdotnetforums.com/showpost.php?p=60157&postcount=2
You could also use the AsyncOperation and AsyncOperationManager from System.ComponentModel namespace to synchronize messages between threads.
 
Back
Top