尝试将数据包单播到可用网络时出现问题

发布于 2024-07-26 15:09:18 字数 755 浏览 10 评论 0原文

尝试将数据包单播到可用网络。 总共有3个网络。 设法仅在一个网络中获取数据包。但我无法在不同网络中接收数据包。

使用这段代码..

        foreach (var i in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
        {
            foreach (var ua in i.GetIPProperties().UnicastAddresses)
            {
                System.Windows.Forms.MessageBox.Show(ua.Address.ToString());
                IPAddress Tip = IPAddress.Parse(ua.Address.ToString());
                IPEndPoint targetEndPoint = new IPEndPoint(Tip, iTargetPort);
                MyUdpClient sendUdpClient = new MyUdpClient();
                int numBytesSent = sendUdpClient.Send(CombineHeaderBody, CombineHeaderBody.Length, targetEndPoint);
            }
        }

有什么问题? 谢谢。

Trying to unicast packets to available networks. There are totally 3 networks. Managed to get packets in only one network.But i am not able to receive the packets in different networks.

using this code..

        foreach (var i in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
        {
            foreach (var ua in i.GetIPProperties().UnicastAddresses)
            {
                System.Windows.Forms.MessageBox.Show(ua.Address.ToString());
                IPAddress Tip = IPAddress.Parse(ua.Address.ToString());
                IPEndPoint targetEndPoint = new IPEndPoint(Tip, iTargetPort);
                MyUdpClient sendUdpClient = new MyUdpClient();
                int numBytesSent = sendUdpClient.Send(CombineHeaderBody, CombineHeaderBody.Length, targetEndPoint);
            }
        }

What is the prob ? Thanks.

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

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

发布评论

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

评论(3

扮仙女 2024-08-02 15:09:18

我认为您正在尝试向自己发送数据包?

您确定没有混淆单播和多播地址吗?

好吧,所以你不是在尝试多播...

你的每个网络接口都有一个 IP 地址。 您在这里所做的是将数据包发送到您的网卡。 这实际上并不是网络问题,因为您的计算机很可能知道自己的 IP 地址并将其重新路由到 127.0.0.1

I think that you're trying to send packets to yourself?

Are you sure you're not confusing unicast and multicast addresses?

Ok so you're not trying to multicast...

Each of your network interface has an ip address. What you're doing here is sending a packet to your network card. It is not really a matter of network because your machine most probably knows its own ip addresses and reroute it to 127.0.0.1

最偏执的依靠 2024-08-02 15:09:18

由于您有多个接口,因此您是多宿主的。 对于每个接口,您都会有一个 IP 地址。 因此,通过三个接口,您将拥有三个本地 IP。 当您使用 UdpClient 时,您需要使用其 IP 指定要发送的接口。

假设您有以下三个本地 IP

10.1.0.1
10.2.0.1
10.4.0.1
网络掩码为 255.255.0.0

,并且您想要将 UDP 数据包发送到 10.2.5.5,您需要将其发送到 10.2.0.1,因此使用以下代码

IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("10.2.0.1"), 0);
IPEndPoint targetEndPoint = new IPEndPoint(IPAddress.Parse("10.2.5.5"), iTargetPort);
UdpClient sendUdpClient = new UdpClient(localEndPoint);
int numBytesSent = sendUdpClient.Send(CombineHeaderBody, CombineHeaderBody.Length, targetEndPoint);

并将 UDP 数据包发送到 10.1.90.5,您需要将其发送到 10.1 .0.1所以使用下面的代码

IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("10.1.0.1"), 0);
IPEndPoint targetEndPoint = new IPEndPoint(IPAddress.Parse("10.1.90.5"), iTargetPort);
UdpClient sendUdpClient = new UdpClient(localEndPoint);
int numBytesSent = sendUdpClient.Send(CombineHeaderBody, CombineHeaderBody.Length, targetEndPoint);

两者的区别是localEndPoint和targetEndPoint。

Since you have more than one interface you are multihomed. For each interface you will have an IPaddress. So with three interfaces you will have three local IP's. When you use the UdpClient you need to specify which interface to send out by using it's IP.

lets assume you have the following three local IP's

10.1.0.1
10.2.0.1
10.4.0.1
with a netmask of 255.255.0.0

and you want to send a UDP packet to 10.2.5.5 you need to send it out 10.2.0.1 so use the following code

IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("10.2.0.1"), 0);
IPEndPoint targetEndPoint = new IPEndPoint(IPAddress.Parse("10.2.5.5"), iTargetPort);
UdpClient sendUdpClient = new UdpClient(localEndPoint);
int numBytesSent = sendUdpClient.Send(CombineHeaderBody, CombineHeaderBody.Length, targetEndPoint);

and to send a UDP packet to 10.1.90.5 you need to send it out 10.1.0.1 so use the following code

IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("10.1.0.1"), 0);
IPEndPoint targetEndPoint = new IPEndPoint(IPAddress.Parse("10.1.90.5"), iTargetPort);
UdpClient sendUdpClient = new UdpClient(localEndPoint);
int numBytesSent = sendUdpClient.Send(CombineHeaderBody, CombineHeaderBody.Length, targetEndPoint);

The difference between the two are the localEndPoint and the targetEndPoint.

绿萝 2024-08-02 15:09:18

我不是一个网络专家,但我会尝试尝试一下。 我假设所有数据包都是通过默认接口发送的,该接口知道三个地址:它自己的地址、环回地址和网关地址。 因此,从 192.168.1.111 发往 10.10.1.117 的数据包需要通过默认网关(假设这是 192.168.1.1),但如果 192.168.1.1 不知道到 10.10.0.0 的路由,那么目标主机会怎样?无法访问对吗? 我不知道,这是我的猜测。

虽然,也许我错了。 也许它不使用默认设备,而是使用所有可用的接口以及 TCP/IP 堆栈。 不管怎样,我很想看看你发现了什么。 请随时通知我们最新情况。

此外,记下地址可能会有所帮助。 追踪路线是你的朋友。

I'm not much of a network guru, but I'll try to take a stab at it. I assume that all the packets are sent through the default interface which knows of three adresses: it's own, loopback, and gateway. Therefore a packet from 192.168.1.111 destined for 10.10.1.117 would need to pass through the default gateway (let's just say this is 192.168.1.1), but what if 192.168.1.1 does not know the route to 10.10.0.0, then destination host unreachable right? I don't know, that's my guess.

Although, maybe I'm wrong. Maybe it doesn't use a default device and instead uses all available interfaces along with the TCP/IP stack. Anyway, I'm curious to see what you find though. Please keep us updated.

Additionally, noting down the addresses might be helpful. Trace route is your friend.

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