Bootstrap

简单的 手写 服务器

服务器

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

public class MyHttpServer {
    //端口
    private int port = 8080;

    //接收请求的方法
    public void receiving(){
        try {
            //创建Socket服务
            ServerSocket serverSocket = new ServerSocket(port);
            //循环接收请求
            while (true){
                //获取连接对象
                Socket socket = serverSocket.accept();
                //获取连接对象的输入流
                InputStream inputStream = socket.getInputStream();
                //创建Request
                MyHttpRequest request = new MyHttpRequest(inputStream);
                //解析请求
                request.parse();
                //创建Response
                OutputStream outputStream = socket.getOutputStream();
                MyHttpResponse response = new MyHttpResponse(outputStream);
                //进行响应
                response.sendRedirect(request.getUri());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

请求

import java.io.IOException;
import java.io.InputStream;

public class MyHttpRequest {

    private InputStream inputStream;
    private String uri;

    public MyHttpRequest(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public void parse(){
        try {
            byte[] bytes = new byte[1024];
            inputStream.read(bytes);
            String request = new String(bytes);
            parseUri(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void parseUri(String request){
        int index1,index2;
        index1 = request.indexOf(' ');
        index2 = request.indexOf(' ',index1+1);
        uri = request.substring(index1+1,index2);
    }

    public String getUri(){
        return this.uri;
    }
}

响应

import java.io.*;

public class MyHttpResponse {

    private OutputStream outputStream;

    public MyHttpResponse(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    public void sendRedirect(String uri){
        //判断uri是否存在
        //不存在返回404
        //存在直接返回目标资源数据
        File file = new File(System.getProperty("user.dir") + "/WebContent" + uri);
        if(file.exists()){
            try {
                //返回目标资源数据
                FileInputStream fileInputStream = new FileInputStream(file);
                byte[] bytes = new byte[(int)file.length()];
                fileInputStream.read(bytes);
                String result = new String(bytes);
                String response = getResponseMessage("200", result);
                this.outputStream.write(response.getBytes());
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                //返回404
                String error = getResponseMessage("404", "404 File Not Found!");
                this.outputStream.write(error.getBytes());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public String getResponseMessage(String code,String message){
        return "HTTP/1.1 "+code+"\r\n"
                +"Content-type: text/html\r\n"
                +"Content-Length: "+message.length()
                +"\r\n"
                +"\r\n"
                +message;
    }
}

测试

public class Test {
    public static void main(String[] args) {
        System.out.println("Server startup successfully");
        MyHttpServer server = new MyHttpServer();
        server.receiving();
    }
}

悦读

道可道,非常道;名可名,非常名。 无名,天地之始,有名,万物之母。 故常无欲,以观其妙,常有欲,以观其徼。 此两者,同出而异名,同谓之玄,玄之又玄,众妙之门。

;