如何测试远程 UDP 端口 [英] How to test a remote UDP Port
问题描述
我正在寻找一种简单的方法来查看服务器上是否打开了远程 UDP 端口
I am looking for a simple way to see if a remote UDP port is open on a server
推荐答案
注意几个防火墙/网络设置/IDS 等可能会影响结果...以下方法不是 100% 可靠,但它是UDP 恕我直言唯一可行的方法(因为 UDP 是无连接的).
您可以使用 UdpClient
, 在底层"http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.client.aspx">socket, 建立连接到那个远程服务器/端口,发送
一些小消息(byte[]
!)并调用接收
.
You can use UdpClient
, set a receive timeout on the underlying socket, make a connection to that remote server/port, Send
some small message (byte[]
!) and call Receive
.
如果端口关闭,您会收到一个异常,说明连接被强制关闭(SocketException
with ErrorCode
10054 = WSAECONNRESET
)... 表示端口未打开.
IF the port is closed you get an exception saying that the connection was forcibly closed (SocketException
with ErrorCode
10054 = WSAECONNRESET
)... which means the port is NOT open.
否则,您要么收到答复,要么收到超时异常 - 两者都应解释为UDP 端口已打开".
Otherwise you either receive an answer OR a timeout exception - both should be interpreted as "the UDP port is open".
这篇关于如何测试远程 UDP 端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!