summaryrefslogtreecommitdiff
path: root/client.c
blob: 524a0a82ca2c9a1c62925d6d873000f38f8b4ca7 (plain)
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
// Initially based on https://stackoverflow.com/a/35570418

#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

int main(void)
{
    int const fd = socket(AF_INET, SOCK_DGRAM, 0);
    if (fd < 0) {
        perror("socket failed");
        return 1;
    }
    struct sockaddr_in const serveraddr = {
        .sin_family = AF_INET,
        .sin_port = htons(50037),
        .sin_addr.s_addr = htonl(0x7f000001),
    };
    for (int i = 0; i < 4; i++) {
        ssize_t const ret = sendto(
                fd, "hello", 5, 0, (struct sockaddr const *)&serveraddr, sizeof(serveraddr));
        if (ret < 0) {
            perror("sendto failed");
            break;
        }
        printf("message sent\n");
    }
    close(fd);
}