Using bio to build a simple HTTP server

Source: Internet
Author: User

Tag: Call the empty line bind net HTTP server address buffered ADE. Net

Usually when using Tomcat, jetty, just use the inside of the servlet, filter, etc., never thought how they interact with the browser. Work encountered some problems, in the process of communicating with other colleagues and learning the source code, only to understand the bottom of the original is a socket, just know what the so-called NiO is ...

Before you know NiO, be sure to learn about bio and use bio to write a simple HTTP server demo: Browser input any request, print out the request information in the background and output Hello World in the browser.

1 ImportJava.io.BufferedReader;2 ImportJava.io.BufferedWriter;3 Importjava.io.IOException;4 ImportJava.io.InputStream;5 ImportJava.io.InputStreamReader;6 ImportJava.io.OutputStream;7 ImportJava.io.OutputStreamWriter;8 Importjava.net.InetSocketAddress;9 ImportJava.net.ServerSocket;Ten ImportJava.net.Socket; One  A  Public classBioserver { -     PrivateServerSocket ServerSocket; -      the     PrivateString IP; -      -     Private intPort; -      +      PublicBioserver (String IP,intPort) { -          This. IP =IP; +          This. Port =Port; A     } at      -      Public voidStartlisten ()throwsIOException { -ServerSocket =NewServerSocket (); -Serversocket.bind (NewInetsocketaddress (IP, port));//Create a listener for the specified port and IP -          -          while(true) { in accept (); -         } to     } +      -     Private voidAccept ()throwsIOException { the         //request received from client *Socket socket =serversocket.accept (); $         Panax Notoginseng         //the input stream is used to receive data transmitted by the client. -InputStream in =Socket.getinputstream (); theInputStreamReader reader =NewInputStreamReader (in); +BufferedReader br =NewBufferedReader (reader); AString line =NULL; the          while(line = Br.readline ())! =NULL) { + System.out.println (line); -             //according to the HTTP protocol, a blank line is separated between the request header and the request body $             if("". Equals (line)) { $                  Break; -             } -         } the          -         //The output stream is used to send a response message to the client, in accordance with the HTTP protocol formatWuyiOutputStream out =Socket.getoutputstream (); theOutputStreamWriter writer =NewOutputStreamWriter (out); -BufferedWriter BW =NewBufferedWriter (writer); WuBw.write ("http/1.1 ok\n"); -Bw.write ("content-type:text/html; Charset=utf-8\n "); AboutBw.write ("\ n"); $Bw.write ("); -Bw.write ("); -Bw.write ("<title>"); -Bw.write ("BIO Http Server"); ABw.write ("</title>"); +Bw.write ("); theBw.write ("<body>"); -Bw.write ("); $Bw.write ("</body>"); theBw.write ("); the          the         //You must not call the Close method until the request read and response writes have been processed, and closing the input stream will also cause the output stream to be unavailable the bw.close (); - writer.close (); in out.close (); the br.close (); the reader.close (); About in.close (); the socket.close (); the     } the      +      Public Static voidMain (string[] args) { -Bioserver Server =NewBioserver ("127.0.0.1", 8080); the         Try {Bayi Server.startlisten (); the}Catch(IOException e) { the e.printstacktrace (); -         } -     } the}

Using bio to build a simple HTTP server

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.