Since RWSocket is not derived from RWPortal, the communication services layer cannot use socket objects directly. If code treats the socket as an RWSocketPortal there is not a problem, since RWSocketPortal is derived from RWPortal. Even if you don't have a portal, there is still a way: you construct a temporary portal, and use that. Let's see how this works.
Suppose an application has a socket, represented as the integer fd, obtained from a legacy C application. You want to use the iostreams module to write the current date into the socket. Here's a function that does this.
void spewDate(SOCKET fd) //1 { RWSocket sock(fd); //2 RWSocketPortal portal(sock,RWSocketPortal::Application); //3 RWPortalOStream strm(portal); //4 strm << RWDate() << endl; //5 } //6
//1 | The socket parameter is of type SOCKET. On most systems, SOCKET is a typedef for integer. This is set by the net library to represent the type of a legacy socket. It works for both Windows and Unix applications. |
//2 | Construct a net library object that wraps the C socket. |
//3 | Build a portal to the socket's communication channel. The second argument indicates who closes the socket channel. Here it is set to Application, meaning that the application closes the socket, so that the RWSocketPortal destructor should not close the socket. If it were set to Portal (the default), then the destructor of the RWSocketPortal would close the socket. |
//4 | Construct an output stream that uses the socket as the destination for its byte stream. |
//5 | Write the current date into the socket. The default constructor for RWDate is used to build a temporary object representing today's date. |
//6 | When the scope of the function exits, the output stream, portal stream, and portal object are all destroyed. All that's left is the original socket descriptor, back in the routine that invoked this function. |
©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.