Linux Socket Programming by Example - Warren Gay


Solving the Quote Service Problem

It is clear from the problem statement that it is desirable to get one set of quotes from the quote provider. There is no point in having all your office workers using separate TCP/IP connections to the same quote server, for the same information. This would use up valuable bandwidth to your Internet service provider. From the free quotation provider's point of view, this is also less than desirable.

One local server program could continually fetch updated stock market quotes for everyone. This information could then be broadcast to the local network for all interested parties. This is the solution that you will apply in this chapter. This application will also give you the chance to review the use of both stream sockets and datagram sockets.

Obtaining Stock Market Quotes

The data source that you'll use for market quotations will be the fine service at In this section, you'll learn how the program manages to fetch the quotation information from


The programs being presented in this chapter worked at the time of writing. It is possible, however, that the Internet service being used might have changed by the time you read this. Notes are provided later in this chapter to help you adjust these programs should this become necessary.

To determine how to obtain stock quotes, you can use your Web browser to visit (Netscape will be assumed here). There, you will be presented with a page that lets you enter a ticker symbol and press the Get Quotes button. Entering RHAT and pressing the button leads you to another page giving you the details about Red Hat Inc. Underneath the quote information, you will find a link that reads Download Spreadsheet Format. This is the gold mine!

Figure 18.1. A screen shot of Netscape after obtaining a stock quote for RHAT at

From here, you have three choices:

  • Move the mouse over the Download Spreadsheet Format link and note the URL that shows up on the status line (Netscape 4.7 shows the link URL there).

  • Select from the menu item View->Page Source. Look through the HTML code for the link that looks something like <a href="/d/ quotes.csv?s=RHAT &f=sl1d1t1c1ohgv&e=.csv">Download Spreadsheet Format</a> (find it by looking for "Download Spreadsheet Format").

  • The best way this information can be captured with Netscape is to right-click on the Download Spreadsheet Format link. From the pop-up menu, select the item Copy Link Location. This will copy the link reference to your Clipboard. Then, you can later paste this information into a file.

From this information, you will have all that you need. Test the facility with telnet as follows (remember to supply 80 after the hostname):

$ telnet 80
Connected to
Escape character is '^]'.
GET /d/quotes.csv?s=RHAT&f=sl1d1t1c1ohgv&e=.csv
Connection closed by foreign host.


The GET command is issued after you are connected, along with the strange-looking pathname and a press of the Enter key (you might need to press Enter twice). If you are successful, you get one line of spreadsheet data in return! Substituting another symbol for RHAT will yield different data.

If you are not having any luck, then check your punctuation and spelling. Accuracy is vital here. Use cut and paste from the screen if possible. If this doesn't help, then you'll need to research how is doing it presently (you might even need to start at the home page). Follow the steps outlined previously so that you'll be able to find out the new hostname and pathname required.

This is a summary of the quote fetch procedure:

  1. Connect to on port 80 (this hostname might change at a future date).

  2. Issue the GET request with the magic pathname shown (the word GET should be uppercase).

  3. A line of spreadsheet format data is returned on the same socket as a response.

  4. The socket is closed.

That is all there is to it! Remember these steps when you examine the source code listings later.

The following sections will illustrate and describe the modules that make up the server and client programs. Unfortunately, space does not output permit a full listing of the source code. The full source code and make files are available, however, at

Table 18.1 lists all the source files that you will need. Some of the more interesting ones will be listed and described within this chapter.

Table 18.1. Source Modules for the Quote Server and Client
Source File Description
Makefile The project make file.
bcast.c Implements the function that performs the quote broadcasting to the local area network.
connect.c Implements a function that connects to the remote Internet quote server.
csvparse.c Parser for the quote data that is returned.
gettick.c Fetches the quote information from the remote Internet quote server.
load.c Loads the stock market ticker symbols from file tickers.rc.
misc.c Small miscellaneous functions.
mkaddr.c The Internet address convenience function.
mktwatch.c The market watch client program. This program receives the local broadcast information and displays it.
msgf.c A module that logs server messages to the syslog logging facility.
qserve.c The module that implements the local quote server program.
quotes.h The common header file for all source modules.
tickers.rc The list of ticker symbols to inquire about. Modify this file to change the tickers to the ones that interest you.


Index terms contained in this section

      stock market quotes
ET command 2nd
      stock market quotes 2nd
      stock market quotes
      GET 2nd
ource modueles
      stock market quotes server and clients
      stock quotes 2nd 3rd 4th
      quote service example 2nd 3rd 4th
tock market quotes
      obtaining 2nd 3rd 4th
      server and clients
uote service problem example 2nd 3rd 4th
uotes (stock market) 2nd 3rd 4th
      fetch proceedure 2nd
      server and clients
      quote service problem 2nd 3rd 4th