5步简单手写一个web服务器

126 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

  1. 建立一个Socket并用死循环的形式监听指定端口
package com.study.diyserver;

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

/**
 * @ClassName HttpServer
 * @Descriotion TODO
 * @Author nitaotao
 * @Date 2022/5/5 8:38
 * @Version 1.0
 * 建立一个Socket并用死循环的形式监听指定端口
 **/
public class HttpServer {
    public static String ROOT = "src/main/resources";
    //默认ROOT文件夹
    public static String defaultPage = "index.html";

    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(8000);
        while (true) {
            //阻塞:等待浏览器的连接
            Socket sk = server.accept();
            System.out.println("等待连接...");
            //启动服务线程
            new HttpThread(sk).start();

        }
    }
}

  1. 实现一个多线程的程序,用来处理每一个用户的请求
package com.study.diyserver;

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

/**
 * @ClassName HttpThread
 * @Descriotion TODO
 * @Author nitaotao
 * @Date 2022/5/5 9:22
 * @Version 1.0
 * 实现一个多线程的程序,用来处理每一个用户的请求
 **/
public class HttpThread extends Thread {
    private Socket socket;

    public HttpThread(Socket sk) {
        this.socket = sk;
    }

    @Override
    public void run() {

        InputStream ins = null;
        OutputStream outs = null;
        try {
            ins = socket.getInputStream();
            outs = socket.getOutputStream();
            Receive receive = new Receive(ins);
            //用Receive类获取浏览器发来的URL请求
            String URL = receive.parse();
            System.out.println("URL:"+URL);
            if ("/".equals(URL)) {
                //如果没有指定文件,那么给传来的URL加上默认文件名
                URL = HttpServer.defaultPage;
            }
            System.out.println("URL:"+URL);
            Answer ans = new Answer(outs);
            //再将URL执行的文件用Answer类的send方法返回给浏览器
            ans.Send(URL);
        } catch (IOException e) {
            System.out.println(e.toString());
        }finally{
            try {
                if (ins != null) {
                    ins.close();
                }
                if (outs != null) {
                    outs.close();
                }
                if (socket != null) {
                    socket.close();
                }
            } catch (Exception e){} {

            }
        }

    }
}

  1. 本类取得了浏览器传过来的URL字符串
package com.study.diyserver;

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

/**
 * @ClassName Receive
 * @Descriotion TODO
 * @Author nitaotao
 * @Date 2022/5/5 9:32
 * @Version 1.0
 * 本类取得了浏览器传过来的URL字符串
 **/
public class Receive {
    private InputStream in = null;

    public Receive(InputStream in) {
        this.in = in;

    }

    //这个方法的目的是将URL的请求的文件返回
    public String parse() {
        //这个变量就是实际从浏览器获取的请求数据流
        StringBuffer receiveStr = new StringBuffer(2048);
        int i;
        byte[] bytes = new byte[2048];
        try {
            i = in.read(bytes);

        } catch (IOException e) {
            i = -1;
            e.printStackTrace();
        }
        for (int j = 0; j < i; j++) {
            //将取得的信息循环追加到receiveStr变量中
            //此处不填 char ,会是数字
            receiveStr.append((char)bytes[j]);
        }
        return getUri(receiveStr.toString());

    }

    //将收到的HTTP协议数据包分解,取出文件名的描述URL
    private String getUri(String receiveStr) {
        System.out.println("===============receiveStr===================");
        System.out.println(receiveStr);
        System.out.println("===============receiveStr===================");
        int index1, index2;
        index1 = receiveStr.indexOf(' ');
        if (index1 != -1) {
            index2 = receiveStr.indexOf(' ', index1 + 1);
            if (index2 > index1) {
                return receiveStr.substring(index1 + 1, index2);
            }
        }
        return null;
    }
}

  1. 设置响应转发,处理地址请求
package com.study.diyserver;

import java.io.*;

/**
 * @ClassName Answer
 * @Descriotion TODO
 * @Author nitaotao
 * @Date 2022/5/5 9:53
 * @Version 1.0
 **/
public class Answer {
    private OutputStream outs;

    public Answer(OutputStream outs) {
        this.outs = outs;
    }
    public void Send(String pagefile) throws IOException {
        byte[] bytes = new byte[2048];
        FileInputStream fis = null;
        File file = new File(HttpServer.ROOT, pagefile);
        System.out.println(file.getPath());
        if (file.exists()) {
            fis = new FileInputStream(file);
            int ch = fis.read(bytes, 0, 2048);
            String sBody = new String(bytes, 0);
            //返回的信息是在文件内容前面加上HTTP协议的格式内容
            String sendMessage = "HTTP/1.1 200 OK\r\n" +
                    "Content-Type:text/html\r\b" +
                    "Content-Length:" + ch + "\r\n" +
                    "\r\n" + sBody;
            //输出文件
            outs.write(sendMessage.getBytes());
        } else {
            //文件不存在的话,就返回HTTP协议格式内容
            String errorMessage = "HTTP/1.1 404 File Not Found\r\n" +
                    "Content-Type:text/html\r\b" +
                    "Content-Length: 23\r\n" +
                    "\r\n" +
                    "<h1>File Not Found</h1>";
            outs.write(errorMessage.getBytes());
        }
        if (fis != null) {
            fis.close();
        }
    }
}

  1. 访问的页面
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>SUCCESS</h1>
</body>
</html>

参考《java程序员上班那点事儿》,强烈推荐!!!