idea代码实现简单的web服务器

一、功能

和tomcat一样提供web服务器

二、知识点
回顾
1、集合
    List ArrayList 对数组的扩展
    Set    HashSet
    Map    HashMap
    List管理学生信息

2、多线程
    1.什么是多线程
    2.为什么要使用多线程
    3.在java中使用多线程

 3、多线程

    在同一个程序中同时执行多个任务
    多一个系统中同时运行多个程序多进程

    提高软件的体验和效率,

 4、 Thread类

    1.创建Thread的子类
    2.完善该类的run方法
    3.实例化该类,并且调用start方法
    4.后面启动的方法,不会等待前面启动的方法


  5、  Runnable接口
           因为接口和抽象类都没有办法new
    步骤:
    1.新建Runnable的实现类
    2.将实现类作为Thread类的构造方法
    3.启动还是用start
    
    优点:
        1.实现类是可以作为多个线程共有对象,可以将公共数据添加在实现类中
        2.方便实现同步锁,在同一时间内,不能有多个线程使用这个方法
                
知识点:
    1.Thread
    2.run
    3.start
    4.Thread.currentThread().getName()
        

三、代码

服务器

public class ServerService {
    private Socket s;
    private InputStream inputStream;
    private OutputStream outputStream;
    public ServerService(Socket s) {
        try {
            this.s=s;
            this.inputStream=s.getInputStream();
            this.outputStream=s.getOutputStream();
            this.work();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void work() {
        while (true){
            try {
                byte[] bytes = new byte[1024];
                int len = inputStream.read(bytes);
                if (len!=-1){
                    String req=new String(bytes,0,len,"utf-8");

                    String fileName=req.split("\n")[0].split(" ")[1].substring(1);

                    File file=new File(fileName);
                    InputStream is=new FileInputStream(file);

                    outputStream.write("HTTP /1.1 200 ok\r\n".getBytes("utf-8"));
                    outputStream.write("Content-Type:text/html\r\n".getBytes("utf-8"));
                    outputStream.write(("Content-Length:" + file.length() + "\r\n").getBytes("utf-8"));
                    outputStream.write("\r\n".getBytes("utf-8"));

                    int len0 = 0;
                    byte[] bytes1 = new byte[1024];
                    while ((len0=is.read(bytes1))!=-1){
                        outputStream.write(bytes1,0,len0);
                    }
                    outputStream.flush();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
    }
}

public class ServerThread extends Thread{
    private Socket s;
    public ServerThread(Socket s) {
        this.s=s;
    }

    @Override
    public void run() {
        System.out.println(this.getName());
        new ServerService(this.s);
    }
}


    
 启动服务器

public class Main {
    public static void main(String[] args) {
        System.out.println("启动服务");
        try {
            ServerSocket ss = new ServerSocket(9999);
            while (true){
                Socket s=ss.accept();
                new ServerThread(s).start();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

   

五、测试

1、访问

https://localhost:9999/index.html

2、结果

3、工程文件截图

六、总结


网络编程(不同主机之间的数据传输)
    三要素
    1.ip    主机的唯一标识    
    2.端口    主机所运行的程序的唯一标识
        65536 0-1023知名端口
        1024-49151 用户程序进程
        49152-65535 私有动态端口
    3.协议
        tcp/ip
tcp通讯协议
    可靠
    请求-响应 服务器
    
    客户端向服务器发送请求
    服务器接收请求
    服务器向客户端发送响应
    客户端接收响应
    Socket套接字
    利用io流实现数据传输

总结2
    tcp socket网络信息传输
    
    server
        1.ServerSocket
            new ServerSocket(port)
        2.当有用户访问,创建套接字
            Socket s=ss.accept();
        3.读取请求 获取InputStream  socket.getInputStream();
            int len=is.read(bytes);
            new String(bytes,0,len,"utf-8")
        4.发送响应OutputStream socket.getOutputStream();
            os.write("".getBytes            ("utf-8"));
            
    client 客户端
        1.Socket s=new Socket("ip或者域名",port);
        localhost 本机的域名
        本机ip


        2.发送请求
        3.接收响应


1.现阶段服务器不是1对1,而是为多个客户端提供服务
2.网络编程,多线程
3.步骤
    1.ServerScocke ss=new ServerSocket(port)
    2.while(true){
        Socket s=ss.accept();
    3.new ServerThread(s).start;    
}
    4.编写线程类,传递Socket对象,要实现的功能,就通过run方法调用,或者直接写在run方法中

接收用户发送的文件,并且保存,简单的上传,
解决文件重名 UUID.randomUUID().toString();

tomcat
    静态资源,动态页面

iis
nginx
    反向代理,并发高 开源软件


静态资源服务器


1.获取到浏览器的请求头部,并且分析请求
2.建立管道  磁盘文件->java内存 fis  java内存=>客户端浏览器
3.发送消息头
     os.write("HTTP /1.1 200 ok\r\n".getBytes("utf-8"));
                os.write("Content-Type:text/html\r\n".getBytes("utf-8"));
                File file=new File("webapp"+pathName);
                os.write(("Content-Length:" + file.length() + "\r\n").getBytes("utf-8"));

                os.write("\r\n".getBytes("utf-8"));

4.数据响应
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值