Solved

http server

Posted on 2001-06-18
4
204 Views
Last Modified: 2010-03-31
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
Comment
Question by:txholdem
  • 2
  • 2
4 Comments
 

Accepted Solution

by:
megloff earned 30 total points
ID: 6204184
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
 
LVL 1

Author Comment

by:txholdem
ID: 6206480
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
 

Expert Comment

by:megloff
ID: 6215945
; ).... 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
 
LVL 1

Author Comment

by:txholdem
ID: 6219041
>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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count11 challenge 6 48
countHi2 challenge 7 44
DO we need Java installed on a Windows PC and WHY ? 13 78
Java Jpanels and Jframe 8 20
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now