File: broadcastserver.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 (217 lines) | stat: -rw-r--r-- 4,783 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* udp-broadcast-server.c:
 * udp broadcast server example 
 * Example Stock Index Broadcast:
 */

#ifdef WIN32
#include <winsock2.h> 
#include <windows.h> 
#else
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#endif

#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>

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

#define MAXQ 4

static struct {
    char *index;
    int start;
    int volit;
    int current;
} quotes[] = {
    { "DJIA", 1030330, 375 },
    { "NASDAQ", 276175, 125 },
    { "S&P 500", 128331, 50 },
    { "TSE 300", 689572, 75 },
};

/*
 * Initialize:
 */
static void initialize(void) {
    short x;
    time_t td;
	
    /*
     * Seed the random number generator:
     */
    time(&td);
    srand((int)td);
	
    for ( x=0; x < MAXQ; ++x )
        quotes[x].current =
            quotes[x].start;
}

/*
 * Randomly change one index quotation:
 */
static void gen_quote(void) {
    short x;    /* Index */
    short v;    /* Volatility of index */
    short h;    /* Half of v */
    short r;    /* Random change */
	
    x = rand() % MAXQ;
    v = quotes[x].volit;
    h = (v / 2) - 2;
    r = rand() % v;
    if ( r < h )
		r = -r;
    quotes[x].current += r;
}

/*
 * 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) {
    short x;    /* index of Stock Indexes */
    double I0;  /* Initial index value */
    double I;   /* Index value */
    char bcbuf[512], *bp;/* Buffer and ptr */
    int z;      /* Status return code */
    int broadcast_sock;      /* Socket */
    int local_send_sock;      /* Socket */
    struct sockaddr_in broadcast_to_addr; /* AF_INET */
    struct sockaddr_in local_to_addr;  /* AF_INET */
    static int so_broadcast = TRUE;
    /*
     * Form the dest address:
     */
    memset(&broadcast_to_addr, 0, sizeof(broadcast_to_addr));

#ifndef WIN32
	broadcast_to_addr.sin_len = sizeof(broadcast_to_addr);
#endif

    broadcast_to_addr.sin_family = AF_INET;
    if (inet_pton(AF_INET, "255.255.255.255",
                  &(broadcast_to_addr.sin_addr.s_addr)) != 1) {
        displayError("inet_pton");
    }
    broadcast_to_addr.sin_port = htons(8124);
    
    /*
     * Create a UDP socket to use:
     */
    broadcast_sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (broadcast_sock == -1)
		displayError("socket()");
    
    /*
     * Allow broadcasts:
     */
    z = setsockopt(broadcast_sock,
                   SOL_SOCKET,
                   SO_BROADCAST,
                   (const char *) &so_broadcast,
                   sizeof so_broadcast);
    
    if (z == -1)
		displayError("setsockopt(SO_BROADCAST)");
    
    /*
     * Form the local dest address:
     */

#ifndef WIN32
	local_to_addr.sin_len = sizeof(broadcast_to_addr);
#endif

    local_to_addr.sin_family = AF_INET;
    if (inet_pton(AF_INET, "127.0.0.1",
                  &(local_to_addr.sin_addr.s_addr)) != 1) {
        displayError("inet_pton");
    }
    local_to_addr.sin_port = htons(8123);
    
    /*
     * Create a UDP socket to use:
     */
    local_send_sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (local_send_sock == -1)
        displayError("socket()");
    
    /*
     * Now start serving quotes:
     */
    initialize();
	
    for (;;) {
        /*
         * Update one quote in the list:
         */
        gen_quote();
		
        /*
         * Form a packet to send out:
         */
        bp = bcbuf;
        for (x = 0; x < MAXQ; ++x) {
            I0 = quotes[x].start / 100.0;
            I = quotes[x].current / 100.0;
            sprintf(bp,
                    "%-7.7s %8.2f %+.2f\n",
                    quotes[x].index,
                    I,
                    I - I0);
            bp += strlen(bp);
        }
        
        /*
         * Broadcast the updated info:
         */
        z = sendto(broadcast_sock,
                   bcbuf,
                   strlen(bcbuf),
                   0,
                   (struct sockaddr *)&broadcast_to_addr,
                   sizeof(broadcast_to_addr)); 
        
        if (z == -1)
			displayError("broadcast sendto()");
        
        z = sendto(local_send_sock,
                   bcbuf,
                   strlen(bcbuf),
                   0,
                   (struct sockaddr *)&local_to_addr,
                   sizeof(broadcast_to_addr)); 
        
        if (z == -1)
			displayError("local sendto()");
        
#ifdef WIN32
		Sleep(4);
#else
		sleep(4)
#endif
    }
    
    return 0;
}