简单的 手写 服务器

服务器

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();
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虚构的乌托邦

如果可以请打赏鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值