Interview Questions

public ServerSocket(int port, int queueLength) throws IOException, BindException

Java Network Programming - Sockets for Servers


(Continued from previous question...)

public ServerSocket(int port, int queueLength) throws IOException, BindException

public ServerSocket(int port, int queueLength) throws IOException, BindException

This constructor creates a ServerSocket on the specified port with a queue length of your choosing. If the machine has multiple network interfaces or IP addresses, then it listens on this port on all those interfaces and IP addresses. The queueLength argument sets the length of the queue for incoming connection requests--that is, how many incoming connections can be stored at one time before the host starts refusing connections. Some operating systems have a maximum queue length, typically five. If you try to expand the queue past that maximum number, the maximum queue length is used instead. If you pass 0 for the port number, the system selects an available port.

For example, to create a server socket on port 5,776 that would hold up to 100 incoming connection requests in the queue, you would write:

    try {
ServerSocket httpd = new ServerSocket(5776, 100);
    }
    catch (IOException e) {
      System.err.println(e);
    }

The constructor throws an IOException (specifically, a BindException) if the socket cannot be created and bound to the requested port. An IOException when creating a ServerSocket almost always means one of two things. Either the specified port is already in use, or you do not have root privileges on Unix and you're trying to connect to a port from 1 to 1,023.

public ServerSocket(int port, int queueLength,
InetAddress bindAddress) throws BindException, IOException

This constructor, which is available only in Java 1.1 and later, creates a ServerSocket on the specified port with the specified queue length. This ServerSocket binds only to the specified local IP address. This constructor is useful for servers that run on systems with several IP addresses (a common practice at web server farms) because it allows you to choose the address to which you'll listen. That is, this ServerSocket listens only for incoming connections on the specified address; it won't listen for connections that come in through the host's other addresses. The other constructors bind to all local IP addresses by default.

For example, www.yousite.com is a particular SPARCstation. It's connected to the Internet with the IP address 789.2.254.81. The same SPARCstation is also called www.xxx.org, but with a different IP address (789.2.254.82). To create a server socket that listens on port 5,776 of www.yousite.com but not on port 5,776 of www.xxx.org, you would write:

 try {
ServerSocket httpd = new ServerSocket(5776, 10, 
 InetAddress.getHostByName("www.yousite.com"));
    }
    catch (IOException e) {
      System.err.println(e);
    }

The constructor throws an IOException (again, really a BindException) if the socket cannot be created and bound to the requested port. A BindException when creating a ServerSocket almost always means one of two things. Either the specified port is already in use, or you do not have root privileges on Unix and you're trying to connect to a port from 1 to 1,023.

Accepting and Closing Connections

A ServerSocket generally operates in a loop that repeatedly accepts connections. Each pass through the loop invokes the accept( ) method. This returns a Socket object representing the connection between the remote client and the local server. Interaction with the client takes place through this Socket object. When the transaction is finished, the server should invoke the Socket object's close( ) method and get ready to process the next incoming connection. However, when the server needs to shut down and not process any further incoming connections, you should invoke the ServerSocket object's close( ) method.

public Socket accept( ) throws IOException

When server setup is done and you're ready to accept a connection, call the ServerSocket's accept( ) method. This method "blocks": it stops the flow of execution and waits until a client connects. When a client does connect, the accept( ) method returns a Socket object. You use the streams returned by this Socket's getInputStream( ) and getOutputStream( ) methods to communicate with the client. For example:

ServerSocket server = new ServerSocket(5776);
while (true) {
Socket connection = server.accept(  );
OutputStreamWriter out 
 = new OutputStreamWriter
     (connection.getOutputStream(  ));
out.write("You've connected to this server.
      Bye-bye now.\r\n");        
 connection.close(  );
    }

(Continued on next question...)

Other Interview Questions