RWSocket
In the Essential Networking Module, class
RWSocket provides low-level socket functionality for TCP/IP. Internally, class
RWSocket is implemented with calls to the Berkeley sockets API provided by the operating system. You will use
RWSocket member functions directly for those times when you want to work with low-level details of your network connection, and you don't need a higher-level interface. Here's an example:
RWSocket s; //1
s.connect(RWInetAddr(80,"www.roguewave.com")); //2
s.sendAtLeast("GET / HTTP/1.0\r\n"); //3
s.shutdownwrite(); //4
On //1, we create an RWSocket using its default constructor. On //2, we connect the socket to www.roguewave.com, port 80. On //3, we use the function sendAtLeast() to send a string to the remote host, and on //4, we shut down the writing side of the socket.
This code example illustrates a perfectly legitimate use of
RWSocket appropriate to development requiring low-level control. Since such direct use can be difficult, however, the class is actually more commonly used in conjunction with the higher-level portal abstraction. Class
RWSocketPortal multiply inherits from both
RWPortal and
RWSocket. For this reason, it can provide both the easy-to-use
RWPortal interface and the
RWSocket flexibility to freely mix high-level portal usage with lower-level socket calls. For example:
RWSocketPortal sp(RWInetAddr(80, "www.roguewave.com"));
RWPortalOStream os(sp);
os << "GET / HTTP/1.0\r\n" << flush; //1
cout << "Sent request to: " << sp.getpeername() << endl; //2
On
//1,
RWPortalOStream is used to send a request to the server, using the higher-level portal interface. However, on
//2 we use the lower-level
getpeername() method, inherited from
RWSocket, to obtain the name of the host to which the
RWSocketPortal is connected.
This is a very typical use of
RWSocket. You will probably avoid using
RWSocket members directly for most tasks since you can perform them through the higher-level APIs provided in the rest of the product. If you need to work directly with the low-level details of a network connection, however, you'll use
RWSocket directly.
RWSocket is discussed in more detail in
Chapter 6, Socket Addresses.