检查是否使用了TCP端口号 [英] Check if TCP port number is used

查看:94
本文介绍了检查是否使用了TCP端口号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个数据库应用程序,其中服务器需要为每个新的数据库文件动态分配一个未使用的TCP端口号。如果它选择一个特定的端口号(例如7890),它如何使用.NET Framework检查该端口是否已在该计算机上使用?

I am working on a database application in which the server needs to dynamically allocate an unused TCP port number to each new database file. If it chooses a particular port number (such as 7890), how can it use the .NET Framework to check if that port is already in use on that computer?

推荐答案

试试这个代码


Try this code



























< td> 结束 尝试
尝试
Dim soc As Socket = System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork,System.Net.Sockets.SocketType.Stream,System.Net。 Sockets.ProtocolType.Tcp)
soc.Connect(IPAddress.Parse( " 127.0.0.1" ),1001)
如果 soc。已连接= True 然后
MessageBox.Show( " Port is Closed"
结束 如果
Catch ex As SocketException
如果 ex.ErrorCode = 10061 然后
MessageBox.Show( "端口打开"
结束 < font style ="color:blue">如果
       Try 
            Dim soc As Socket = New System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp)  
            soc.Connect(IPAddress.Parse("127.0.0.1"), 1001)  
            If soc.Connected = True Then 
                MessageBox.Show("Port is Closed")  
            End If 
        Catch ex As SocketException  
            If ex.ErrorCode = 10061 Then 
                MessageBox.Show("Port is Open")  
            End If 
        End Try 


这篇关于检查是否使用了TCP端口号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆