Socket Broadcasting with Multiple Adapters

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm getting some weirdness when sending out broadcasts on a system with two
adapters. The adapters are configured with IP addresses on seperate subnets.
Packets get sent out on each interface, but the source addresses (as seen
from a packet sniffer) on both packets is the IP address for adapter #1. This
creates a problem when clients on adapter #2's network try to respond because
they can't reach the source address on adapter #1's network.

Here's some code that illustrates the problem:
public static void Main(string[] args)
{
byte[] bytes = Encoding.UTF8.GetBytes("Testing broadcast...");
IPEndPoint endpoint = new IPEndPoint(IPAddress.Broadcast, 10485);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);

socket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.Broadcast, 1);
socket.SendTo(bytes, endpoint);
}

My question is, am I doing something wrong? Or is this expected (or even
desired) behavior? If so, any ideas how I could make it work correctly?
 
From your code it is not clear what is "IPAddress" for (IPAddress.Broadcast)

Try to make sure your socket is bound to 0.0.0.0 IP address

beyond that - it is wierd
 
IPAddress.Broadcast is a reference to the broadcast address statically
provided by the IPAddress class. It's in there for code readability. I've
also tried sending to IPAddress.Parse("255.255.255.255") with the same
result. As for binding to 0.0.0.0, I've tried that before as well: no luck.

Arthur M. said:
From your code it is not clear what is "IPAddress" for (IPAddress.Broadcast)

Try to make sure your socket is bound to 0.0.0.0 IP address

beyond that - it is wierd

Ian Smith said:
I'm getting some weirdness when sending out broadcasts on a system with two
adapters. The adapters are configured with IP addresses on seperate subnets.
Packets get sent out on each interface, but the source addresses (as seen
from a packet sniffer) on both packets is the IP address for adapter #1. This
creates a problem when clients on adapter #2's network try to respond because
they can't reach the source address on adapter #1's network.

Here's some code that illustrates the problem:
public static void Main(string[] args)
{
byte[] bytes = Encoding.UTF8.GetBytes("Testing broadcast...");
IPEndPoint endpoint = new IPEndPoint(IPAddress.Broadcast, 10485);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);

socket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.Broadcast, 1);
socket.SendTo(bytes, endpoint);
}

My question is, am I doing something wrong? Or is this expected (or even
desired) behavior? If so, any ideas how I could make it work correctly?
 
There are two problems here,

I have not tried broadcasting through socket - hence theory here:

If binding to 0.0.0.0 does not work (I wonder what is the source then)
you have to open a socket per network card;

Also remember, that when you broadcast, you should broadcast to a local
network doing otherwies is bad networking.

Broadcasts wont always fall into x.x.x.255 scheme, you are looking at IP
Address + MASK combination; without going into a lecture

if I have a class C with address of 192.168.1.0, which i split into 29 bit
nets, I will get

192.168.1.0 / broadcast 192.168.1.7, 192.168.1.8 / broadcast 192.168.1.15
etc...
Just watch out for that, because broadcasting to 192.168.1.255 in that case
will deliver data to supernet if it is reachable:)

Thinking of that, it does make sense to use 2 different sockets, to make
sure each of your networks gets its own handling for broadcasted data.







Ian Smith said:
IPAddress.Broadcast is a reference to the broadcast address statically
provided by the IPAddress class. It's in there for code readability. I've
also tried sending to IPAddress.Parse("255.255.255.255") with the same
result. As for binding to 0.0.0.0, I've tried that before as well: no luck.

Arthur M. said:
From your code it is not clear what is "IPAddress" for (IPAddress.Broadcast)

Try to make sure your socket is bound to 0.0.0.0 IP address

beyond that - it is wierd

Ian Smith said:
I'm getting some weirdness when sending out broadcasts on a system with two
adapters. The adapters are configured with IP addresses on seperate subnets.
Packets get sent out on each interface, but the source addresses (as seen
from a packet sniffer) on both packets is the IP address for adapter #1. This
creates a problem when clients on adapter #2's network try to respond because
they can't reach the source address on adapter #1's network.

Here's some code that illustrates the problem:
public static void Main(string[] args)
{
byte[] bytes = Encoding.UTF8.GetBytes("Testing broadcast...");
IPEndPoint endpoint = new IPEndPoint(IPAddress.Broadcast, 10485);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);

socket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.Broadcast, 1);
socket.SendTo(bytes, endpoint);
}

My question is, am I doing something wrong? Or is this expected (or even
desired) behavior? If so, any ideas how I could make it work correctly?
 
Back
Top