广播UDP消息到所有可用的网卡 [英] Broadcasting UDP message to all the available network cards

查看:819
本文介绍了广播UDP消息到所有可用的网卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要发送一个UDP信息到特定的IP和端口。

由于有3个网卡,

  10.1.x.x
10.2.x.x
10.4.x.x

当我发送一个UDP消息,我只在一个网络适配器接收消息......在IP的其余部分都没有收到。

我要检查网络适配器同时发送消息。我该怎么做?


目前我使用以下内容:

  IPEndPoint localEndPoint =新IPEndPoint(IPAddress.Parse(LocalIP),0);
IPEndPoint targetEndPoint =新IPEndPoint(TargetIP,iTargetPort);
UdpClient sendUdpClient =新的UdpClient(localEndPoint);
INT numBytesSent = sendUdpClient.Send(CombineHeaderBody,CombineHeaderBody.Length,targetEndPoint);


解决方案

这实际上是棘手的,这听起来因为如果你有多个接口的广播将不会经常出去所有的接口。为了解决这个问题,我创建这个类。

 公共类MyUdpClient:UdpClient
{
   公共MyUdpClient():基地()
   {
      //调用属于UdpClient基类保护客户财产。
      插座S = this.Client;
      //使用由客户端返回到设置一个选项使用UdpClient不可用的插座。
      s.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.Broadcast,1);
      s.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.DontRoute,1);
   }   公共MyUdpClient(IPEndPoint ipLocalEndPoint):基地(ipLocalEndPoint)
   {
      //调用属于UdpClient基类保护客户财产。
      插座S = this.Client;
      //使用由客户端返回到设置一个选项使用UdpClient不可用的插座。
      s.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.Broadcast,1);
      s.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.DontRoute,1);
   }}

然后通过广播发送UDP数据包我用像我使用IPAddress.Broadcast和MyUdpClient这是从code中的不同以下。

  IPEndPoint localEndPoint =新IPEndPoint(IPAddress.Parse(LocalIP),0);
IPEndPoint targetEndPoint =新IPEndPoint(IPAddress.Broadcast,iTargetPort);
MyUdpClient sendUdpClient =新MyUdpClient(localEndPoint);
INT numBytesSent = sendUdpClient.Send(CombineHeaderBody,CombineHeaderBody.Length,targetEndPoint);

此外,你应该,如果你使用特定的ip地址,而不是广播注意路由表将只发送它的地址相匹配的接口。

所以你的榜样单播的情况下使用,但你需要LocalIP设置IP您要送出去的本地接口。有三个接口,您将有三个本​​地IP的,你需要选择正确的使用。

  IPEndPoint localEndPoint =新IPEndPoint(IPAddress.Parse(LocalIP),0);
IPEndPoint targetEndPoint =新IPEndPoint(TargetIP,iTargetPort);
MyUdpClient sendUdpClient =新MyUdpClient(localEndPoint);
INT numBytesSent = sendUdpClient.Send(CombineHeaderBody,CombineHeaderBody.Length,targetEndPoint);

由于线路被关闭,你可能会看到它在所有接口上,但你需要测试这个单播情况。

如果你不关心发送IP或端口,你可以使用下面的code。

  IPEndPoint targetEndPoint =新IPEndPoint(TargetIP,iTargetPort);
MyUdpClient sendUdpClient =新MyUdpClient();
INT numBytesSent = sendUdpClient.Send(CombineHeaderBody,CombineHeaderBody.Length,targetEndPoint);

或用于广播

  IPEndPoint targetEndPoint =新IPEndPoint(IPAddress.Broadcast,iTargetPort);
MyUdpClient sendUdpClient =新MyUdpClient();
INT numBytesSent = sendUdpClient.Send(CombineHeaderBody,CombineHeaderBody.Length,targetEndPoint);

与IPAddress.Broadcast的问题是,它们不会通过任何网关路由。为了解决这个问题,你可以通过创建IPAddresses的列表,然后循环和发送。此外,由于发送可能会失败,你无法控制你也应该有一个try / catch块的网络问题。

  ArrayList的ip_addr_acq =新的ArrayList();
ip_addr_acq.Add(IPAddress.Parse(10.1.1.1)); //添加到地址发送到列表尝试
{
   的foreach(ip地址curAdd在ip_addr_acq)
   {
       IPEndPoint targetEndPoint =新IPEndPoint(curAdd,iTargetPort);
       MyUdpClient sendUdpClient =新MyUdpClient();
       INT numBytesSent = sendUdpClient.Send(CombineHeaderBody,CombineHeaderBody.Length,targetEndPoint);       Thread.sleep代码(40); //每个消息之间的小延迟
    }
 }
 抓住
 {
 //处理异常
 }

编辑:见上述变化有多个接口,单播和还<一href=\"http://stackoverflow.com/questions/1096497/problem-trying-to-unicast-packets-to-available-networks/1101383#1101383\"相对=nofollow>问题试图单播数据包到可用网络。

I need to send a UDP message to specific IP and Port.

Since there are 3 network cards,

10.1.x.x
10.2.x.x
10.4.x.x

when i send a UDP message,i am receiving the message only in one network adapter...the rest of the ip's are not receiving.

I want to check for the network adapter while sending the message. How can I do that?


Currently I am using the following:

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

解决方案

This is actually trickier that it sounds because if you have more than one interface the broadcasts will not always go out all of the interfaces. To get around this I created this class.

public class MyUdpClient : UdpClient
{
   public MyUdpClient() : base()
   {
      //Calls the protected Client property belonging to the UdpClient base class.
      Socket s = this.Client;
      //Uses the Socket returned by Client to set an option that is not available using UdpClient.
      s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
      s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontRoute, 1);
   }

   public MyUdpClient(IPEndPoint ipLocalEndPoint) : base(ipLocalEndPoint)
   {
      //Calls the protected Client property belonging to the UdpClient base class.
      Socket s = this.Client;
      //Uses the Socket returned by Client to set an option that is not available using UdpClient.
      s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
      s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontRoute, 1);
   }

}

Then to send the UDP packet via broadcast I use something like the following I am using IPAddress.Broadcast and MyUdpClient which is the different from your code.

IPEndPoint  localEndPoint  = new IPEndPoint(IPAddress.Parse(LocalIP), 0);
IPEndPoint  targetEndPoint = new IPEndPoint(IPAddress.Broadcast, iTargetPort);
MyUdpClient sendUdpClient  = new MyUdpClient(localEndPoint);
int numBytesSent = sendUdpClient.Send(CombineHeaderBody, CombineHeaderBody.Length, targetEndPoint);

Also you should note if you use a specific ipaddress instead of broadcast the route table will only send it out the interface that matches the address.

so for your example unicast case use but you need to set LocalIP to the IP for the local interface you want to send out. With three interfaces you would have three local IP's and you need to pick the correct one to use.

IPEndPoint  localEndPoint  = new IPEndPoint(IPAddress.Parse(LocalIP), 0);
IPEndPoint  targetEndPoint = new IPEndPoint(TargetIP, iTargetPort);
MyUdpClient sendUdpClient  = new MyUdpClient(localEndPoint);
int numBytesSent = sendUdpClient.Send(CombineHeaderBody, CombineHeaderBody.Length, targetEndPoint);

Because route is turned off you might see it on all interfaces but you will need to test this for the unicast case.

If you don't care about the send IP or port you can use the following code.

IPEndPoint  targetEndPoint = new IPEndPoint(TargetIP, iTargetPort);
MyUdpClient sendUdpClient  = new MyUdpClient();
int numBytesSent = sendUdpClient.Send(CombineHeaderBody, CombineHeaderBody.Length, targetEndPoint);

or for broadcast

IPEndPoint  targetEndPoint = new IPEndPoint(IPAddress.Broadcast, iTargetPort);
MyUdpClient sendUdpClient  = new MyUdpClient();
int numBytesSent = sendUdpClient.Send(CombineHeaderBody, CombineHeaderBody.Length, targetEndPoint);

The problem with IPAddress.Broadcast is that they will not route through any gateways. To get around this you can create list of IPAddresses and then loop through and send. Also since Send can fail for network issues that you cannot control you should also have a try/catch block.

ArrayList ip_addr_acq = new ArrayList();


ip_addr_acq.Add(IPAddress.Parse("10.1.1.1")); // add to list of address to send to

try
{
   foreach (IPAddress curAdd in ip_addr_acq) 
   {
       IPEndPoint  targetEndPoint = new IPEndPoint(curAdd , iTargetPort);
       MyUdpClient sendUdpClient  = new MyUdpClient();
       int numBytesSent = sendUdpClient.Send(CombineHeaderBody, CombineHeaderBody.Length, targetEndPoint);

       Thread.Sleep(40); //small delay between each message
    }
 }
 catch
 {
 // handle any exceptions
 }

Edit: see above change to unicast with multiple interfaces and also Problem Trying to unicast packets to available networks.

这篇关于广播UDP消息到所有可用的网卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆