Skip to content

Instantly share code, notes, and snippets.

@elpatron68
Created July 10, 2015 12:25
Show Gist options
  • Save elpatron68/257e4e2531fdb8729874 to your computer and use it in GitHub Desktop.
Save elpatron68/257e4e2531fdb8729874 to your computer and use it in GitHub Desktop.
VB.NET: Check if a TCP Port on a remote site answers requests
Imports System.Net.Sockets
Private Function _CheckTCPPort(ByVal sIPAdress As String, ByVal iPort As Integer, Optional ByVal iTimeout As Integer = 5000)
Dim socket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
' Connect using a timeout (default 5 seconds)
Dim result As IAsyncResult = socket.BeginConnect(sIPAdress, iPort, Nothing, Nothing)
Dim success As Boolean = result.AsyncWaitHandle.WaitOne(iTimeout, True)
If Not success Then
' NOTE, MUST CLOSE THE SOCKET
socket.Close()
Return False
End If
Return True
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment