/* * seed the random number generator: */ time(&td); srand((int)td);
for(x=0;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 r = -r; quotes[x].current += r; }
/* * This function report the error and * exits back to the shell: */ static void bail(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 s; /* socket */ struct sockaddr_in adr_srvr; /* AF_INET */ int len_srvr; /* length */ struct sockaddr_in adr_bc; /* AF_INET */ int len_bc; /* length */ static int so_broadcast = TRUE; static char *sv_addr = "127.0.0.1:*", *bc_addr = "127.255.255.255:9097";
/* * Form a server address: */ if(argc >2) /* server address */ sv_addr = argv[2];
/* * bind an address to our socket,so that * client programs can listen to this * server */ z = bind(s,(struct sockaddr *)&adr_srvr,len_srvr);
if(z==-1) bail("bind()");
/* * Now start serving quotes: */ initialize();
for(;;) { /* * update on quote in the list: */ gen_quote();
/* * Form a packet to send out: */ bp = bcbuf; for(x=0;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(s,bcbuf,strlen(bcbuf),0,(struct sockaddr *)&adr_bc,len_bc); if(z==-1) bail("sendto()"); sleep(4); }
/* * This function report the error and * exits back to the shell: */ static void bail(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; int x; struct sockaddr_in adr; /* AF_INET */ int len_inet; /* length */ int s; /* socket */ char dgram[512]; /* recv buffer */ static int so_reuseaddr = TRUE; static char *bc_addr = "127.255.255.255:9097";
/* * use a server address from the command * line,if one has been provided. * otherwise,this program will default * to using the arbitrary address * 127.0.0.23: */ if(argc>1) /* broadcast address: */ bc_addr = argv[1];
/* * Create a UDP socket to use: */ s = socket(AF_INET,SOCK_DGRAM,0); if(s==-1) bail("socket()");
/* * Form the broadcast address: */ len_inet = sizeof adr;
z = mkaddr(&adr, &len_inet, bc_addr, "udp");
if(z==-1) bail("bad broadcast address");
/* * Allow multiple listeners on the * broadcast address: */ z = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &so_reuseaddr, sizeof so_reuseaddr);
if(z==-1) bail("setsockopt(SO_REUSEADDR)");
/* * bind out socket to the broadcast address: */ z = bind(s, (struct sockaddr *)&adr, len_inet);
if(z==-1) bail("bind(2)");
for(;;) { /* * wait for a broadcast message: */ z = recvfrom(s, /*socket */ dgram, /* receiving buffer */ sizeof dgram, /* max rcv buf size */ 0, /* flags:no options */ (struct sockaddr *)&adr, /* addr */ &x); /* addr len */