O'Reilly logo

Java™ Phrasebook by Timothy Fisher

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

Creating a Server and Accepting a Request

public static final short PORT = 9988;
											ServerSocket server = new ServerSocket(PORT);
											while ((clientSock = server.accept( )) != null) {
											// Process client request
											}

In this phrase, we use a ServerSocket instance to create a server listening on port 9988. We pass the port that we want the server to listen on to the constructor of the ServerSocket. Once the server socket is created, we call the accept() method to wait for a client connection. The accept() method blocks until a connection with a client is made. When a client connection is made, a new Socket instance is returned.

If a security manager is being used, the security manager’s checkAccept() method is called with clientSock.getInetAddress().getHostAddress() ...

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