File: broadcastclient2.c

package info (click to toggle)
o2 1.0~repack-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,156 kB
  • sloc: ansic: 9,132; python: 137; sh: 111; makefile: 10
file content (116 lines) | stat: -rw-r--r-- 2,719 bytes parent folder | download
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* udp-broadcast-client.c
 * udp datagram client
 * Get datagram stock market quotes from UDP broadcast:
 * see below the step by step explanation
 */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif

/*
 * This function reports the error and
 * exits back to the shell:
 */
static void
displayError(const char *on_what) {
    fputs(strerror(errno),stderr);
    fputs(": ",stderr);
    fputs(on_what,stderr);
    fputc('\n',stderr);
    exit(1);
}

int
main(int argc,char **argv) {
    int z;
    socklen_t x;
    struct sockaddr_in adr;  /* AF_INET */
    int len_inet;            /* length */
    int s;                   /* Socket */
    char dgram[512];         /* Recv buffer */
    
    
    /*
     * Form the broadcast address:
     */
    len_inet = sizeof adr;
    
    adr.sin_family = AF_INET;
    if (inet_pton(AF_INET, "127.0.0.1", &adr.sin_addr.s_addr) != 1) {
        displayError("inet_pton");
    }
    adr.sin_port = htons(8124);
    
    struct addrinfo hints;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_DGRAM;
    hints.ai_protocol = 0;
    hints.ai_flags = AI_PASSIVE;
/* WORKS
    struct addrinfo *res;
    if (getaddrinfo(NULL, "8124", &hints, &res) < 0) {
        displayError("getaddrinfo");
    }
*/
    /* TEST */
    adr.sin_family = AF_INET;
    adr.sin_addr.s_addr = htonl(INADDR_ANY);
    adr.sin_port = htons(8124);
    /*
     * Create a UDP socket to use:
     */
    s = socket(AF_INET,SOCK_DGRAM,0);
    if (s == -1)
        displayError("socket()");

    
    /*
     * Bind our socket to the broadcast address:
     */
    z = bind(s, /*TEST*/ (struct sockaddr *) &adr, sizeof(adr));
             /* WORKS
             res->ai_addr,
             res->ai_addrlen);
              */
    
    if (z == -1)
        displayError("bind(2)");
    
    for (;;) {
        /*
         * Wait for a broadcast message:
         */
        x = sizeof(adr);
        z = recvfrom(s,      /* Socket */
                     dgram,  /* Receiving buffer */
                     sizeof dgram,/* Max rcv buf size */
                     0,      /* Flags: no options */
                     (struct sockaddr *)&adr, /* Addr */
                     &x);    /* Addr len, in & out */
        
        if (z < 0)
            displayError("recvfrom(2)"); /* else err */
        
        fwrite(dgram, z, 1, stdout);
        putchar('\n');
        
        fflush(stdout);
    }
    
    return 0;
}