Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

11.2 A Simple Client

Let's start using the net library by writing a simple network client. The code connects to a TCP/IP server, and then writes a message to the server. If you have problems getting this code to run, move on to the next section, which discusses common problems.

//1Include the header file rw/toolpro/sockport.h, which contains the declarations needed for the RWSocketPortal class. All of the Tools.h++ Professional header files are located in the rw/toolpro directory.
//2Include the Internet address header file, rw/toolpro/inetaddr.h. This declares the TCP/IP address classes, needed to connect to Internet servers.
//3The header file rw/toolpro/winsock.h includes declarations necessary for using the Winsock DLL. On the Unix operating system, this include file does nothing, but including it increases code portability.
//4If you are using a Winsock DLL, the DLL must be initialized before it is used, and cleaned up again when you are through. Defining an instance of the RWWinSockInfo class in a scope to contain the use of the net library, as we do here, does this initialization and cleanup. This is described later.

Under Unix, this line does nothing. It is still a good idea to include an RWWinSockInfo object where appropriate, in case your code ever needs to be ported to Windows.
//5Construct a socket portal, which is connected to the Internet server on net.roguewave.com at port 3010. The constructor takes as a parameter a reference to the abstract base class RWSockAddrBase. We build an RWInetAddr object in place and connect to the address specified by this object. This works because RWInetAddr is derived from RWSockAddrBase.

If you are not on the Internet, you may want to change this line to connect to a server that you can reach. Port 7 or 9 (the echo discard servers) of a Unix or NT machine may be a good choice. Your system administrator or resident network guru can usually suggest a local server for experiments.
//6Send a message to the server. There are several member functions for sending data, depending on the format of the information and whether or not it is acceptable to send only part of the data. The sendAtLeast() member function calls the underlying communication system's send() function multiple times, if necessary to make sure that all data is sent.

The next section explains the example in detail.

11.2.1 Where's the Portal?

You may have noticed that the example program doesn't use RWPortal. Instead, it uses an RWPortal subclass, RWSocketPortal. Aside from the constructor, this section uses only member functions available in the base class RWPortal. A full discussion of the relationship between RWPortal and RWSocketPortal is in Section 11.5.1, "The Portal and PortalImpl Classes." For now, remember that almost everything you learn about RWSocketPortal also applies to its base class, RWPortal.


Previous fileTop of DocumentContentsIndexNext file

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