Foxtable(狐表)用户栏目专家坐堂 → 如何获取已占用的端口号?


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

主题:如何获取已占用的端口号?

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


加好友 发短信
等级:四尾狐 帖子:989 积分:8827 威望:0 精华:0 注册:2016/4/10 14:33:00
如何获取已占用的端口号?  发帖心情 Post By:2023/9/27 7:37:00 [只看该作者]

如题:如何获取已占用的端口号?

 回到顶部
帅哥,在线噢!
有点蓝
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110574 积分:562760 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2023/9/27 9:46:00 [只看该作者]

全局代码

Public Class TestPost
    
    Const PROCESS_ALL_ACCESS As Int32 = &H1F0FFF
    Const PROCESS_QUERY_INFORMATION As Int32 = &H00000400
    Const PROCESS_VM_READ As Int32 = &H0010
    Const PROCESS_VM_WRITE As Int32 = &H0020

    Public Declare Function QueryFullProcessImageNameW Lib "Kernel32.dll"(ByVal hProcess As Int32, ByVal flags As UInt32, ByVal nameList As Char(), ByRef nameLen As UInt32) As Int32

        Public Declare Function OpenProcess Lib "Kernel32.dll"(ByVal DesiredAccess As Int32, ByVal bInheritHandle As Boolean, ByVal ProcessId As Int32) As Int32

            Public Declare Function CloseHandle Lib "Kernel32.dll"(ByVal hObject As Int32) As Boolean

                Public Declare Function GetLastError Lib "Kernel32.dll"() As Int32
                    
                    
                    Public Function GetInfo() As List(Of String)
                        Dim infoList As New List(Of String)
                        Dim procInfo As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo()
                        Dim pro As System.Diagnostics.Process = New System.Diagnostics.Process()
                        pro.StartInfo.FileName = "cmd"
                        pro.StartInfo.UseShellExecute = False
                        pro.StartInfo.RedirectStandardInput = True
                        pro.StartInfo.RedirectStandardOutput = True
                        pro.StartInfo.RedirectStandardError = True
                        pro.StartInfo.CreateNoWindow = True
                        pro.Start()
                        pro.StandardInput.WriteLine("netstat -anob")
                        pro.StandardInput.WriteLine("exit")
                        Dim reg As New System.Text.RegularExpressions.Regex("\s+", RegexOptions.Compiled)
                        Dim line As String = Nothing
                        infoList.Clear()
                        
                        While (CSharpImpl.__Assign(line, pro.StandardOutput.ReadLine())) IsNot Nothing
                            line = line.Trim()
                            
                            If line.StartsWith("TCP", StringComparison.OrdinalIgnoreCase) Then
                                line = reg.Replace(line, ",")
                                infoList.Add(line)
                            End If
                            
                            If line.StartsWith("UDP", StringComparison.OrdinalIgnoreCase) Then
                                line = reg.Replace(line, ",")
                                infoList.Add(line)
                            End If
                        End While
                        
                        pro.Close()
                        
                        Return infoList
                    End Function
                    
                    Private Class CSharpImpl
                        < Obsolete("Please refactor calling code to use normal Visual Basic assignment") > 
                        Shared Function __Assign(Of T)(ByRef target As T, value As T) As T
                            target = value
                            Return value
                        End Function
                    End Class
                End Class


命令窗口测试

Dim a As New TestPost

Dim lst As List(Of String) = a.GetInfo()

For Each s As String In lst
    Output.Show(s)
Next

 回到顶部