Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

http server

How many lines does it take to write a simplest http server using Java? What are the most fundamental functions required for an http server?
0
txholdem
Asked:
txholdem
  • 2
  • 2
1 Solution
 
megloffCommented:
Not many lines.... but this is always relative.
For a simple Web server, You need to programm the following loop:

- wait for a socket connection
- accept the connection ( bind it)
- handles the connection and request in an new thread
- open the reqested file and send them to the client
- close the socket and terminate the thread

Hope this helps

Example Code ( but without filehandling ):

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

class Webserver
{
 public void start()
 {
   try
   {  // initialise server socket on specified port
     ServerSocket ss = new ServerSocket(8001);
     // loop
     for(;;)
     {  // wait for client request
     Socket sc = ss.accept();
     // create new thread that handles the request          ClientThread t = new ClientThread(sc);
     // thread start
     t.start();
     }
   }
   // Exception handling    
   catch (IOException e)
   {}
}
     
}


class ClientThread extends Thread
{  // member
   private Socket m_s;
   // Constructor
   public ClientThread( Socket s)
   { m_s = s; }
   // overloaded method
   public void run()
   {
        try
     {      
          // initialise Input, read request  
          BufferedReader reader = new BufferedReader(
       new InputStreamReader( m_s.getInputStream()) );
          // initialise Output, do response
        PrintWriter writer = new PrintWriter(
new OutputStreamWriter( m_s.getOutputStream()) );

     // create HTTP Header, so Browser of the client
        // think its an HTTP Connection                    writer.print("HTTP/1.0 20\nContent-Type: text/plain\n\n");
     // write HTML Content to the Client
        // read here instead the request and open
        // the req. file and
        // and send them to the client          
     writer.print("<HTML><B>Hello</B></HTML>");
     writer.flush();                        
     // close Connection    
     writer.close();
     reader.close();
     m_s.close();
      }
      // exceptionhandling
      catch(IOException e)
      {}
  }
}
0
 
txholdemAuthor Commented:
What makes this an HTTP server, rather than just any other server? I do not see anywhere in the code that specifies the HTTP protocol. Will the connection be using TCP/IP?
0
 
megloffCommented:
; ).... HTTP its a simple Text Protocole it bases like the DOS on Commands with some Arguments. TCP / IP is only for transering the commands from one machine to another (exactly from n application to another) For learning a good example... send the request back from the server to the client ... so you will see how the HTTP works. And after that implement a client browser ...so you will see how any webserver will work.

I could send you these  applications if you want ... but for that open a new question and give me some points.. ; )

For more informations have a look at the HTTP standartisation http://www.faqs.org/rfcs/rfc2616.html

Of course the HTTP uses the TCP/IP like all the others protocols of the Web (FTP, HTTPS, Gopher, telnet, wap... ) because the Network is based on an hierarchy of protocols (also called OSI layers) ... and this protocols have been developing step by step...so in  some years there will exists new protocols that are based on other protocols and so on... welcome to the web...; )
0
 
txholdemAuthor Commented:
>send the request back from the server to the client ... so you will see how the HTTP works.

I thought that's how TCP/IP works. The only difference is that HTTP server sends some formatted text using HTML or the like?

>give me some points.. ; )

if you'd responded faster, I would have given you an 'A' which automatically gives you more points. welcome to EE!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now