Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

17.3 Greeting Protocol

We need a concrete example to show how we use our receptionists. In this section, we define a simple client-server protocol, the greeting protocol, for this purpose. The greeting protocol is simple enough to be easily understood, yet complex enough to serve as an adequate demonstration.

Here's how the greeting protocol works. The greeting server has a well known address where it can be reached. By tradition, this address is TCP port 3010 on the server's home machine. Rogue Wave's greeting server lives at net.roguewave.com:3010, port 3010 on the net library home machine. Our example greeting server always attempts to brighten a client's day by responding with whatever greeting the previous client gave. First, let's suppose that you call the greeting server and send it a cheery salutation like "Hey dude!" followed by a newline character. The previous client called from Australia, so the server responds to you with "G'day mate!" The next client, calling from Waterloo, Canada, supplies the greeting "How's it going, eh." The server, remembering what you said, tells the client "Hey dude!" The following comic shows life from the server's point of view.

Figure 16 -- Greeting protocol comic

17.3.1 Client Application

Here's a net library client application that exercises the greeting protocol.

Here's how the client program works:

//1The client connects to the server at address addr and sends it the salutation salutation. The details of initializing these objects are not discussed in this tutorial.
//2This line connects portal to the greeting server.
//3Send our greeting. The sendAtLeast() call is guaranteed to send the whole string provided as its argument. If necessary, it sends multiple packets to ensure that the entire string gets sent.
//4Build a C++ input stream to receive the server's response. Using an istream allows us to use the RWCString member function readline() on the next line.
//5Read the single line containing the server's response. The actual application code would now likely do something with the response, for example print it.
//6If any of the net library calls used above fail, they throw an exception. This could happen, for example, if the greeting server is not running or if the host machine cannot be reached, perhaps because one of your colleagues tripped over your network cable and disconnected it. Rather than require you to check each net library call for an error condition, exceptions are used so that you can catch all errors in one place. In our client program, any troubles in the network code cause the flow of control to go to the catch block defined on this line. When an error does occur we just print out the error message and let the program end. Any open communication channels are automatically closed by the net library object destructors.

Previous fileTop of DocumentContentsIndexNext file

©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.