Using Generic Socket Addresses
RWSockAddr represents any address family. It is a concrete class that can be instantiated directly and used as member data in objects. You can use
RWSockAddr when you do not know the address family until run time.
NOTE >> The Networking package has one family of addresses,
RWInetAddr. However, if you follow the guidelines in this section, your application will work with any family of addresses.
Constructing Generic Addresses
You can build an
RWSockAddr in two ways:
• Use a specification string
The string consists of colon-separated fields that describe the address. The first field gives the address family. The rest of the fields depend on the address family. The following code shows how you can build the address of Rogue Wave’s World Wide Web server.
RWSockAddr addr = "inet:www.roguewave.com:80";
• Use the stream extraction operator to read an address from an input stream
RWSockAddr addr;
cin >> addr;
Anything in the address that is enclosed in parentheses is ignored. For example, in the following code:
RWSockAddr addr = "inet:www.roguewave.com(198.68.9.3):80";
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 can be nested.
Understanding How RWSockAddr Is Implemented
The
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.
Figure 9 shows the objects that are created by the following code.
RWSockAddr addr = "inet:net.roguewave.com:3010";
RWSockAddr addr2 = addr;
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.