Solved

http server

Posted on 2001-06-18
4
208 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

920 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

17 Experts available now in Live!

Get 1:1 Help Now