I had a function before that could ping an IP address or host name and get me the delay in milliseconds. But that was only for port 80 to my understanding of pings. I needed the function to work on a different, more used port. Since you can't ping a port, I designed a little function that worked like a ping to get millisecond delay from you to any port on any ip address.
What this does is get the current time of day and store it in the variable StartCheck, then checks a port on an ip address and uses TcpClient to connect to that port. After the connection is finished, it immediately gets the time of day again and stores it in another variable, EndCheck. It then Subtracts the startcheck from the endcheck, thus getting the elapsed time. Then FullCheck gets an integer using 'Check.TotalMilliseconds' command, which will get the amount of milliseconds elapsed. It then returns that integer.
To call this function, just use
Replace 127.0.0.1 with the IP you want to use, and 80 with the port you want to use.
It will then return the delay in milliseconds to that port.
VB.NET:
Public Shared Function GetPortMs(ByRef IP As String, ByVal Port As Integer)
Dim StartCheck As DateTime = DateTime.Now()
Dim ts As System.Net.Sockets.TcpClient = New System.Net.Sockets.TcpClient(IP, Port)
Dim EndCheck As DateTime = DateTime.Now()
Dim Check As TimeSpan = EndCheck - StartCheck
Dim FullCheck = Check.TotalMilliseconds
ts.GetStream.Close()
ts.Close()
Return FullCheck
End Function
What this does is get the current time of day and store it in the variable StartCheck, then checks a port on an ip address and uses TcpClient to connect to that port. After the connection is finished, it immediately gets the time of day again and stores it in another variable, EndCheck. It then Subtracts the startcheck from the endcheck, thus getting the elapsed time. Then FullCheck gets an integer using 'Check.TotalMilliseconds' command, which will get the amount of milliseconds elapsed. It then returns that integer.
To call this function, just use
VB.NET:
GetPortMs("127.0.0.1", "80")
It will then return the delay in milliseconds to that port.
Last edited: