joni
joni

Reputation: 43

How to know winsock port is open or not

I want to create an application server with winsock. and I do not know how to determine if a port is open or not

    With AxWinsock1
        .LocalPort = TextBox1.Text
        .Listen()
    End With

Upvotes: 2

Views: 988

Answers (1)

Dayan
Dayan

Reputation: 8031

Here is a good site to get you started on Socket programming with VB.NET if interested: http://vb.net-informations.com/communications/vb.net_socket_programming.htm

To check if a port is open, you can just attempt to connect to it and if it throws a SocketException you can assume the port is closed.

Dim host As String = "localhost"
Dim port As Integer = 23112

Dim addr As IPAddress = DirectCast(Dns.GetHostAddresses(host)(0), IPAddress)

Try   

    Dim tcpList As New TcpListener(addr, port)   
    tcpList.Start()

Catch sx As SocketException
  'Catch exception - No available port
End Try

Upvotes: 1

Related Questions