如何知道winsock端口是否开放

时间:2014-04-01 13:33:02

标签: vb.net

我想用winsock创建一个应用服务器。 我不知道如何确定端口是否打开

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

1 个答案:

答案 0 :(得分:1)

如果感兴趣,这是一个很好的网站,可以帮助您开始使用VB.NET进行Socket编程: http://vb.net-informations.com/communications/vb.net_socket_programming.htm

要检查端口是否已打开,您可以尝试连接到该端口,如果它抛出SocketException,您可以认为该端口已关闭。

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