/** * 简易版的服务器, 仅支持一次请求 * 创建服务器,并启动 * * 请求地址: http://localhost:8888/ * * 1、请求 * 2、响应 * @author Administrator * */ public class Server3 { private ServerSocket server; public static final String CRLF="\r\n"; public static final String BLANK=" "; /** * @param args */ public static void main(String[] args) { Server3 server = new Server3(); server.start(); } /** * 启动方法 */ public void start(){ try { server = new ServerSocket(8888); this.receive(); } catch (IOException e) { e.printStackTrace(); } } /** * 接收客户端 */ private void receive(){ try { Socket client =server.accept(); byte[] data=new byte[20480]; int len =client.getInputStream().read(data); //接收客户端的请求信息 String requestInfo=new String(data,0,len).trim(); System.out.println(requestInfo); //响应 StringBuilder responseContext =new StringBuilder(); responseContext.append("<html><head><title>HTTP响应示例</title>" + "</head><body>Hello 中国!</body></html>"); StringBuilder response =new StringBuilder(); //1) HTTP协议版本、状态代码、描述 response.append("HTTP/1.1").append(BLANK).append("200").append(BLANK).append("OK").append(CRLF); //2) 响应头(Response Head) response.append("Server:bjsxt Server/0.0.1").append(CRLF); response.append("Date:").append(new Date()).append(CRLF); response.append("Content-type:text/html;charset=GBK").append(CRLF); //正文长度 :字节长度 一定要是 响应正文内容字节长度,比如长为100字节,那么在浏览器解析时仅解析这100字节 response.append("Content-Length:").append(responseContext.toString().getBytes().length).append(CRLF); //3)正文之前 response.append(CRLF); //4)正文 response.append(responseContext); System.out.println(responseContext); //输出流 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream())); bw.write(response.toString()); bw.flush(); bw.close(); } catch (IOException e) { } } /** * 停止服务器 */ public void stop(){ } }
http协议下----简易版的服务器代码
猜你喜欢
转载自chengjianxiaoxue.iteye.com/blog/2182999
今日推荐
周排行