Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

14.4 Using Communications Services with Sockets

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.

//1The 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.
//2Construct a net library object that wraps the C socket.
//3Build 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.
//4Construct an output stream that uses the socket as the destination for its byte stream.
//5Write the current date into the socket. The default constructor for RWDate is used to build a temporary object representing today's date.
//6When 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.

Previous fileTop of DocumentContentsIndexNext file

©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.