Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

13.4 Generic Socket Addresses


NOTE: At the present, there is only one family of addresses, RWInetAddr. However, future releases of Tools.h++ Professional may add additional address families. If you follow the guidelines in this section, your code will continue to work with future enhancements to the net library.

Sometimes an application doesn't know the family of an address until run time. For example, you might want a function to return a socket address, without knowing what the family of the address is at compile time. This situation comes up a couple of times in the net library interface. For example, the RWSocket::getsockname member function returns the socket's address. If the socket is an Internet socket, it would be logical to return an RWInetAddr. If it is some other kind of socket, it would be logical to return an address appropriate for that type. Unfortunately, the function can only return one type. If we want to avoid the memory management quagmire of returning a reference or a pointer, there seems to be no good solution.

The RWSockAddr class is just what you need for these situations. Like RWSockAddrBase, RWSockAddr can represent any address family, but unlike RWSockAddrBase, RWSockAddr is a concrete class, which can be instantiated directly and used as member data in objects.

13.4.1 Constructing Generic Addresses

One way to build an RWSockAddr is to use a specification string. The string consists of a number of colon separated fields, which describe the address. The first field gives the address family; the rest of the fields vary depending on the address family. Here's how you might build the address of Rogue Wave's World Wide Web server.

You can also use the stream extraction operator to read an address from an input stream:

Anything in the address enclosed in parentheses is ignored. So if you say

then the dotted decimal IP address is ignored. This convention is used so that the output formats for socket addresses can contain auxiliary information in parenthesis and still be suitable as input formats. The parentheses may be nested.

13.4.2 How It Is Implemented

How does this work? Internally, an RWSockAddr keeps a reference counted pointer to the real address, which is an object on the heap of some type derived from RWSockAddrBase. The RWSockAddr acts as a proxy for the heap object: when you call member functions on the RWSockAddr, it forwards the calls to the object on the heap, which actually does the work.

For example, let's say you have code like this:

The objects this creates are shown in the following diagram.

Figure 11 -- Objects created by RWSockAddr example

As the proxy objects go out of scope, the reference count is decremented. When no more objects refer to the object anRWInetAddr, it is automatically deleted.


Previous fileTop of DocumentContentsIndexNext file

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