tiancai
//直接使用 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){ } }
}

效果如图

分类:

技术点:

相关文章:

  • 2021-07-19
  • 2022-01-27
  • 2021-12-17
  • 2021-09-21
  • 2021-12-31
  • 2021-12-27
  • 2021-08-21
  • 2021-04-27
猜你喜欢
  • 2021-12-17
  • 2022-01-10
  • 2022-12-23
  • 2022-12-23
  • 2021-12-02
  • 2021-06-17
  • 2021-11-23
相关资源
相似解决方案