java.net

The java.net package provides system-independent network communication functionality. It allows Java programs to use TCP or UDP to communicate over the Internet. The classes URL, URLConnection, Socket, and ServerSocket use TCP. DatagramPacket, DatagramSocket, and MulticastSocket use UDP.

TCP (Transport Control Protocol) is a connection-based protocol that provides a reliable flow of data between two computers. When two applications want to communicate to each other reliably, they establish (and maintain) a connection using TCP. Like the phone company, TCP guarantees that data sent from one end of the connection actually gets to the other end, and in the same order it was sent. Otherwise, an error is reported.

UDP (User Datagram Protocol) sends independent packets of data, called datagrams, from one computer to another with no guarantees about arrival. UDP is not connection-based like TCP. Sending datagrams is much like sending a letter through the postal service: the order of delivery is not important and is not guaranteed, and each message is independent of any other.

For many applications, the guarantee of reliability is critical. For other applications (like a time service or a "ping" utility) the guarantee is not necessary, or, the performance overhead may be prohibitive.

Data transmitted over the Internet is accompanied by addressing information that identifies the computer and the port for which it is destined. The computer is identified by its 32-bit IP address, which the IP protocol uses to deliver data to the right computer on the network. Ports are identified by a 16-bit number, which TCP and UDP use to deliver the data to the right application.

In a connection-based protocol such as TCP, a server application binds a socket to a specific port number. This has the effect of registering the server with the system to receive all data destined for that port. A client can then "rendezvous" with the server at the server's port.

[References. Flanagan97b, pp185-220. Horstmann98, pp133-185. Heller, pp492-592]