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.
#include <rw/rstream.h> #include <rw/toolpro/sockport.h> //1 #include <rw/toolpro/inetaddr.h> //2 #include <rw/toolpro/winsock.h> //3 main() { RWWinSockInfo info; //4 RWSocketPortal p( RWInetAddr(3010,"net.roguewave.com") ); //5 p.sendAtLeast("Hello out there!"); //6 return 0; }
//1 | Include 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. |
//2 | Include the Internet address header file, rw/toolpro/inetaddr.h. This declares the TCP/IP address classes, needed to connect to Internet servers. |
//3 | The 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. |
//4 | If 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. |
//5 | Construct 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. |
//6 | Send 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.
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.
©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.