Checking for Server

Thonord

Member
Joined
Oct 25, 2012
Messages
23
Programming Experience
10+
I'm trying to check if a server is present on the network
I thought the client could create a non blocking socket, connect to it within a Try Catch and either get a response or an exception.

The code below demonstrates my thoughts. For testing purposes I use a "Hard Wired" IpAddy where I know there is a server, its running and it works.

But! I only get an exception. I never get a True!

VB.NET:
Private Function CheckServer(ByVal MyServer As String, ByVal MyPort As Integer) As Boolean


        Dim Address As IPAddress = IPAddress.Parse("10.0.0.53")
        Dim EndPoint As New IPEndPoint(Address, 5066)
        Dim TestSocket As New Socket(EndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)


        TestSocket.Blocking = False


        Try
            TestSocket.Connect(EndPoint)
            Return True
        Catch ex As Exception
            Return False
        End Try


    End Function

Why does not my code return True when the is a running server at 10.0.0.53:5066 ?

Tom
 
Specify a value for AddressFamily. Also remember to close your connection after you have opened it.
 
I'd like to continue this thread, but rephrase it: "Checking for a Client" and changing the timeout.

If I:

Dim client As New TcpClient("10.0.0.19", 5067)

and there is nobody with that IpAddy on my local network, the Dim timeouts after 20 sec.

I want to set a shorter Timeout.

Is that possible?
 
Thank you and after using a free c# - Vb.net converter I am trying the code, but it don't seem to work for me. It throws an exception whether the client is connected or not - eh I think. I really don't understand the code.
But I can set the time delay before it does what it does to the thread
.
I'm gonna try something else.
Worst case, I'll revert to VB6, use good old Socketwrench from Catalyst and make me a dll. What Socketwrench cant do to a socket - I have never needed to do.
Tom
 
Last edited:
I really don't understand the code.
It is simple code really, it calls BeginConnect which is non-blocking async call, it then uses its AsyncWaitHandle to wait for specified timeout - or that socket signals connected.
WaitOne returns True if socket has been connected before timeout, in which case code completes async call with EndConnect. If timeout occurs code cancels connection by calling Close then throws exception.
Convert C# to VB.NET - A free code conversion tool - developer Fusion converted the code ok.
 
Back
Top