Implementing a simple Multithreaded HTTP server

Source: Internet
Author: User
Tags readline socket thread

Later, this server will be enriched to achieve more functions, such as:

1. Support Post method submission

2. Support the spread of the binary delivery

3. Support thread pool processing

4. The use of NIO non-blocking form to achieve

1package thread;


2


3import Java.io.BufferedReader;


4import java.io.IOException;


5import Java.io.InputStream;


6import Java.io.InputStreamReader;


7import Java.io.OutputStream;


8import Java.io.PrintWriter;


9import Java.net.ServerSocket;


10import Java.net.Socket;


11


12/** *//**


* Copyright (C): 2009


* @author Chen Wanhan


* June, 2009 2:39:39 PM


16 * *


17


18/** *//**


* Threaded Network Server


20 * This is a simple multi-threaded HTTP server


21 * Multithreading is used to handle high concurrent user requests


22 * *


23public class Httpserver {


public static void Main (String [] args) {


httpserver hs=new httpserver ();


int I=1, port=8888;


Socket Received=null;


try{


serversocket server=new ServerSocket (port);


while (true) {


received=server.accept ();


if (received!=null) {


hs.new ProcessThread (i++,received). Start ();


34}


35}


}catch (IOException e) {


Notoginseng e.printstacktrace ();


38}


39}


40


class ProcessThread extends Thread


42 {


-Private int thread_number=0;


private Socket Received=null;


45


public ProcessThread (int thread_number, Socket received) {


super ();


this.thread_number = Thread_number;


this.received = received;


50}


51


public void Run () {


System.out.println ("The first" +thread_number+ "Processing thread started ...");


if (received!=null) {


try{


System.out.println ("Connect User's address:" +received.getinetaddress (). gethostaddress ());


InputStream In=received.getinputstream ();


BufferedReader d= New BufferedReader (New InputStreamReader (in));


String result=d.readline ();


while (Result!=null &&!result.equals ("")) {


SYSTEM.OUT.PRINTLN (result);


Result=d.readline ();


63}


OutputStream Out=received.getoutputstream ();


printwriter outstream=new PrintWriter (out,true);


String msg1= "<html><head><title></title></head><body><h 1>, Roger! </h1></body></html> ";


outstream.println ("http/1.0 OK"); Returns the reply message and ends the answer


outstream.println ("CONTENT-TYPE:TEXT/HTML;CHARSET=GBK");


outstream.println (); According to the HTTP protocol, the null row ends the header information


outstream.println (MSG1);


Outstream.flush ();


Outstream.close ();


}catch (IOException e) {


E.printstacktrace ();


}finally{


try{


Received.close ();


}catch (IOException e) {


E.printstacktrace ();


80}


81}


82}


83}


84}


85}


86

Run this program to start the server, and then in the browser address bar input: http://localhost:8888/, you can see the return results.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.