演示浏览器访问Web服务器的处理过程
1 import java.io.*; 2 import java.net.*; 3 4 public class WebServerDemo { 5 public static void main(String[] args) throws IOException { 6 ServerSocket ss = new ServerSocket(10000); 7 Socket s = ss.accept(); 8 byte[] bytes = new byte[1024]; 9 int len = 0; 10 InputStream is = s.getInputStream(); 11 len = is.read(bytes); 12 System.out.println("读取得字节数:" + len); 13 System.out.println(new String(bytes)); 14 //向浏览器写入内容 15 OutputStream os = s.getOutputStream(); 16 os.write("<h1 style='color:red'>您好,浏览器!</h1>".getBytes()); 17 s.close(); 18 ss.close(); 19 } 20 }
记录有用的信息和数据,并分享!