How to Udp Broadcast with C in Linux

How to broadcast Message using UDP sockets locally?

The server should not be bound to an address you get from getaddrinfo, instead it should be bound to 127.255.255.255 (for the loopback interface).

For a ready-made example of broadcast server/client see http://www.ccplusplus.com/2011/09/udp-broadcast-client-server-example.html

UDP Broadcast receive - Bind to several NIC's

If I understand you correctly, you have two NICs, connected to two physical networks (i.e. network cables, hubs), with each having a separate IP address from the same subnet address range?

The short answer is that your network configuration is wrong. If they really are separate physical networks, then they should have different subnet addresses. It depends on what you mean with separate physical networks, separate hardware? You can NOT have two separate subnets with the same subnet address. Thats why I am saying your network configuration is wrong.

However, The impression I get is that you are trying to bridge the two networks, so that the two NICs belong to the same subnet (not separate). Well, then you should bridge them. You bridge the two NICs together and assign ONE IP address to the bridge. Then you will be able to receive your packets on both NICs.

In linux:

brctl addbr br0
ifconfig eth0 0.0.0.0 down
ifconfig eth1 0.0.0.0 down
brctl addif br0 eth0
brctl addif br0 eth1
ifconfig eth0 up
ifconfig eth1 up
ifconfig br0 up
ifconfig br0 192.168.225.107 (or 192.168.225.108, whatever you prefer)

UDP broadcast and multicast messages arrive but recvfrom does not receive anything

You're not correctly setting the incoming interface for multicast traffic, and you're not setting the outgoing interface at all.

When you call joinMulticastGroup, you pass an empty string for the second argument which is supposed to contain the IP address of the incoming multicast interface as a string. So if for example the machine's IP is 192.168.178.34, then you pass "192.168.178.34" for that argument.

If you don't set the outgoing multicast interface explicitly, the OS will choose whichever interface is the "default". You should use the IP_MULTICAST_IF socket option, passing the address of a struct in_addr specifying the IP address.



Related Topics



Leave a reply



Submit