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
I tried running this server on my computer, but I got the following errors:
ReplyDeleteroot@linaro-ubuntu-desktop:/home/remote/Desktop/UDP# ./udp-broadcast-server.c 127.255.255.255:9097 & [1] 10658
[1] 2781
./udp-broadcast-server.c: line 1: /bin: Is a directory
./udp-broadcast-server.c: line 2: mkaddr.c: command not found
./udp-broadcast-server.c: line 3: mkaddr.c: command not found
: No such file or directoryine 4: */
./udp-broadcast-server.c: line 15: $'\r': command not found
./udp-broadcast-server.c: line 20: $'\r': command not found
./udp-broadcast-server.c: line 21: syntax error near unexpected token `('
'/udp-broadcast-server.c: line 21: ` extern int mkaddr(
[1]: command not found
[1]+ Exit 2 ./udp-broadcast-server.c 127.255.255.255:9097
Any idea what I'm doing wrong?
There is problem in your compilation. You need to first compile the code to run the executable. As mentioned above also
Delete"[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"
As mentioned by you seems you are running the code file
"./udp-broadcast-server.c "
My comp was connected to LAN with the ip of 192.168.0.10 but I am getting the following issue
ReplyDelete./udp-broadcast-server 127.255.255.255:9097 &
[1] 29353
Success: Bad server address
[1]+ Exit 1 ./udp-broadcast-server 127.255.255.255:9097