martes, 21 de febrero de 2017

Clase Visual Basic .NET comprueba si un puerto TCP está abierto

Public Class clsHostPort
    Private m_hostname As String
    Private m_port As Integer
    Private m_timeout As Integer
    Private m_bIsOpen As Boolean

    Sub New(ByVal HostName As String, ByVal Port As Integer, Optional ByVal timeout As Integer = 10)
        '127.0.0.1 is the local machine
        m_hostname = HostName
        m_port = Port
        m_timeout = timeout
        m_bIsOpen = False
    End Sub

    Public Function IsPortOpen() As Boolean
        'first check valid port value
        If m_port >= 0 And m_port < 65536 Then
        Else
            Return False
        End If

        Dim Client As System.Net.Sockets.TcpClient = Nothing
        Try
            Client = New System.Net.Sockets.TcpClient(m_hostname, m_port)
            Client.ReceiveTimeout = m_timeout
            m_bIsOpen = True
        Catch ex As System.Net.Sockets.SocketException
            Console.WriteLine(ex.Message)
            m_bIsOpen = False
        Finally
            If Not Client Is Nothing Then
                Client.Close()
            End If
            IsPortOpen = m_bIsOpen
        End Try
    End Function

End Class

No hay comentarios:

Publicar un comentario

VBA Access. Redondeo de números decimales con el método medio redondeo. Alternativa a la función Round (bankers round)

 Private Function Redondeo(ByVal Numero As Variant, ByVal Decimales As Integer) As Double     'Aplica método medio redondeo (half round ...