Navigation: Programming Cookbook > Sockets Connectivity >
If we want to connect one machine to another across a network, it is essential to be able to identify machines easily. The TCP/IP Internet Protocol identifies machines by means of their IP addresses. An IP address usually looks something like 18.104.22.168. Each machine on a LAN or connected to the Internet will have a unique IP address. However, these IP addresses are difficult to remember (and sometimes may change) so it is useful to have an alternative naming scheme. TCP/IP has the concept of host names where each computer may be given a textual name. There must therefore be a mapping mechanism of host names to IP addresses and vice versa. How this works is beyond the scope of this text but, if you intend to test Sockets Connection across a network, you should find out the hostname and IP address of the computers you'll be using. You may have to contact your network administrator to find out this information.
Sockets Connection uses the InternetAddress class to represent an Internet address, either by hostname or IP address. You can also use this class for mapping between IP addresses and hostnames as and when required.
"The local machine can always be identified by the hostname -- localhost"
ia := InternetAddress host: 'localhost'.
"Display the IP address for this host"
In fact, the 'localhost' name and #[127 0 0 1] address are aliases present on all machines. You cannot use them for talking from one machine to another. You will have to substitute your own IP addresses and host names, where appropriate, in the example below.
"Display the real IP address of your machine"
(InternetAddress host: 'moll') ipAddress.
"Display a reverse lookup of host name from IP address"
(InternetAddress ipAddress: #[192 169 0 1]) host.
This latter operation can often be quite time-consuming.
In the examples that follow, we will assume that the server machine has an IP address of 22.214.171.124 and a host name of 'moll'.