//直接使用 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 "; msg+="Content-Type:text/html " ; msg+="Content-length:100 " ; msg+=" "; msg+="<h1>cccccccccccccccccccccccccccccccc</h1>"; outputStream.write(msg.getBytes()); socket.close(); } } catch (Exception ex){ } }
}
效果如图