views:

1168

answers:

2

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);
A: 

If you are send to a specific IP address then you are unicasting, not broadcasting.

Simeon Pilgrim
Oh yeah thats right. But i shud select the network before sending right ?
Anuya
Ah you trying to send a packet out each local network card to a remote host with a single network card? If your try to test each network card, find a host on that network to send a message to. Let the OS do the routing for you.
Simeon Pilgrim
But the OS is always routing to the first network card 10.1.x.x
Anuya
If you want to send out different Nic's send to the different addresses of the hosts. That is if they are all on three networks. It comes down to what you are try to achieve, testing the network connectivity, or send an actual higher layer message.
Simeon Pilgrim
+2  A: 

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.

Rex Logan
Hi Rex Logan, when i tried ur code, i am getting the following error :A Socket operation was attempted to an unreacheable host.I am confused here, the network adapter with that IP exists.when i debugged the below line...int numBytesSent = sendUdpClient.Send(CombineHeaderBody, CombineHeaderBody.Length, targetEndPoint);i saw targetEndPoint = {10.4.1.2:1982}.when the targetEndPoint is {10.1.1.1:1982} i am receiving the packet in remote machine. :(
Anuya
What happens if you send to IPAddress.Broadcast? You might try the simplified versions I added as well.
Rex Logan
You also might try commenting out s.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.DontRoute, 1);or s.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.Broadcast, 1) to see what happens
Rex Logan
Its the same. could find that IP.I tried to ping 10.4.1.2 but it is says "Destination host unreachable". So now it is a network problem or still applicaiton problem ? Thanks.
Anuya
when i do ipaddress.broadcast, i am not receing the packet in remote machine.
Anuya