Linux Socket Programming by Example - Warren Gay


Compiling and Running the Demonstration

To compile the demonstration project, obtain the source code for Chapter 18 and type make shown as follows:

001 $  <$nopage>make <$nopage>
002 gcc -c -g -Wall -Wreturn-type -D_GNU_SOURCE  qserve.c
003 gcc -c -g -Wall -Wreturn-type -D_GNU_SOURCE  csvparse.c
004 gcc -c -g -Wall -Wreturn-type -D_GNU_SOURCE  msgf.c
005 gcc -c -g -Wall -Wreturn-type -D_GNU_SOURCE  load.c
006 gcc -c -g -Wall -Wreturn-type -D_GNU_SOURCE  gettick.c
007 gcc -c -g -Wall -Wreturn-type -D_GNU_SOURCE  bcast.c
008 gcc -c -g -Wall -Wreturn-type -D_GNU_SOURCE  connect.c
009 gcc -c -g -Wall -Wreturn-type -D_GNU_SOURCE  misc.c
010 gcc -c -g -Wall -Wreturn-type -D_GNU_SOURCE  mkaddr.c
011 gcc -o qserve qserve.o csvparse.o msgf.o load.o gettick.o bcast.o connect.o misc.o 
mkaddr.o 013 gcc -c -g -Wall -Wreturn-type -D_GNU_SOURCE mktwatch.c 014 gcc -o mktwatch mktwatch.o mkaddr.o 015 $

This produces two executables:

  • The qserve executable, which is the quotation server program

  • The mktwatch executable, which is the broadcast listener client

The following sections describe how to run these programs.

Starting the qserve Quotation Server

If you have no network adapter card installed, simply use the default loopback parameters ( is the default broadcast address and port number used). The defaults are used when the server is started as follows:

001 $  <$nopage>./qserve & <$nopage>
002 [1] 798
003 $

If you have a network adapter card, you can broadcast on one interface as follows (here, the interface broadcast address and port number used is

$ ./qserve -b &
[2] 800

In both cases, the server is started, which should begin to contact the Web site and start broadcasting. Before launching the client, you might want to start another window to watch for server errors in your /var/log/messages log file. You can do this by entering the following (as root if log file permissions require):

# tail -f /var/log/messages
Nov 26 16:34:10 pepper qserve: 34 tickers loaded.
Nov 26 16:34:37 pepper qserve: Unknown Ticker: 'NEC'

In the log output, observe that 34 ticker symbols were loaded from the tickers.rc file, and that symbol NEC was not known by the server. Any other server messages will be logged to the warning, error, and information logs.


If you cannot find the log file as shown, examine your /etc/syslog.conf file to determine how your logs are configured on your system. If necessary, consult the syslog.conf(5) man page for details about the file format.

Starting the mktwatch Client

The client program can be started in any user's session. You simply must be certain that the broadcast address agrees with the way qserve was started. If you provided no command-line arguments to use the qserve defaults, then do the same for the client:

001 $ . <$nopage>/mktwatch

If you specified a broadcast address to qserve, then supply the same address to the client program with the -b option as follows (substituting your broadcast address and port):

001 $ . <$nopage>/mktwatch -b

Be patient after starting the client program. The server program will pause 20 seconds or more between updates. After it wakes up, it will go through the list of tickers that it must update.


If you don't see any output within one or two minutes, then check to see that you have the server running. If it is, then you have probably not specified addresses correctly.

An easy error to make is to forget the port number that follows the IP number (:9777 in the examples shown). If this number is omitted, then zero is assumed by the mkaddr.c module, which allows the operating system to pick any free port number instead!

Whether you used the local loopback interface default or used your local area network, you should be able to obtain results that look something like this:

001 $  <$nopage>./mktwatch -b <$nopage>
002 CHP.TO     20.000  11/26/1999   3:58PM  +0.550   19.750  20.500  19.600     7657
003 AMZN       93.125  11/26/1999   1:01PM  +5.875   91.062  95.125  90.500 11496000
004 AMD        27.688  11/24/1999   1:02PM  +0.438    0.000  28.125  26.750  1880400
005 CSCO       93.188  11/26/1999   1:01PM  +0.750   95.250  95.375  92.812 12171000
006 DELL       42.938  11/26/1999   1:01PM   -0.37   43.562  43.938  42.750 11280900
007 EMC        90.312  11/26/1999   1:00PM  +1.000   89.562  90.375  89.375  1739800
008 GTW        79.062  11/26/1999   1:01PM  -0.188   79.688  79.688  78.938   391700
009 MOT        119.031 11/26/1999   1:48PM  +2.094  118.562 120.688 117.750  1440400
010 NCR        32.938  11/26/1999   1:01PM  -0.062   32.750  32.938  32.500   134800
011 NN         24.312  11/26/1999   1:00PM  +1.875   23.375  24.625  23.062  1602000
012 NOK        146.750 11/26/1999   1:18PM  +7.500  144.938 147.125 144.500  1935000
013 NT         81.688  11/26/1999   1:00PM  +3.383   81.000  82.188  79.562  2226900
014 ORCL       73.625  11/26/1999   1:01PM  +1.812   71.000  73.875  70.812  4617500
015 XRX        28.188  11/26/1999   1:00PM  +0.000   28.312  28.750  28.000  2428600
016 YHOO       226.875 11/26/1999   1:01PM  -4.125  233.000 235.250 225.375  2008900
017 RHAT       213.500 11/26/1999   1:00PM +44.562  184.000 219.938 181.000  2296600
018 COB        8.312   11/24/1999   1:00PM  +1.188   0.000    8.375   7.312   316300
019 ATYT       10.375  11/26/1999  12:59PM  +0.438   10.250  10.375  10.125    80800
020  <$nopage>

This output should continue to scroll as the tickers become updated. The time between updates will increase to a maximum of five minutes if the server is allowed to run all day.

If the Service Changes

If the service should move or change in some way, you'll need to rediscover the service by checking out its Web pages. After you determine the new server address (if it changes), you can specify that on the qserve command line using the -a option, as follows (with or without the additional -b option):

$ ./qserve -a &
[1] 821


Substitute the hostname for and the port number (port 80 for the Web). The port number must be supplied—it is not optional. Check the /var/log/messages file for any errors.

If you see a number of data format errors being reported in the log file, then this suggests that the spreadsheet data format has changed. This will require some code change if this has happened. Module gettick.c—lines 52 to 69—is a good place to start making changes. The sprintf(3) statement starting in line 96 might also need to be adjusted.


The Internet remains a fun place to be if people don't abuse it. Please be considerate of when running this example program. Please do not short-circuit the program to poll its server too frequently. This will spoil the fun for everyone.


Index terms contained in this section

      qserve quotation
      stock market quotes 2nd 3rd 4th 5th stock market quote example
ktwatch client program
      starting 2nd 3rd
      stock quotes 2nd 3rd 4th 5th
      quote service example 2nd 3rd 4th 5th
serve quotation server
      mktwatch client program 2nd 3rd
      qserve quotation server
tock market quotes
      obtaining 2nd 3rd 4th 5th
      qserve quotation server
      quote service example 2nd 3rd 4th 5th
uotation server (qserve)
uote service problem example 2nd 3rd 4th 5th
uotes (stock market) 2nd 3rd 4th 5th
      fetch proceedure 2nd 3rd 4th 5th
      qserver quotation server
      quote service problem 2nd 3rd 4th 5th