Created
July 10, 2015 12:25
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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