ICMP套接字总是超时

发布于 2024-12-27 03:52:37 字数 1420 浏览 1 评论 0原文

我正在尝试用 C# 向我的路由器发送 ICMP AddressMask 请求。但是,我的套接字总是超时,或者,如果未设置超时,则会导致应用程序无限期循环。 这是代码:

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
socket.Bind(new IPEndPoint(interfaceAddress, 0));

Random rnd = new Random();
UInt16 seed, sn;

seed = (UInt16)rnd.Next();
sn = (UInt16)rnd.Next();

var icmpAddressMaskRequest = new Byte[12];

icmpAddressMaskRequest[0] = 17;
icmpAddressMaskRequest[4] = (Byte)(seed >> 8);
icmpAddressMaskRequest[5] = (Byte)(seed & 0xff);
icmpAddressMaskRequest[6] = (Byte)(sn >> 8);
icmpAddressMaskRequest[7] = (Byte)(sn & 0xff);

UInt16 checksum = 0;
for (int i = 0; i < 6; i++)
{
    Int32 twoOctects = icmpAddressMaskRequest[2 * i] * 256 + icmpAddressMaskRequest[2 * i + 1];
    Int32 tempChecksum = checksum + twoOctects;

    if (tempChecksum > UInt16.MaxValue)
        checksum = (UInt16)((tempChecksum & 0xffff) + 1);
    else
        checksum = (UInt16)tempChecksum;
}

icmpAddressMaskRequest[2] = (Byte)(255 - checksum >> 8);
icmpAddressMaskRequest[3] = (Byte)(255 - checksum & 0xff);

EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse("192.168.100.1"), 0);

socket.ReceiveTimeout = 2000;
socket.SendTo(icmpAddressMaskRequest, remoteEndPoint);

var buffer = new Byte[4096];
socket.ReceiveFrom(buffer, ref remoteEndPoint);

我知道许多类型的路由器不会响应地址掩码请求,但我尝试使用简单的回显请求,结果是相同的。

I am trying to send an ICMP AddressMask request to my router in C#. However, my socket always time out, or, if the timeout isn't set, makes the application loop indefinitely.
Here is the code:

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
socket.Bind(new IPEndPoint(interfaceAddress, 0));

Random rnd = new Random();
UInt16 seed, sn;

seed = (UInt16)rnd.Next();
sn = (UInt16)rnd.Next();

var icmpAddressMaskRequest = new Byte[12];

icmpAddressMaskRequest[0] = 17;
icmpAddressMaskRequest[4] = (Byte)(seed >> 8);
icmpAddressMaskRequest[5] = (Byte)(seed & 0xff);
icmpAddressMaskRequest[6] = (Byte)(sn >> 8);
icmpAddressMaskRequest[7] = (Byte)(sn & 0xff);

UInt16 checksum = 0;
for (int i = 0; i < 6; i++)
{
    Int32 twoOctects = icmpAddressMaskRequest[2 * i] * 256 + icmpAddressMaskRequest[2 * i + 1];
    Int32 tempChecksum = checksum + twoOctects;

    if (tempChecksum > UInt16.MaxValue)
        checksum = (UInt16)((tempChecksum & 0xffff) + 1);
    else
        checksum = (UInt16)tempChecksum;
}

icmpAddressMaskRequest[2] = (Byte)(255 - checksum >> 8);
icmpAddressMaskRequest[3] = (Byte)(255 - checksum & 0xff);

EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse("192.168.100.1"), 0);

socket.ReceiveTimeout = 2000;
socket.SendTo(icmpAddressMaskRequest, remoteEndPoint);

var buffer = new Byte[4096];
socket.ReceiveFrom(buffer, ref remoteEndPoint);

I know that many types of router do not respond to an address mask request, but I tried even with a simple Echo-Request and the result was the same.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林散布 2025-01-03 03:52:37

您应该首先下载 Wireshark,这将允许您查看是否确实正在发送响应(以及是否正在发送您的请求)。

关于 ping 没有响应的事实,其他人也有同样的问题。

该代码特别可能存在 C# 处理按位运算方式的问题,您必须非常小心,因为 Byte 始终是带符号的,并且经常升级为 int,这会导致各种意外结果。

You should start with downloading Wireshark, this will allow you to see whether a response is actually being send (and whether your request is being send).

With regards to the fact that a ping does not respond, somebody else had the same problem.

This code specifically might be having issues with the way C# handles bitwise operations, you have to be very carefull there because a Byte is always signed and often upgraded to an int which causes all sorts of unexpected results.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文