Foxtable(狐表)用户栏目专家坐堂 → 如何判断端口被占用?


  共有5831人关注过本帖树形打印复制链接

主题:如何判断端口被占用?

帅哥哟,离线,有人找我吗?
有点蓝
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110574 积分:562760 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2022/2/23 16:53:00 [显示全部帖子]

需要添加引用:system.dll

Dim t As String = "TCP" '协议
Dim port As Integer = 52177 '需要判断的端口
Dim flag As Boolean = False
Dim properties As System.Net.NetworkInformation.IPGlobalProperties =  System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties()
Dim ipendpoints() As System.Net.IPEndPoint

If t = "TCP" Then
    ipendpoints = properties.GetActiveTcpListeners()
Else
    ipendpoints = properties.GetActiveUdpListeners()
End If

For Each ipendpoint As System.Net.IPEndPoint In ipendpoints
    
    If ipendpoint.Port = port Then
        flag = True
        Exit For
    End If
Next

ipendpoints = Nothing
properties = Nothing
if flag then
msgbox( "端口使用中")
endif

 回到顶部