【发布时间】:2015-04-27 10:06:51
【问题描述】:
我正在尝试基于 this question 创建 UDP 端口检查器,但是在端口范围 1-100 上测试(例如)www.google.com 时,我收到每个端口的异常“连接尝试失败,因为连接方一段时间后没有正确响应,或者连接的主机没有响应,建立连接失败”。下面是我测试端口的代码:
try {
UdpClient udpClient = new UdpClient(Port);
Socket uSocket = udpClient.Client;
uSocket.ReceiveTimeout = 5000;
uSocket.Connect(Adress, Port);
udpClient.Connect(Adress, Port);
IPEndPoint RemoteIpEndPoint = new IPEndPoint(Adress, Port);
Byte[] sendBytes = Encoding.ASCII.GetBytes("?");
udpClient.Send(sendBytes, sendBytes.Length);
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
} catch (SocketException e) {
if (e.ErrorCode == 10054) {
return false;
} else {
return false;
}
}
return true;
【问题讨论】:
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。