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.
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;
}
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