hudac
hudac

Reputation: 2798

UDP broadcasts aren't being sent on specific occasions

I've got this strange occurrence:

This is how I set up a UDP socket for broadcasting:

int broadcast_enable = 1;
int my_socket;

if ((my_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {

    fprintf(stderr, "ERROR socket\n");
    return 0;
}

memset(&my_addr, '\0', sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
my_addr.sin_port = htons(20007);

setsockopt(my_socket, SOL_SOCKET, SO_BROADCAST, &broadcast_enable, sizeof(broadcast_enable));

This is how I send messages through this socket:

int len = sizeof(my_addr);
char buff;

buff = 'b';

if (sendto(my_socket, &buff, sizeof(buff), 0, (struct sockaddr*)&my_addr, len) < 0) {

    fprintf(stderr, "ERROR sendto\n");
    return 0;
}

I put WireShark on the sending computer and listened to the wifi interface.
1. When I connect to network 'A' I can see the messages being sent from the sender.
2. When I connect to network 'B' I can't see the messages being sent from the sender.

Does it rings a bell to anyone?
Please tell if you need any further information.

Upvotes: 1

Views: 248

Answers (2)

mike622867
mike622867

Reputation: 377

You may be using Wireshark wrong, e.g., looking at a wrong i/f or wrongly filtering the results. Make sure you use it correctly, or better yet, try tcpdump to make sure your packets are indeed not being sent:

sudo tcpdump -i any -n udp

Upvotes: 1

Simon Richter
Simon Richter

Reputation: 29618

You haven't specified which interface to send the packet to. Broadcast packets are still sent to one interface only.

If I remember correctly, the default for 255.255.255.255 is the interface with the default route. If you use one of the subnet broadcast addresses on one of the interfaces, that interface is selected; alternatively, you can explicitly choose one via a socket option.

Upvotes: 1

Related Questions