tags:

views:

50

answers:

1

I am trying to implement a web-server which serves static pages only. Here is the code of my ultra mini web server.

import java.io.*;
import java.net.*;

class MyWebServer{

    static ServerSocket serSocket = null ;

    static{
     try {
      serSocket = new ServerSocket(80) ;
      System.out.println("Server started successfully....\n\n") ;
     } catch (IOException io) {
      System.out.println( io.getMessage() );
      System.exit(1) ;
     }
    }

    public static void main(String []args){

     while(true){
      try {
       new Thread( new ServingThread( serSocket.accept() ) ).start() ;
      } catch (IOException io) {
       System.out.println(io.getMessage());
      }
     }
    }
}

class ServingThread implements Runnable{
    private Socket socket ;

    public ServingThread(Socket socket){
     this.socket = socket ;
     System.out.println("Receives a new browser request from " + socket);
    }

    public void run() {
     BufferedReader in = null ;
     PrintWriter out = null ;

     try {

      //reading request headers from browser starts here
      in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      String str = ".";
            while (!str.equals("")){
             str = in.readLine();
             //System.out.println(str);
            }
            System.out.println("\n\n");
            //reading request headers from browser ends here


      //writing output on outputstream starts here
      out = new PrintWriter( socket.getOutputStream(), true ) ;

      int i = (int) ( Math.random() * 10) ;
      out.println(i) ;
      System.out.println(i) ;
      //writing output on outputstream ends here

      socket.close() ;
      System.out.println("Request successfully fulfilled.\n") ;
     }
     catch (IOException io) {
      System.out.println(io.getMessage());
     }
    }
}

It works fine, but some times (not always)

  • my mozilla firefox client automatically sends a request, when i add a new tab (Ctrl + T) or closes an existing tab.
  • When i request for http://localhost/ , single digit gets displayed on my browser. But the server's console shows that it get 2 or 3 requests, so, it shows 2 or 3 numbers accordingly.

I googled a lot but don't get an satisfactory answer for this.

Any thoughts/explanations are highly appreciated. :)

+1  A: 

First of all, I'm assuming you're doing this as an exercise to learn about sockets / http / etc... Because if you're really trying to implement a web server, you should seriously consider using an existing one or at least looking at its source - there's a LOT of stuff involved in even the most simplistic HTTP server implementation.

That said, your "server" :-) doesn't implement HTTP properly (doesn't return status code, for one) so browser's behavior is really undetermined here - it may attempt to retry the same request multiple times or it may not. Caching issue may be at play here too - once you do implement HTTP, you'll need to send appropriate caching headers if you don't want "new tab" to repeat request.

ChssPly76
any online reference for simplistic java implemented web-servers with source code. :-) .
rits
ChssPly76
thnx ChssPly. I am already done with the second link. It also don't implement HTTP properly.
rits
It does, actually. A very limited subset of HTTP protocol, of course, but it handles GET / HEAD methods and returns status codes properly.
ChssPly76