Java Sockets from the Server Side
The BSD operations socket(), bind() and listen() for a server-side socket are subsumed in a constructor for the ServerSocket class:
ServerSocket s = new ServerSocket(port) ;
Here port is the integer port number, such as 80 (if you are writing a Web server), on which the server will listen.
Next the Java server will call the accept() method and wait for clients to connect to it. accept() returns an ordinary socket, completing the socket-pair for the connection:
Socket connection = s.accept() ;
After processing the request, the client goes back to waiting on accept(), for new client requests.
- Real servers fork a thread or process to deal with the request, and return immediately to waiting for the next client connection.