Function that gives delay in milliseconds from you to an IP address+Port

Untamed

Well-known member
Joined
Jul 25, 2009
Messages
53
Programming Experience
5-10
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.

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")
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.
 
Last edited:
If connection is successful you must close the socket and client afterwards.
VB.NET:
ts.GetStream.Close()
ts.Close()
 
If connection is successful you must close the socket and client afterwards.

Kk, thanks for the move [couldn't find correct forum for this topic.]
Also, I have only been using VB.Net for about 1 week or so, so I am new to all this coding. Anyways, thanks for the info... code has been edited to close the TcpClient.
 
Untamed said:
VB.NET:
        Return FullCheck
        ts.close()
    End Function
The function returns at Return statement, so the ts.Close is never called.

I added a code sample to post 2 that shows how to close the socket and client. This according to the documentation for the TcpClient.Close method.
 
could he just call the ts.close and not even worry about the ts.getstream.close
Compare what documentation says for TcpClient.Close Method for .Net 2.0 and 3.5. Note that the BCL that system.dll belongs to was not changed from 2.0 to 3.5, it is the same library.
TcpClient.Close Method (System.Net.Sockets) 3.5
TcpClient.Close Method (System.Net.Sockets) 2.0
As you can see closing the client will eventually cause the socket to drop also, but this can take some time. Calling close on the client does not call close on the socket, it just marks it ready for disposal. I have confirmed this behaviour in previous socket projects, where I had to use the same socket (port) again right away I also has to close it explicitly before it came available.
 
Back
Top