Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Port to Ping

Tags:

vb.net

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?

like image 726
developer__c Avatar asked Dec 22 '22 23:12

developer__c


2 Answers

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).

like image 171
Martin Liversage Avatar answered Jan 08 '23 06:01

Martin Liversage


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
like image 31
Khalid Rahaman Avatar answered Jan 08 '23 08:01

Khalid Rahaman