Friday, September 9, 2011

udp broadcast server c


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: 
  1. 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.  
  2. The function initialize() in lines 44 to 58 is called once to initialize for the simulation.  
  3. The function gen_quote() is called to randomly change the simulated value of a randomly selected stock market index (lines 63 to 77).  
  4. The main() program, which forms the basis of the server, is contained within lines 92 to 222. 
The basic operation of this stock market server is as follows:
  1. Default addresses are declared in lines 106 and 107. These are used when no command-line arguments are supplied. 
  2. If two command-line arguments are supplied, then the server address takes the address from the second argument (line 114).
  3. If one or more command-line arguments are supplied, then the broadcast argument is taken from argument one (line 118).
  4. The server address is formed (lines 123 to 132).
  5. The broadcast address is formed (lines 137 to 146).
  6. A socket is created (lines 151 to 153). 
  7. The SO_BROADCAST option is enabled on the socket (lines 158 to 165).
  8. The server address is bound (lines 172 to 177). 
  9. The stock market indices are initialized (line 182).
  10. 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


  1. I tried running this server on my computer, but I got the following errors:

    root@linaro-ubuntu-desktop:/home/remote/Desktop/UDP# ./udp-broadcast-server.c & [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

    Any idea what I'm doing wrong?

    1. There is problem in your compilation. You need to first compile the code to run the executable. As mentioned above also

      "[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 "

  2. My comp was connected to LAN with the ip of but I am getting the following issue

    ./udp-broadcast-server &
    [1] 29353
    Success: Bad server address
    [1]+ Exit 1 ./udp-broadcast-server