简易web服务器(java版)

时间:2023-03-09 02:37:48
简易web服务器(java版)
//直接使用 ServerSocket 监听服务器端口,就能实现web服务器
package ThreadPoolTest; import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.concurrent.*; public class Main {
public static void main(String[] args) throws UnknownHostException {
HttpServer();
} static void HttpServer(){ ServerSocket serverSocket =null;
try{
serverSocket=new ServerSocket(91);
InputStream inputStream;
OutputStream outputStream;
while (true){
Socket socket=serverSocket.accept();
inputStream=socket.getInputStream();
outputStream=socket.getOutputStream();
String msg="HTTP/1.1 200 OK \r\n";
msg+="Content-Type:text/html\r\n" ;
msg+="Content-length:100\r\n" ;
msg+="\r\n";
msg+="<h1>cccccccccccccccccccccccccccccccc</h1>"; outputStream.write(msg.getBytes());
socket.close();
} }
catch (Exception ex){ } }
}

效果如图

简易web服务器(java版)