The accept method A Simple Web Server

1. Create an instance of ServerSocket . As part of doing so, you will supply a port on which ServerSocket listens for connections. 2. Call the accept method of ServerSocket .Once you do this, the server program simply waits for client connections.

2.3.1 The accept method

The key to using ServerSocket is the accept method. It has the following signature: public Socket accept throws IOException There are two important facts to note about accept . The first is that accept is a blocking method. If a client never attempts to connect to the server, the server will sit and wait inside the accept method. This means that the code that follows the call to the accept method will never execute. The second important fact is that accept creates and returns an instance of Socket . The socket that accept returns is created inside the body of the accept method for a single client; it encapsulates a connection between the client and the server. Therefore, any server written in Java executes the following sequence of steps: 1. The server is initialized. Eventually, an instance of ServerSocket is created and accept is called. 2. Once the server code calls accept , ServerSocket blocks, waiting for a client to attempt to connect. 3. When a client does connect, ServerSocket immediately creates a new instance of Socket , which will be used to communicate with the client. Remember that an instance of Socket that is returned from accept encapsulates a connection to a single client. [ 6] ServerSocket then returns the new Socket to the code that originally called accept . [ 6] Setting up this socket involves some communication with the client; this communication which is completely hidden inside the socket libraries is again called handshaking.

2.3.2 A Simple Web Server

To illustrate how to use ServerSocket , well write a simple web server. Its not a very impressive web server; it doesnt scale very well, it doesnt support secure sockets, and it always sends back the same page. On the other hand, the fact that it works at all and can be written in so few lines of code is a testament to the power of sockets. The main method for our web server is contained in the com.ora.rmibook.chapter2.WebServer class. The heart of our web server is the startListening method: public void startListening { ServerSocket serverSocket; try { serverSocket = new ServerSocket80; } catch IOException e {return;} whiletrue { try { Socket client = serverSocket.accept ; wait here processClientRequestclient; bad design--should handle requests in separate threads and immediately resume listening for connections client.close ; } catch IOException e{} } } This application works exactly as described in the preceding comments: an instance of ServerSocket is created, and then accept is called. When clients connect, the call to accept returns an instance of Socket , which is used to communicate with the client. The code that communicates with the client does so by using the sockets input and output streams. It reads the request from the sockets input stream and displays the request in a JTextArea . The code that reads the request explicitly assumes that the client is following the HTTP protocol and sending a valid HTTP request. [ 7] [ 7] Among other things, the readRequest method assumes that the presence of a blank line signals the end of the request. After the request is read, a Hello World page is sent back to the client: private void processClientRequestSocket client throws IOException { _displayArea.appendClient connected from port + client.getPort + on machine + client.getInetAddress +\n; _displayArea.appendRequest is: \n; readRequestclient; sendResponseclient; } private void readRequestSocket client throws IOException { BufferedReader request=null; request = new BufferedReadernew InputStreamReaderclient.getInputStream ; String nextLine; while null=nextLine=request.readLine { Ideally, wed look at what the client said. But this is a very simple web server. if nextLine.equals { break; } else { _displayArea.append\t + nextLine + \n; } } _displayArea.append--------------------------------------- \n; return; } private void sendResponseSocket clien t throws IOException { BufferedWriter response; response = new BufferedWriternew OutputStreamWriterclient.getOutputStream ; response.write_mainPage; response.flush ; } Figur e 2- 3 is a screenshot of our web server in action, handling a request made using Netscape Navigator 6. Figure 2-3. The WebServer application Note the use of metadata here. When a web browser asks a web server for a page, it sends information in addition to what page it wants™a description of how the page should be sent and what the page should contain. In the previous example, the web browser stated what protocol is being used HTTP 1.0, what type of web browser it is Netscape 6, what sort of response is desired indicated by the two Accept lines, and the site that referred to the page being requested i.e., if you clicked on a link to request the page, the page you were on is passed to the web server as well.

2.4 Customizing Socket Behavior