A simple Socket implementation of HTTP response server written in JAVA

A simple Socket implementation of HTTP response server written in JAVA, it is easy to understand the principle of Web server after reading.
package test.io;  
  
import java.net.*;  
import java.io. *;  
  
/**
 * A simple HTTP response server implemented by Socket. <br>
 * Only for the purpose of familiarizing yourself with the HTTP protocol, you can see the data format sent by the browser.
 *  
 * @author  */  
public class MyWebServer {  
  public static void main(String[] args) {  
    Socket socket = null;  
    try {  
      // Create a server socket listening on port 8000  
      ServerSocket s = new ServerSocket(8000, 3);  
      System.out.println("MyWebServer is waiting for a connection from the browser\n");  
      while (true) {  
        socket = s.accept();  
        System.out.println("Connection established. Port number: " + socket.getPort());  
        new MyWebServerThread(socket).start();  
      }  
    } catch (IOException e) {  
      e.printStackTrace ();  
    }  
  }  
}  
  
class MyWebServerThread extends Thread {  
  private Socket socket;  
  
  MyWebServerThread(Socket socket) {  
    this.socket = socket;  
  }  
  
  @Override  
  public void run() {  
    try {  
      InputStreamReader is = new InputStreamReader(socket.getInputStream());  
      char[] bs = new char[2048];  
      PrintStream out;  
      out = new PrintStream(socket.getOutputStream());  
      StringBuilder msg = new StringBuilder();  
      // If there is no data for 10 milliseconds, it is deemed that there is no new data.  
      // Because of Keep-Alive, the browser may not actively disconnect.  
      // In actual application, it will be determined according to whether the first line of the protocol is GET or POST.  
      socket.setSoTimeout(10);  
      //  
      // Read the request data here and do the corresponding processing  
      //  
      int len ​​= -1;  
      try {  
        while ((len = is.read(bs)) != -1) {  
          msg.append(bs, 0, len);  
          msg.append("\n");  
        }  
      } catch (Exception ex) {  
        // ex.printStackTrace();  
      }  
      // The following is the home page content directly generated by the server  
      // 1. First output the response header information to the browser  
      out.println("HTTP/1.1 200 OK");  
      out.println("Content-Type:text/html;charset:GBK");  
      out.println();  
      // 2. Output home page information  
      out  
          .println("<HTML><BODY>"  
              + "<center>"  
              + "<H1>HTTP protocol test server, current time:"  
              + new java.util.Date()  
              + "</h1>"  
              + "<form method='get'>username:<input type='text' name='username'/>password:<input type='text' name='password'/><input type='submit' value='GET测试'/></form><br/>"  
              + "<form method='post'>username:<input type='text' name='username'/>password:<input type='text' name='password'/><input type='submit' value='POST测试'/></form><br/>"  
              + "<form method='post'  enctype='multipart/form-data'>phototitle:<input type='text' name='phototitle'/>photo:<input type='file' name='photo'/><input type='submit' value='Upload测试'/></form>"  
              + "</center>The data you submitted is as follows:<pre>" + msg.toString() + "</pre></BODY></HTML>");  
      out.flush();  
      out.close();  
      is.close();  
      System.out.println("close");  
      // close the connection  
      socket.close();  
    } catch (IOException e) {  
      e.printStackTrace ();  
    }  
  }  
}  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325391271&siteId=291194637