Socket programming for TCP

The java.net.Socket class represents a socket, and the java.net.ServerSocket class represents a server socket through which the server can listen to client sockets and establish a connection with them:

The following are the steps that occur when the connection is established:

  1. The ServerSocket object is instantiated by the server with a port number for communication:
        ServerSocket server = new ServerSocket( PORT );
  1. The server calls the accept() method of ServerSocket and waits until the client connects to a given port. This is called server listening on a given port:
        Socket serverclient = server.accept();

Get Distributed Computing in Java 9 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.