开发者

Adding Port to Ping

I am using 开发者_开发百科VB.net to ping a domain/IP, using the following code;

My.Computer.Network.Ping(Address, 1000)

I now want to add a port to the domain/IP - e.g google.co.uk:21

How would I go about this?


You cannot ping a port but you can try to connect to a specific port using either TCP/IP or UDP. The port concept belongs to the transport layer of the protocol stack (either TCP or UDP) while the ping is at the lower network layer (the ICMP protocol).


You can test if a port is "open" by using this function:

Function CheckPortOpen(ByVal hostname As String, ByVal portnum As Integer) As Boolean

    Dim ipa 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)
        sock.Connect(ipa, portnum)
        If (sock.Connected = True) Then
            sock.Close()
            sock = Nothing
            Return True
       End If

    Catch sx As SocketException
        If sx.ErrorCode = 10061 Then
            Return False
        Else
            Return Nothing
        End If

    End Try

End Function


You don't, because that's not how ping works. There's no concept of a port in the ICMP protocol.

If you want to see if a port is open on a server, you must attempt to connect to it using TCP or UDP (depending on the protocol expected for that server). There's no other way to check for an open port.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜