标签:
第一章 一个简单的Web服务器
HttpServer.java
package com.simpleHttpServer; import java.net.Socket; import java.net.ServerSocket; import java.net.InetAddress; import java.io.InputStream; import java.io.OutputStream; import java.io.IOException; import java.io.File; public class HttpServer { public static final String WEB_ROOT = System.getProperty("user.dir") + File.separator + "webroot"; // 关闭HttpServer命令 private static final String SHUTDOWN_COMMAND = "/SHUTDOWN"; // 是否收到关闭HttpServer命令 private boolean shutdown = false; public static void main(String[] args) { HttpServer server = new HttpServer(); server.await(); } public void await() { ServerSocket serverSocket = null; int port = 8080; try { serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1")); } catch (IOException e) { e.printStackTrace(); System.exit(1); } // 循环等待Http请求 while (!shutdown) { Socket socket = null; InputStream input = null; OutputStream output = null; try { socket = serverSocket.accept(); input = socket.getInputStream(); output = socket.getOutputStream(); // 创建一个Request对象并解析 Request request = new Request(input); request.parse(); // 创建一个Response对象 Response response = new Response(output); response.setRequest(request); response.sendStaticResource(); // 关闭socket socket.close(); // 检查是否是关闭命令 shutdown = request.getUri().equals(SHUTDOWN_COMMAND); } catch (Exception e) { e.printStackTrace(); continue; } } } }
Request.java
package com.simpleHttpServer; import java.io.InputStream; import java.io.IOException; public class Request { private InputStream input; private String uri; public Request(InputStream input) { this.input = input; } public void parse() { StringBuffer request = new StringBuffer(2048); int i; byte[] buffer = new byte[2048]; try { i = input.read(buffer); } catch (IOException e) { e.printStackTrace(); i = -1; } for (int j = 0; j < i; j++) { request.append((char) buffer[j]); } System.out.print(request.toString()); uri = parseUri(request.toString()); } private String parseUri(String requestString) { int index1, index2; index1 = requestString.indexOf(‘ ‘); if (index1 != -1) { index2 = requestString.indexOf(‘ ‘, index1 + 1); if (index2 > index1) return requestString.substring(index1 + 1, index2); } return null; } public String getUri() { return uri; } }
Response.java
package com.simpleHttpServer; import java.io.OutputStream; import java.io.IOException; import java.io.FileInputStream; import java.io.File; public class Response { private static final int BUFFER_SIZE = 1024; Request request; OutputStream output; public Response(OutputStream output) { this.output = output; } public void setRequest(Request request) { this.request = request; } public void sendStaticResource() throws IOException { byte[] bytes = new byte[BUFFER_SIZE]; FileInputStream fis = null; try { File file = new File(HttpServer.WEB_ROOT, request.getUri()); if (file.exists()) { fis = new FileInputStream(file); int ch = fis.read(bytes, 0, BUFFER_SIZE); while (ch != -1) { output.write(bytes, 0, ch); ch = fis.read(bytes, 0, BUFFER_SIZE); } } else { String errorMessage = "HTTP/1.1 404 File Not Found\r\n" + "Content-Type: text/html\r\n" + "Content-Length: 23\r\n" + "\r\n" + "<h1>File Not Found</h1>"; output.write(errorMessage.getBytes()); } } catch (Exception e) { System.out.println(e.toString()); } finally { if (fis != null) fis.close(); } } }
(1)System.getProperty("user.dir")是取得当前工作目录
HttpServer中定义的public static final String WEB_ROOT 即 工作目录/webroot
(2) 在创建TCP服务端监听的时候
try { serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1")); } catch (IOException e) { e.printStackTrace(); System.exit(1); }
程序主动捕获了IOException,深入ServerSocket源码可以看到:
public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException { setImpl(); if (port < 0 || port > 0xFFFF) throw new IllegalArgumentException( "Port value out of range: " + port); if (backlog < 1) backlog = 50; try { bind(new InetSocketAddress(bindAddr, port), backlog); } catch(SecurityException e) { close(); throw e; } catch(IOException e) { close(); throw e; } }
在创建ServerSocket对象(开启TCP服务端监听)的时候可能抛出三种异常
(1) new IllegalArgumentException 在要监听的TCP端口范围不在正确范围0-65535之间,就抛出参数异常 改异常时候RuntimeException 不用显式在方法后throws以及方法调用处try catch
(2) SecurityException 也是一个RuntimeException 异常触发条件, 如果安全管理器存在并且其 checkListen
方法不允许进行该操作。
(3) IOException 是非运行时异常 如果打开套接字时发生 I/O 错误。
简要介绍下ServerSocket:
把服务器套接字绑定到特定的端口号,这样远程客户端才能定位TCP服务,如果传递进来的值为零(zero),就使用任何空闲的端口--但是客户端可能没办法访问该服务,除非用什么方式通知了客户端端口号是多少,为队列分配足够的空间以支 持特定数量的客户端套接字。在ServerSocket(int port, int numberOfClients)构造函数的重载版本 中,加入了InetAddress参数,在多地址计算机上,它允许服务器套接字绑定到某个特定的IP地址。例如,某台计算机可能有两块网 卡,或者使用虚拟IP地址把它配置成像几台计算机一样工作的时候。如果地址的值为空(null),服务 器套接字将在所有的本地地址上接受请求。在默认情况下,队列的大小设置为50,但是也提供了备用的构造函数,它允许修改这个设置。如果端口已经被绑定了,或者安全性约束条件(例如安全性规则或知名端口上的操作系统约束条件)阻挡了访问,就会产生异常。
标签:
原文地址:http://www.cnblogs.com/wuxinliulei/p/4852353.html