mario::konrad
programming / C++ / sailing / nerd stuff
Broadcast Sender/Listener
© 2003 / Mario Konrad

This tutorial shows how to send and receive broadcast UDP packets on Linux.

Code

listener.c:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <errno.h>
#include <string.h>
#include <arpa/inet.h>

int main(int argc, char ** argv)
{
    int rc;
    int sock;
    int tmp;
    struct sockaddr_in addr;
    int32_t data;
    struct sockaddr_in caddr;
    socklen_t clen;

    sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (sock < 0) {
        perror("socket");
        return EXIT_FAILURE;
    }

    tmp = 1;
    rc = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof(tmp));
    if (rc < 0) {
        perror("setsockopt");
        return EXIT_FAILURE;
    }

    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(5678);
    addr.sin_addr.s_addr = htonl(INADDR_ANY);

    rc = bind(sock, (const struct sockaddr *)&addr, sizeof(addr));
    if (rc < 0) {
        perror("bind");
        return EXIT_FAILURE;
    }

    for (;;) {
        memset(&caddr, 0, sizeof(caddr));
        clen = sizeof(caddr);
        rc = recvfrom(sock, &data, sizeof(data), 0,
            (struct sockaddr *)&caddr, &clen);
        if (rc < 0) {
            perror("recv");
            return EXIT_FAILURE;
        }

        printf("recv: 0x%08x  from  %s\n", data,
            inet_ntoa(caddr.sin_addr));
    }

    return EXIT_SUCCESS;
}

sender.c:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <netinet/in.h>

int main(int argc, char ** argv)
{
    int rc;
    int sock;
    int tmp;
    struct sockaddr_in addr;
    int32_t data;
    struct hostent * hp;

    sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (sock < 0) {
        perror("socket");
        return EXIT_FAILURE;
    }

    tmp = 1;
    rc = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &tmp, sizeof(tmp));
    if (rc < 0) {
        perror("setsockopt");
        return EXIT_FAILURE;
    }

    hp = gethostbyname(argv[1]);
    if (hp == 0) {
        perror("sethostbyname");
        return EXIT_FAILURE;
    }

    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(5678);
    addr.sin_addr.s_addr = ((struct in_addr *)hp->h_addr)->s_addr;

    data = 0xdeadbeef;

    rc = sendto(sock, &data, sizeof(data), 0,
        (const struct sockaddr *)&addr, sizeof(addr));
    if (rc < 0) {
        perror("sendto");
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

Compile

$ gcc -o listener listener.c -Wall -Wextra
$ gcc -o sender sender.c -Wall -Wextra

Execute

Start listener in one shell:

$ ./listener

Start sender in another shell:

$ ./sender 127.0.0.255

If you are on the 192.168.1.0 network:

$ ./sender 192.168.1.255