菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
327
0

下面我们将主要介绍如何实现一个基于java的Http服务器

原创
05/13 14:22
阅读数 757047

 

Http服务器主要由三个类构成:HttpServerRequestResponse。其中程序的入口在HttpServer类,它调用await()方法,使得Server开始等候客户端的连接。当客户端连接后,它将把静态的页面内容发送给客户端浏览器。下面分别介绍这三个类:

1HttpServer

 HttpServer需要有一个服务器的根目录这在WEB_ROOT变量中定义的:public static final String WEB_ROOT =System.getProperty("user.dir") + File.separator  + "webroot";当我们运行服务器的时候可以通过-D选项指定环境变量user.dir的值。这个类中最重要的方法就是await()方法,内容如下: 

 1 public void await() { 
 2     ServerSocket serverSocket = null 3     int port = 8080 4     try { 
 5       serverSocket =  new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1")); 
 6     } 
 7     catch (IOException e) { 
 8       e.printStackTrace(); 
 9       System.exit(1); 
10     } 
11     // Loop waiting for a request 
12     while (!shutdown) { 
13       Socket socket = null14       InputStream input = null15       OutputStream output = null16       try { 
17         socket = serverSocket.accept(); 
18         input = socket.getInputStream(); 
19         output = socket.getOutputStream(); 
20 
21         // create Request object and parse 
22         Request request = new Request(input); 
23         request.parse(); 
24 
25         // create Response object 
26         Response response = new Response(output); 
27         response.setRequest(request); 
28         response.sendStaticResource(); 
29 
30         // Close the socket 
31         socket.close(); 
32 
33         //check if the previous URI is a shutdown command 
34         shutdown = request.getUri().equals(SHUTDOWN_COMMAND); 
35       } 
36       catch (Exception e) { 
37         e.printStackTrace(); 
38         continue39       } 
40     } 
41   } 

    await()方法内构造一个ServerSocket的实例,等客户端连接进来的时候把socket.getInputStream()传递给Request类进行解析,把socket.getOutputStream()传递给Response类,然后再把request对象传递给Response,最后调用Response.sendStaticResource()方法发送数据给客户端。socket.close()后监测是不是接受到了关闭Server的命令,如果是的话跳出循环结束程序。

 

 

2. Request

  Request类的主要目的是对http请求进行封装,它有一个InputStream类型参数的构造器 

 1 public Request(InputStream input) {  2     this.input = input;  3   }  

 

  同时它还有一个重要的String类型的成员变量uriRequest的目的就是从inputStream中提取uri,这是由两个函数实现的

 1 public void parse() { 
 2     // Read a set of characters from the socket 
 3     StringBuffer request = new StringBuffer(2048); 
 4     int i; 
 5     byte[] buffer = new byte[2048]; 
 6     try { 
 7       i = input.read(buffer); 
 8     } 
 9     catch (IOException e) { 
10       e.printStackTrace(); 
11       i = -112     } 
13     for (int j=0; j<i; j++) { 
14       request.append((char) buffer[j]); 
15     } 
16     System.out.print(request.toString()); 
17     uri = parseUri(request.toString()); 
18   } 
19   private String parseUri(String requestString) { 
20     int index1, index2; 
21     index1 = requestString.indexOf(' '); 
22     if (index1 != -1) { 
23       index2 = requestString.indexOf(' ', index1 + 1); 
24       if (index2 > index1) 
25         return requestString.substring(index1 + 1, index2); 
26     } 
27     return null28   } 

  其中parseUri(String request)方法是私有方法,它把String参数进行解析把两个空格之间的字符串返回,这是遵循Http请求的定义规则的,如果你不清楚可以参考基于java实现http服务器之一。

 

3.Response

发表评论

0/200
327 点赞
0 评论
收藏