Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

13.3 Socket Address Base Class

The root of the socket address class hierarchy is RWSockAddrBase, an abstract base class that contains the member functions that the net library classes need to recognize and use a socket address. Most of the net library interfaces that need a socket address-the interfaces for connecting a socket client to a server, for example-take a const RWSockAddrBase& as a parameter. This means that you can use a reference to any of the classes derived from RWSockAddrBase as an argument to these functions.

Since RWSockAddrBase is an abstract class, you can't instantiate an RWSockAddrBase object directly-instead you instantiate the appropriate derived class. When you know the address family when you are writing the code, you can use an object of the type appropriate to that family.

For example, to create an Internet family address, you would instantiate an RWInetAddr object, like this:

13.3.1 Preparing Socket Addresses

Some socket address objects have interfaces that require information that can only be obtained by using the network to communicate with other machines. For example, you can get the host name associated with an Internet address like this:

The call in line //1 may require a network access to look up the name. This may cause the thread of control to block while the network access occurs. While this may be fine most of the time, sometimes you may want fine control over when blocking may occur, and would like to avoid this situation.

The socket address classes have been designed so that all network access is done by a single virtual function: RWSockAddrBase::prepare(). This way, you can, if you want to, explicitly control when use of an address object might cause the thread of control to block. By calling prepare() right after an address is constructed, you guarantee that any future use of that address will not cause the thread of control to block. For example, you could restructure the above code like this:

Now, the code on line //2 will no longer hit the network, and therefore cannot block, since the call to prepare() on line //1 has already looked up all the information for that address.

13.3.2 Printing Socket Addresses

You can use the function RWSocket::id() to get an ASCII representation of the socket. For example, suppose that you have an address for the SMTP (Simple Mail Transfer Protocol) port on Rogue Wave's mail gateway, constructed using

If we print this, using

the output is

We would get the same output using the address itself:

The amount of output can be tailored somewhat by supplying an optional argument to the id() function. The argument is an integer between 0 and 9, and (in general) controls the amount of output-the higher the argument, the more descriptive the output. For example, addr.id(9) returns the string

The following table includes guidelines for how id behaves and sample output for an Internet stream address. Other address types will have different formatting details, although the guidelines in the first few columns will still be followed.

Table 10 -- Behavior of id and sample output for an Internet stream address

 
Parameter provided to id()May access networkAlways includes family fieldSample output for id(parameter) given RWInetAddr( "198.68.9.6:mail" )
0(default)
   
198.68.9.6:mail
1
    198.68.9.6:mail
2
    inet:stream:198.68.9.6:mail
3
X
  roguewave.com:25
4
X
  roguewave.com(198.68.9.6):25(smtp)
5
X
  roguewave.com(198.68.9.6):25(smtp)
6
X
X
inet:stream:roguewave.com:25
7
X
X
inet:stream:roguewave.com(198.68.9.6):25(smtp)
8
X
X
inet:stream:roguewave.com(198.68.9.6):25(smtp)
9
X
X
inet:stream:roguewave.com(198.68.9.6):25(smtp,mail)


Previous fileTop of DocumentContentsIndexNext file

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