• java webServer(一)


    java webServer实现

    浏览器和服务器使用的是http协议,http协议使用的是tcp

    这里主要在服务器端监听端口号

    实现功能

    通过浏览器向服务器发送http请求:http://localhost:9999/index.html(这里监听的是9999端口)

    服务器解析http请求,生成Html响应客户端请求

    WebServer.java

      1 package com.gxf.webServer;
      2 
      3 import java.io.BufferedReader;
      4 import java.io.IOException;
      5 import java.io.InputStream;
      6 import java.io.InputStreamReader;
      7 import java.io.OutputStream;
      8 import java.io.PrintStream;
      9 import java.net.ServerSocket;
     10 import java.net.Socket;
     11 
     12 /**
     13  * web服务器
     14  * 监听9999端口号
     15  * @author Administrator
     16  *
     17  */
     18 public class WebServer {
     19     //服务器监听端口号
     20     private final int PORT_LISTENING = 9999;
     21     
     22     public static void main(String args[]){
     23         WebServer webServer = new WebServer();
     24         
     25         //启动服务器
     26         webServer.startService();
     27     }
     28     
     29     
     30     //启动web服务器
     31     public void startService(){
     32         ServerSocket serverSocket = null;
     33         try {
     34             serverSocket = new ServerSocket(PORT_LISTENING);
     35         } catch (IOException e) {
     36             e.printStackTrace();
     37         }
     38         while(serverSocket != null){
     39             try {
     40                 Socket clientSocket = serverSocket.accept();
     41                 //new 一个线程处理客户端请求
     42                 new HandleRequest(clientSocket).start();
     43             } catch (IOException e) {
     44                 e.printStackTrace();
     45             }
     46         }
     47     }
     48 }
     49 
     50 /**
     51  * 解析客户端的http请求
     52  * 生成html响应客户端请求
     53  * @author Administrator
     54  *
     55  */
     56 class HandleRequest extends Thread{
     57     //客户端socket
     58     private Socket clientSocket;
     59     private OutputStream os;
     60     private InputStream is;
     61     
     62     public HandleRequest(Socket clientSocket){
     63         this.clientSocket = clientSocket;
     64         try {
     65             os = this.clientSocket.getOutputStream();
     66             is = this.clientSocket.getInputStream();
     67         } catch (IOException e) {
     68             e.printStackTrace();
     69         }
     70     }
     71     
     72     public void run(){
     73         //解析客户端http请求
     74         BufferedReader bfr = new BufferedReader(new InputStreamReader(is));
     75         String requestLine = "";
     76         String resourceRequest = "";
     77         try {
     78             requestLine = bfr.readLine();
     79             int spaceStartIndex = requestLine.indexOf(" ");
     80             int spaceEndIndex = requestLine.indexOf("HTTP");
     81             resourceRequest = requestLine.substring(spaceStartIndex, spaceEndIndex);
     82         } catch (IOException e) {
     83 
     84             e.printStackTrace();
     85         }
     86         
     87         //生成html输出到客户端浏览器,响应客户端请求
     88         PrintStream ps = new PrintStream(os);
     89         ps.println("<html>");
     90         ps.println("<meta content='text/html; charset=gb2312' http-equiv='Content-Type'/>");
     91         ps.println("<head><title>Welcome To MyWebServer</title></head>");
     92         ps.println("<h1>request resource is:" + resourceRequest + "</h1>");
     93         ps.println("</html>");
     94         
     95         ps.flush();
     96         ps.close();        
     97         
     98         try {
     99             clientSocket.close();
    100         } catch (IOException e) {
    101             e.printStackTrace();
    102         }
    103     }
    104 }

    这里只是解析了请求的资源

    来一请求,开一个线程处理,如果大量request,或者处理request时间长了,服务器会崩溃

     Excutor版本

    ExcutorWebServer.java

     1 package com.gxf.webServer;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.IOException;
     5 import java.io.InputStreamReader;
     6 import java.io.PrintStream;
     7 import java.net.ServerSocket;
     8 import java.net.Socket;
     9 import java.util.concurrent.Executor;
    10 import java.util.concurrent.Executors;
    11 
    12 /**
    13  * web服务器
    14  * 使用Excutor框架 
    15  * @author Administrator
    16  *
    17  */
    18 public class ExcutorWebServer {
    19     //监听端口号
    20     private static final int PORTNUM_LISTENING = 9999;
    21     //线程池大小
    22     private static final int NUM_THREADS_POOL = 100;
    23     
    24     private static Executor executor = Executors.newFixedThreadPool(NUM_THREADS_POOL);
    25     
    26     public static void main(String args[]) throws IOException{
    27         ServerSocket serverSocket = new ServerSocket(PORTNUM_LISTENING);
    28         while(true){
    29             final Socket clientSocket = serverSocket.accept();
    30             
    31             Runnable task = new Runnable(){
    32                 public void run(){
    33                     //处理客户端请求
    34                     try {
    35                         handlRequest(clientSocket);
    36                     } catch (IOException e) {
    37                         // TODO Auto-generated catch block
    38                         e.printStackTrace();
    39                     }                
    40                 }
    41             };
    42             
    43             executor.execute(task);
    44         }
    45     }
    46     
    47     /**
    48      * 处理请求 
    49      * @param task
    50      * @throws IOException 
    51      */
    52     public static void handlRequest(Socket clientSocket) throws IOException{
    53         System.out.println(clientSocket.getRemoteSocketAddress());
    54         //解析客户端http请求
    55         BufferedReader bfr = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    56         String requestLine = "";
    57         String resourceRequest = "";
    58         try {
    59             requestLine = bfr.readLine();
    60             int spaceStartIndex = requestLine.indexOf(" ");
    61             int spaceEndIndex = requestLine.indexOf("HTTP");
    62             resourceRequest = requestLine.substring(spaceStartIndex, spaceEndIndex);
    63         } catch (IOException e) {
    64 
    65             e.printStackTrace();
    66         }
    67         
    68         //生成html输出到客户端浏览器,响应客户端请求
    69         PrintStream ps = new PrintStream(clientSocket.getOutputStream());
    70         ps.println("<html>");
    71         ps.println("<meta content='text/html; charset=gb2312' http-equiv='Content-Type'/>");
    72         ps.println("<head><title>Welcome To MyWebServer</title></head>");
    73         ps.println("<h1>request resource is:" + resourceRequest + "</h1>");
    74         ps.println("</html>");
    75         
    76         ps.flush();
    77         ps.close();        
    78         
    79         try {
    80             clientSocket.close();
    81         } catch (IOException e) {
    82             e.printStackTrace();
    83         }
    84     }
    85 }
  • 相关阅读:
    使用Python快速生成虚拟的超大文件
    常用的 adb 命令合集
    Jmeter 性能测试之反向代理录制性能测试脚本
    论医院网络时钟系统(NTP时钟服务器)的重要性
    北斗时钟装置(卫星时间同步系统)应用自动化系统探讨
    解决前端部署到Nginx非根目录下页面出现空白的问题
    解决docker中Easyexcel因缺少字体无法导出的问题
    看图认识HTML5
    看图知Docker
    ASIS CTF Finals 2020
  • 原文地址:https://www.cnblogs.com/luckygxf/p/4581762.html
Copyright © 2020-2023  润新知