Using Communication Services with Sockets
The Communication Services Layer cannot use socket objects directly because RWSocket is not derived from RWPortal. The Communication Services Layer can use RWSocketPortal, though, because it is derived from RWPortal. In addition, if a portal is not available, a temporary portal can be used.
Example 14 writes the current date to a socket that is obtained from a legacy C application.
Example 14 – Using RWSocketPortal to write into a socket
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. It is set by the Networking package to represent the type of a legacy socket. It works for both Windows and UNIX applications.
//2 Constructs an object that wraps the C socket.
//3 Builds a portal to the socket’s communication channel. The second argument names the object that closes the socket channel. It is set to Application, which means that the application closes the socket and the RWSocketPortal destructor should not close the socket. If the second argument were set to Portal (the default), then the destructor of the RWSocketPortal would close the socket.
//4 Constructs an output stream that uses the socket as the destination for its byte stream.
//5 Writes the current date into the socket. The default constructor for RWDate builds 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. Only the original socket descriptor is left in the routine that invoked this function.