栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

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

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

import java.io.IOException;
import java.net.ServerSocket;
import java.net.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;


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;


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.*;


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 OKrn" +
                    "Content-Type:text/htmlrb" +
                    "Content-Length:" + ch + "rn" +
                    "rn" + sBody;
            //输出文件
            outs.write(sendMessage.getBytes());
        } else {
            //文件不存在的话,就返回HTTP协议格式内容
            String errorMessage = "HTTP/1.1 404 File Not Foundrn" +
                    "Content-Type:text/htmlrb" +
                    "Content-Length: 23rn" +
                    "rn" +
                    "File Not Found";
            outs.write(errorMessage.getBytes());
        }
        if (fis != null) {
            fis.close();
        }
    }
}

  1. 访问的页面



    
    Title


SUCCESS

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/859803.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号