Solved

http server

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
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 …
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

626 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