Linux Socket Programming by Example - Warren Gay

< BACKCONTINUE >
159231153036212041242100244042145096184016146223183074028121223008110133023230132168118233067

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 http://finance.yahoo.com. In this section, you'll learn how the program manages to fetch the quotation information from http://finance.yahoo.com.

NOTE

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 http://finance.yahoo.com (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 http://finance.yahoo.com.
graphics/18fig01.gif

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 finance.yahoo.com 80
Trying 204.71.201.75…
Connected to finance.yahoo.com.
Escape character is '^]'.
GET /d/quotes.csv?s=RHAT&f=sl1d1t1c1ohgv&e=.csv
"RHAT",168.9375,"11/24/1999","4:00PM",0,147.5625,175.5,145.6875,3061700
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 http://finance.yahoo.com is doing it presently (you might even need to start at the http://yahoo.com 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 http://finance.yahoo.com 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 http://www.quecorp.com/series/by_example/.

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.

< BACKCONTINUE >

Index terms contained in this section

ervers
      stock market quotes
ET command 2nd
etching
      stock market quotes 2nd
lients
      stock market quotes
ommands
      GET 2nd
ource modueles
      stock market quotes server and clients
ownloading
      stock quotes 2nd 3rd 4th
roblems
      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
xamples
      quote service problem 2nd 3rd 4th