Sockets and Streams

The java.net package provides an object-oriented framework for the creation and use of Internet Protocol (IP)[5] sockets. In this section, we’ll take a look at these classes and what they offer.

IP Addressing

Before communicating with another party, you must first know how to address your messages so they can be delivered correctly. Notice that I didn’t say that you need to know where the other party is located—once a scheme for encoding a location is established, I simply need to know my party’s encoded address to communicate. On IP networks, the addressing scheme in use is based on hosts and port numbers.

A given host computer on an IP network has a hostname and a numeric address. Either of these, in their fully qualified forms, is a unique identifier for a host on the network. The JavaSoft home page, for example, resides on a host named www.javasoft.com, which currently has the IP address 204.160.241.98. Either of these addresses can be used to locate the machine on an IP network. The textual name for the machine is called its Domain Name Services (DNS) name, which can be thought of as a kind of alias for the numeric IP address.

In the Java API, the InetAddress class represents an IP address. You can query an InetAddress for the name of the host using its getHostName() method, and for its numeric address using getAddress() . Notice that, even though we can uniquely specify a host with its IP address, we do not necessarily know its physical location. ...

Get Java Distributed Computing now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.