Essential Networking Module User’s Guide : PART II The Networking Package : Chapter 6 Socket Addresses : Using Internet Addresses
Using Internet Addresses
Internet addresses are the most popular socket address family. They identify communication endpoints on an internet, which is a network of computers that communicate using the TCP/IP suite of protocols. The “capital-I” Internet is the best known internet, but the Internet family of addresses is also used for thousands of smaller networks.
An internet address has two parts:
• Host, which identifies the computer
• Port, which identifies where to send information to the host
An internet address is represented in the Networking package as an RWInetAddr. As shown in Figure 10 , an RWInetAddr object contains two objects: an RWInetHost to represent the host, and an RWInetPort to represent the port.
Figure 10 – An RWInetAddr object
Constructing a Host
A host is identified by either a symbolic name, like net.roguewave.com, or by an Internet Protocol (IP) address, like 198.68.9.6. A particular host can have more than one symbolic name. A host address usually has only one IP address, although it is possible for a host to have more than one address.
You can construct a host object by specifying either the IP address or host name as a string, as shown in the following examples.
 
RWInetHost host1("net.roguewave.com");
RWInetHost host2("198.68.9.6");
Once a host object is constructed, you can use the member functions of RWInetHost to determine characteristics of the host.
Example 9 prints information about a host.
Example 9 – Printing information about a host
#include <rw/rstream.h>
#include <rw/network/RWInetAddr.h>
 
int main()
{
RWWinSockInfo info;
RWInetHost host = RWInetHost::me(); //1
cout << host.getName() << endl; //2
cout << RWInetHost::addressAsString(host.getAddress())
<< endl; //3
return 0;
}
//1 Returns a host object representing the host on which the program is running.
//2 Prints the primary symbolic name of the host.
//3 RWInetHost::getAddress() returns the four-byte IP address of the host. Usually, this address is printed a byte at a time using dotted decimal notation. The static function RWInetHost::addressAsString converts the address from a long integer to a string in dotted decimal format.
Building Internet Addresses
Example 10 shows how to build an Internet address object by specifying the port and then the host.
Example 10 – Building internet addresses
RWInetAddr addr1( 3010, "net.roguewave.com" ); //1
RWInetAddr addr2( 3010 ); //2
RWInetAddr addr3( "stream:net.roguewave.com:3010" ); //3
//1 Includes both a port and a host. Automatic type conversion turns the integer 3010 into an RWInetPort, and turns the string into an RWInetHost.
//2 When the host name is omitted, the special host ANY is used. This address connects to any available IP address on the current computer.
//3 Sends the address as a string. The format is the socket type (stream or dgram), followed by the host identifier, followed by a port identifier. The fields are separated by colons. You can omit the socket type and host, which means that both net.roguewave.com:3010 and 3010 are allowed. The default socket type is stream. If only the port number is used, the host defaults to ANY.