O'Reilly logo

Java RMI by William Grosso

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Sockets

Sockets are an abstraction for network connections that first appeared on Unix systems in the mid-1970s. In the intervening 25 years, the socket interface has become a cornerstone of distributed programming. Java supports sockets with the classes and interfaces defined in the java.net package.

Specifically, java.net contains two classes that are the core Java classes used when reliable communication between two different processes is necessary: Socket and ServerSocket. They have the following roles:

Socket

Enables a single connection between two known, established processes. In order to exchange information, both programs must have created instances of Socket.

ServerSocket

Manages initial connections between a client and a server. That is, when a client connects to a server using an instance of Socket, it first communicates with ServerSocket. ServerSocket immediately creates a delegate (ordinary) socket and assigns this new socket to the client. This process, by which a socket-to-socket connection is established, is often called handshaking.[7]

Another way to think of this: sockets are analogous to phone lines; ServerSockets are analogous to operators who manually create connections between two phones.

Creating a Socket

In order to create a socket connection to a remote process, you must know two pieces of information: the address of the remote machine and the port the socket uses.

Addresses are absolute—they specify a single computer somewhere on your ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required