首页 > 代码库 > 30天轻松学习javaweb_模拟tomcat

30天轻松学习javaweb_模拟tomcat

运行 javac Server.java 编译java文件

执行 java Server 运行程序

在ie中输入 http://localhost:9999/ 打开模拟的服务程序

import java.net.*;
import java.io.*;

public class Server {

    public static void main(String args[]) throws Exception {
        System.out.println("start");
        ServerSocket server = new ServerSocket(9999);
        
        Socket sock = server.accept();
        System.out.println("listen");
        FileInputStream in = new FileInputStream("d:\\1.html");
        OutputStream out = sock.getOutputStream();

        int len = 0;
        byte buffer[] = new byte[1024];
        while ((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }
        in.close();
        out.close();
        sock.close();
        server.close();
        System.out.println("end");
    }
}