Here is the step by step code flow of the abover udp server: The server shown in Listing above is functionally divided into the following sections:
- The table of stock market indexes is declared in lines 27 to 39. Four indexes are defined with starting values and a crude form of volatility value, which is used for the simulation.
- The function initialize() in lines 44 to 58 is called once to initialize for the simulation.
- The function gen_quote() is called to randomly change the simulated value of a randomly selected stock market index (lines 63 to 77).
- The main() program, which forms the basis of the server, is contained within lines 92 to 222.
- Default addresses are declared in lines 106 and 107. These are used when no command-line arguments are supplied.
- If two command-line arguments are supplied, then the server address takes the address from the second argument (line 114).
- If one or more command-line arguments are supplied, then the broadcast argument is taken from argument one (line 118).
- The server address is formed (lines 123 to 132).
- The broadcast address is formed (lines 137 to 146).
- A socket is created (lines 151 to 153).
- The SO_BROADCAST option is enabled on the socket (lines 158 to 165).
- The server address is bound (lines 172 to 177).
- The stock market indices are initialized (line 182).
- The server loop begins in line 184.
Compiling the above server:
Take this version of mkaddr.c
[root@rhel54x64 socket]# gcc -c -D_GNU_SOURCE -Wall -Wreturn-type -g udp-broadcast-server.c
[root@rhel54x64 socket]# gcc -c -D_GNU_SOURCE -Wall -Wreturn-type -g mkaddr.c
[root@rhel54x64 socket]# gcc -g udp-broadcast-server.o mkaddr.o -o udp-broadcast-server
Here is the complete udp broadcast client-server application