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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With