?
Solved

http server

Posted on 2001-06-18
4
Medium Priority
?
219 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 

Accepted Solution

by:
megloff earned 90 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

718 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