Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everone

I am having some problems with finding a code
My situation is we have lan partys and have a couple of servers up and running eg. Call of duty 4
Now i want to create a simple program that checks if my server is running on port 28960 if its online then the picturebox turns green and not then red and also i will have a textbox to set the interval to check

So how do i query a port to check if its online or not?

Regards
Posted

You don't. You send a query to the server and the server has to respond. If the server does not have any method you can call to get a response that denotes that it is running, you have to consult the documentation on the server you're running to see if there is any other method you can use to "ping" the server.
 
Share this answer
 
You can't ping a port because the ping operation does not operate at port level. You can however check to see if it is in use on the local computer or if it is open on the remote computer by means of connecting to the port using either TCP or UDP.

See if remote port is open:
VB
Function CheckPortOpen(ByVal hostname As String, ByVal portnum As Integer) As Boolean

    Dim ipAddr As IPAddress = CType(Dns.GetHostAddresses(hostname)(0), IPAddress)

    Try
        Dim sock As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        Console.WriteLine("Testing " & hostname & ":" & portnum)
        'Open connection to host
        sock.Connect(ipAddr, portnum)
        If (sock.Connected = True) Then
            sock.Close()
            sock = Nothing
            Return True
       End If

    Catch sex As SocketException
        If sex.ErrorCode = 10061 Then
            Return False
        Else
            Return Nothing
        End If
    End Try
End Function
 
Share this answer
 
Comments
Dave Kreskowiak 13-Jul-14 20:12pm    
The problem with this is the assumption that something is actually listening on the other end. The port may be available for connections but there's no universal way to tell if the server is responding on it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900