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


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

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

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


加好友 发短信
等级:三尾狐 帖子:603 积分:4090 威望:0 精华:0 注册:2019/11/14 23:06:00
如何判断端口被占用?  发帖心情 Post By:2022/2/23 15:55:00 [只看该作者]

比如想在开启前判断8080端口是否被占用
[此贴子已经被作者于2022/2/23 16:25:41编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
BG小白
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:三尾狐 帖子:603 积分:4090 威望:0 精华:0 注册:2019/11/14 23:06:00
  发帖心情 Post By:2022/2/23 15:57:00 [只看该作者]

。。
[此贴子已经被作者于2022/2/23 16:25:54编辑过]

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


加好友 发短信
等级:超级版主 帖子:107147 积分:544978 威望: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

 回到顶部
帅哥哟,离线,有人找我吗?
tld
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:三尾狐 帖子:729 积分:7556 威望:0 精华:0 注册:2017/2/3 10:29:00
  发帖心情 Post By:2023/5/6 14:31:00 [只看该作者]

。。
[此贴子已经被作者于2023/5/6 14:31:05编辑过]

 回到顶部