Send a broadcast message from all network adapters

I have an application that sends broadcast messages and listens for response packets. Below is a snippet of code.

m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); m_socket.Bind(new IPEndPoint(IPAddress.Any, 2000)); m_socket.BeginSendTo( buffer, 0, buffer.Length, SocketFlags.None, new IPEndPoint(IPAddress.Broadcast, 2000), Callback), null ); 

When I launch the application, no broadcast message was sent. I have three network adapters on my machine. One of them is my local network adapter, and the other two are VMWare virtual adapters. When I launch my application, I see (using wirehark network capture) that a broadcast message is being sent from one of VMWare's network adapters.

I would like to change the code so that the broadcast message is sent from all network adapters to the PC. What is the best way to do this?

+7
c # sockets
source share
4 answers

You can use the following to get all your IP addresses (and much more). This way you can iterate over the list and associate (e.g. Jon B) with the specific IP address you want when you send your multicast.

 foreach (var i in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()) foreach (var ua in i.GetIPProperties().UnicastAddresses) Console.WriteLine(ua.Address); 
+11
source share

When you call Bind (), you set the local IP endpoint. Instead of using IPAddress.Any, use the IP address of the network card from which you want to send. You will need to do this separately for each network card.

+6
source share

Check out http://salaam.codeplex.com/ My friend and I developed a class library called Salaam. download the source code or use binaries (.dll) to use it.

+1
source share

You can use IPAddress.Any when creating tcpListener. This will connect the tcp listener to all interfaces

-one
source share

All Articles