How to UDP Broadcast with C in Linux?
Asked
Active
Viewed 1.1e+01k times
36
-
Programmatically, or via utilities/tools? – ayaz Dec 03 '08 at 15:14
4 Answers
51
In many IP stack, such as Linux, this code does not work. Your socket must have broadcast permissions. Try this:
bcast_sock = socket(AF_INET, SOCK_DGRAM, 0);
int broadcastEnable=1;
int ret=setsockopt(bcast_sock, SOL_SOCKET, SO_BROADCAST, &broadcastEnable, sizeof(broadcastEnable));
/* Add other code, sockaddr, sendto() etc. */
-
8is right! If you don't give the socket broadcasting permissions, you will get permission denied error messages. – Chang Hyun Park Sep 20 '12 at 17:09
-
7This behavior is actually required by POSIX, see http://pubs.opengroup.org/onlinepubs/009695399/functions/sendto.html `sendto() shall fail if the SO_BROADCAST option is not set for the socket` – Jan Smrčina Mar 07 '16 at 16:02
-
4Could you clarify what you mean by `this code does not work`? The question doesn't contain any code. – Emil S. Jul 30 '19 at 17:54
-
@EmilS. look at the other answers. They all from '08, while this one is from '12. I already had code without `setsockopt`, but it didn't work. So here is why. I vote up. – Mikolasan Oct 06 '21 at 13:39
-
3Yes, the code sample in this answer does answer the question well and with up-to-date code. But I still don't know which code is referred to by `this code does not work`. If the code in the other answers is referred to, then it should be `The code from the other answers does not work`. If a specific answer is referred to, you can use `The code from [user]'s answer does not work` or even `does not work by itself`. The wording here just makes this answer a little hard to understand. – Emil S. Oct 08 '21 at 11:50
16
Unwind has it right, except you should use 'sendto'
Here is an example, that assumes you already have a socket. It was taken from clamav
static void
broadcast(const char *mess)
{
#define BROADCAST_PORT 30000u
struct sockaddr_in s;
int broadcastSock = socket(AF_INET, SOCK_DGRAM, 0);
if(broadcastSock < 0)
return;
memset(&s, '\0', sizeof(struct sockaddr_in));
s.sin_family = AF_INET;
s.sin_port = htons(BROADCAST_PORT)
s.sin_addr.s_addr = INADDR_BROADCAST; /* This is not correct : htonl(INADDR_BROADCAST); */
cli_dbgmsg("broadcast %s to %d\n", mess, broadcastSock);
if(sendto(broadcastSock, mess, strlen(mess), 0, (struct sockaddr *)&s, sizeof(struct sockaddr_in)) < 0)
perror("sendto");
}
4
Typically using the Berkeley sockets API, to sendto()
one or more datagrams to a known broadcast-class IP address.

unwind
- 391,730
- 64
- 469
- 606
-
I changed the function suggested, to match the actual code shodane dug up. – unwind Dec 03 '08 at 15:33
3
I wrote udp multicast server recently for testing. To subscribe to multicast you would subscribe your client to Multicast group 225.0.0.37 port 12346 and port 12345 (2 feeds - one feeds sends "Hello, World!" the other one "Bye, Office!").
I've been using it for testing my client, both client and server run on the same box so there might be bits that may not work but give it a try first.
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#define BYE_OFFICE 12346
#define HELLO_PORT 12345
#define HELLO_GROUP "225.0.0.37"
int main(int argc, char *argv[])
{
struct sockaddr_in addr;
struct sockaddr_in addr2;
int fd;
int fd2;
char *message = "Hello, World!";
char *message2 = "Bye, Office!";
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("socket");
exit(1);
}
if ((fd2 = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("socket");
exit(1);
}
/* set up destination address */
memset(&addr,0,sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(HELLO_GROUP);
addr.sin_port=htons(HELLO_PORT);
memset(&addr2,0,sizeof(addr2));
addr2.sin_family = AF_INET;
addr2.sin_addr.s_addr = inet_addr(HELLO_GROUP);
addr2.sin_port=htons(BYE_OFFICE);
while (1)
{
if (sendto(fd, message, strlen(message), 0,(struct sockaddr *) &addr, sizeof(addr)) < 0)
{
perror("sendto");
exit(1);
}
sleep(3);
if (sendto(fd2, message2, strlen(message2), 0,(struct sockaddr *) &addr2, sizeof(addr2)) < 0)
{
perror("sendto2");
exit(1);
}
sleep(3);
}
}
-
10What does this have to do with the question? The question is about broadcast and this is about multicast – xaxxon Sep 02 '13 at 07:10
-
1Hate to wake a dead thread, but IMO this is related to the question, just maybe a little extra verbose. This is the more useful answer on this page, in my opinion—with the exception of the one mentioning that your socket needs broadcast permissions. While multicast and broadcast are different, this code is functionally the same as broadcast. – Will Eccles Mar 13 '20 at 12:02
-