自学内容网 自学内容网

DIY-Tomcat part 2 实现Processor和Connector以及测试所用TestClient

实现Processor

package Webserver.src.processor;

import java.io.IOException;

import Webserver.src.connector.Request;
import Webserver.src.connector.Response;

public class StaticProcessor {

    public void process(Request request, Response response) {
        try {
            response.sendStaticResource();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
  • 访问静态资源
  • 简单的调用response中的sendStaticResource即可

实现Connector

package Webserver.src.connector;

import java.io.Closeable;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import Webserver.src.processor.StaticProcessor;

public class Connector implements Runnable{
    
    private static final int DEFAULT_PORT = 8888;

    private ServerSocket server;
    private int port;
//绑定端口
    public Connector() {
        this(DEFAULT_PORT);
    }

    public Connector(int port) {
        this.port = port;
    }

    public void start() {
    //创建线程并启动该线程,会自动调用线程中的run方法
        Thread thread = new Thread(this);
        thread.start();
    }

    @Override
    public void run() {
        try {
        //创建服务器socket
            server = new ServerSocket(port);
            System.out.println("启动服务器,监听端口:" + port);
//BIO式建立网络间通讯
            while (true) {
            //server.accept会阻塞直到链接成功建立
                Socket socket = server.accept();
                InputStream input = socket.getInputStream();
                OutputStream output = socket.getOutputStream();
//创建Request并解析HTTP请求头中的uri
                Request request = new Request(input);
                request.parse();
//创建Response并绑定request以获取uri
                Response response = new Response(output);
                response.setRequest(request);
//调用process读取文件内容并输出到输出流中
                StaticProcessor processor = new StaticProcessor();
                processor.process(request, response);
//及时关闭socket节省服务器资源
                close(socket);
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {
        //最后确保关闭服务器,节省资源
            close(server);
        }
    }

    private void close(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();  
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    }
}
  • 建立了个BIO模型的网络间通讯服务器
  • 调用process将请求的文件内容输出到输出流中

实现客户端TestClient以及服务器端Bootstrap

TestClient

package Webserver.src.test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class TestClient {
    
    public static void main(String[] args) throws UnknownHostException, IOException {
    //建立socket监听主机:"localhost" 端口:8888
        Socket socket = new Socket("localhost", 8888);
        OutputStream output = socket.getOutputStream();
        //发送HTTP请求并及时关闭输出流,节省资源
        output.write("GET /index.html HTTP/1.1".getBytes());
        socket.shutdownOutput();
//读入输入中的内容并输出
        InputStream input = socket.getInputStream();
        byte [] buffer = new byte[2048];
        int length = input.read(buffer);
        StringBuilder response = new StringBuilder();
        for (int i = 0; i < length; i ++) {
            response.append((char)buffer[i]);
        }
        System.out.println(response.toString());
        socket.shutdownInput();
//关闭socket,关闭客户端
        socket.close();
    }
}
  • 建立客户端
  • 接受客户端传入的消息并打印

Bootstrap

package Webserver.src;

import Webserver.src.connector.Connector;

public class Bootstrap {
    
    public static void main(String[] args) {
        Connector connector = new Connector();
        connector.start();
    }
}
  • 启动服务器端
  • 等待客户端连接和解析其发送的请求

测试结果

先后启动服务器端以及客户端
在这里插入图片描述

  • 可见成功返回200 OK的HTTP响应头和请求的文件内容

启动服务器端并在web浏览器上发送请求

  1. 发送http://localhost:8888/index.html请求

在这里插入图片描述
成功返回对应的网页内容

  1. 发送不合法的请求

在这里插入图片描述

成功返回File not found的网页内容


原文地址:https://blog.csdn.net/m0_62112384/article/details/144138690

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!