Check whether a port is open on a remote machine

 

Sometimes you would like to know whether a port is open on a remote machine. Now while this is no nmap, the following code should give you an idea on how to check whether a port is open.

Private Function isRemotePortOpen(ByVal hostName As String, ByVal port As Integer) As Boolean
    Dim client As TcpClient = Nothing
    Dim rtn as Boolean
    Try
        client = New TcpClient(Host, PortNumber)
        rtn = True
    Catch ex As SocketException
        rtn = False
    Finally
        If Not client Is Nothing Then
            client.Close()
        End If
    End Try
    Return rtn
End Function