Upd broadcast on all adapters using sockets

G

Gunnar_Frenzel

Hello,
this might be an easy question, but I don't have any handy solution at
hand. I have an application that is supposed to send UDP broadcast. So
far so easy, I did:

Socket sockSendBroadcast = null;
IPEndPoint ipeSendBroadcast = null;

ipeSendBroadcast = new IPEndPoint(IPAddress.Broadcast, iSomePort);

sockSendBroadcast = new Socket(ipeSendBroadcast.AddressFamily,
SocketType.Dgram, ProtocolType.Udp);

sockSendBroadcast.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.Broadcast, 1);

sockSendBroadcast.SendTo(Encoding.ASCII.GetBytes("blah"),
ipeSendBroadcast);


That works fine so far, but when I have two in the system (e.g. LAN and
WLAN NIC in different subnets) then it sends the broadcast to the LAN
broadcast address on the LAN adapter only. So I didn't reach any WLAN
devices. What I want to do is send a broadcast the the LAN subnet
broadcast address as well as the WLAN subnet broadcast address. I could
get all local IPs using IPHostEntry of the local machine but how can I
create the corresponding broadcast sockets without binding them to a
specific Port? It is supposed to use the next free port as source port,
just like it does using the code above for one network adapter. For
example is there a way to get the corresponding Subnetbroadcast address
when I have all local IPs retreived from the IPHostEntry to create two
IPEndPoints with instead of using IPAddress.Broadcast? Then the
broadcast to subnet's 2 broadcast address would probably send using
adapter 2...
Thanks in advance and sorry if it's totally easy and I'm to confused to
see the easy way... ;)
Gunnar
 
M

Markus Stoeger

That works fine so far, but when I have two in the system (e.g. LAN and
WLAN NIC in different subnets) then it sends the broadcast to the LAN
broadcast address on the LAN adapter only. So I didn't reach any WLAN
devices. <snip>

You are on the right track already. You have to bind one socket to each
network interfaces IP address. If you bind to port 0 it will allocate
the next free port. And use SetSocketOption to enable broadcasting.

hth,
Max
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top