Problem Trying to unicast packets to available networks

早过忘川 提交于 2019-12-11 10:58:37

问题


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.


回答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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/1096497/problem-trying-to-unicast-packets-to-available-networks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!