使用BIO建立简单的http服务器

2021-05-20 01:29

阅读:351

YPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">

标签:调用   空行   bind   net   http服务器   address   buffered   ade   .net   

平时在使用tomcat、jetty时,只是使用里面的servlet、filter等,从来没想过它们如何同浏览器进行交互的。工作中遇到一些问题,在同其它同事交流及学习源码的过程中,才明白底层原来走的也是socket,才知道所谓的NIO是什么。。。

了解NIO之前,肯定得先了解BIO,出于学习的目的 ,使用BIO写一个简单的http服务器DEMO:浏览器输入任何请求,后台打印出请求信息并在浏览器中输出Hello World

 1 import java.io.BufferedReader;
 2 import java.io.BufferedWriter;
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.io.InputStreamReader;
 6 import java.io.OutputStream;
 7 import java.io.OutputStreamWriter;
 8 import java.net.InetSocketAddress;
 9 import java.net.ServerSocket;
10 import java.net.Socket;
11 
12 public class BioServer {
13     private ServerSocket serverSocket;
14     
15     private String ip;
16     
17     private int port;
18     
19     public BioServer(String ip, int port) {
20         this.ip = ip;
21         this.port = port;
22     }
23     
24     public void startListen() throws IOException {
25         serverSocket = new ServerSocket();
26         serverSocket.bind(new InetSocketAddress(ip, port)); //创建对指定端口及IP的监听
27         
28         while (true) {
29             accept();
30         }
31     }
32     
33     private void accept() throws IOException {
34         //收到客户端的请求
35         Socket socket = serverSocket.accept();
36         
37         //输入流用于接收客户端传输过来的数据
38         InputStream in = socket.getInputStream();
39         InputStreamReader reader = new InputStreamReader(in);
40         BufferedReader br = new BufferedReader(reader);
41         String line = null;
42         while ((line = br.readLine()) != null) {
43             System.out.println(line);
44             //按照HTTP协议,请求头和请求体之间为一空行分隔
45             if ("".equals(line)) {
46                 break;
47             }
48         }
49         
50         //输出流用于向客户端发送响应消息,需遵从HTTP协议格式
51         OutputStream out = socket.getOutputStream();
52         OutputStreamWriter writer = new OutputStreamWriter(out);
53         BufferedWriter bw = new BufferedWriter(writer);
54         bw.write("HTTP/1.1 200 OK\n");
55         bw.write("Content-Type: text/html; charset=UTF-8\n");
56         bw.write("\n");
57         bw.write("");
58         bw.write("");
59         bw.write(""<span>);
</span><span>60</span>         bw.write("BIO Http Server"<span>);
</span><span>61</span>         bw.write("");
62         bw.write("");
63         bw.write("");
64         bw.write("

Hello World!

"); 65 bw.write(""); 66 bw.write(""); 67 68 //必须在请求读取和响应写入都处理完毕之后才可以调用close方法,将输入流关闭也会导致输出流不可用 69 bw.close(); 70 writer.close(); 71 out.close(); 72 br.close(); 73 reader.close(); 74 in.close(); 75 socket.close(); 76 } 77 78 public static void main(String[] args) { 79 BioServer server = new BioServer("127.0.0.1", 8080); 80 try { 81 server.startListen(); 82 } catch (IOException e) { 83 e.printStackTrace(); 84 } 85 } 86 }

 

使用BIO建立简单的http服务器

标签:调用   空行   bind   net   http服务器   address   buffered   ade   .net   

原文地址:http://www.cnblogs.com/smart-elf/p/7710837.html


评论


亲,登录后才可以留言!