RWSocketPortal
When you use the Essential Networking Module, you are usually either connecting to a remote server as a client, or waiting for remote clients to connect to you as a server. For either task, you use RWSocketPortal. We already used RWSocketPortal when connecting to a remote server in A Simple Code Example. Here's another example:
 
RWSocketPortal sp(RWInetAddr(80, "www.roguewave.com")); //1
In //1, we create a connection to www.roguewave.com, port 80, using class RWSocketPortal. Notice that we initialize the RWSocketPortal with an RWInetAddr object. RWInetAddr encapsulates an Internet address and can be constructed using a port and a host, as shown here.
Once we have created the connection using RWSocketPortal, it can work with the familiar C++ iostreams to send and receive data. Let’s continue the example:
 
RWSocketPortal sp(RWInetAddr(80, "www.roguewave.com")); //1
RWPortalIStream istr(sp); //2
RWPortalOStream os(sp); //3
os << "GET / HTTP/1.0\r\n" << flush; //4
while(istr.good()) { //5
RWCString data;
data.readLine(istr);
cout << data << flush;
}
On //2 and //3, we use RWPortalIStream and RWPortalOStream to give us a high-level iostream interface to our network connection. These classes derive from istream and ostream, respectively, and can be used anywhere a standard iostream is used.
On //4, we write our HTTP request. Finally, in the while loop beginning on //5, we read each line of the response using RWCString::readLine(), a member function that reads a line from the given stream into the RWCString, and echo it to cout. That’s all there is to it. Of course, we could write an even simpler version of this program by using the prebuilt HTTP classes in the Internet Protocols Module, but this example shows how easy it is to use classes of the Essential Networking Module alone. For more information on transferring data using streams, see Chapter 5, Streams.