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:
- The ServerSocket object is instantiated by the server with a port number for communication:
ServerSocket server = new ServerSocket( PORT );
- 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();