手寫服務器系列(4) 簡單服務器

基於上一篇博客繼續 往下寫。本篇博客我們正式開始寫一個簡單版本的服務器。

1. 思考

    我們需要考慮服務器應該具備哪些能力。首先,我們需要一個socket程序接收客戶端請求;然後,我們需要給客戶端一個返回;最後,我們需要包裝一下http消息中攜帶的參數提供給使用了我們的服務器的上層程序。

2. 手寫一個簡單服務器

2.1 HttpRequest

package com.zlyx.easy.server.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class HttpRequest {

	private String method;

	private String url;

	public HttpRequest(InputStream in) throws IOException {
		InputStreamReader isReader = new InputStreamReader(in);
		BufferedReader bufferedReader = new BufferedReader(isReader);
		String string = bufferedReader.readLine();
		String[] httpInfos = string.split(" ");
		this.method = httpInfos[0];
		this.url = httpInfos[1];
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

}

2.2  HttpResponse

package com.zlyx.easy.server.http;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class HttpResponse {

	public BufferedWriter writer;

	public HttpResponse(OutputStream os) {
		writer = new BufferedWriter(new OutputStreamWriter(os));
	}

	public void write(String content) throws IOException {
		writer.append("HTTP/1.1 200 OK\n").append("Content-Type: text/html\n").append("\r\n").append("<html><body>")
				.append(content).append("</body></html>");
		writer.flush();
	}
}

2.3  HttpServlet

package com.zlyx.easy.server.servlet;

import java.io.IOException;
import java.net.Socket;

import com.zlyx.easy.server.http.HttpRequest;
import com.zlyx.easy.server.http.HttpResponse;

public class HttpServlet {

	private HttpRequest request;

	private HttpResponse response;

	public HttpServlet(Socket socket) throws IOException {
		request = new HttpRequest(socket.getInputStream());
		response = new HttpResponse(socket.getOutputStream());
	}

	/**
	 * 處理get請求
	 * 
	 * @throws IOException
	 */
	public void doGet() throws IOException {
		System.out.println(request.getUrl());
		response.write(String.valueOf(request.hashCode()));
	}
}

2.4 MainServer

package com.zlyx.easy.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import com.zlyx.easy.server.servlet.HttpServlet;

public class MainServer {

	/**
	 * server port
	 */
	private static int port = 8080;

	public static void main(String[] args) throws IOException {
		ServerSocket ss = null;
		try {
			ss = new ServerSocket(port);
			while (true) {
				Socket socket = ss.accept();
				while (true) {
					new HttpServlet(socket);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			ss.close();
		}
	}
}

 啓動ManServer後通過瀏覽器訪問http://localhost:8080/,控制檯輸出:

瀏覽器輸出:

一個簡單的服務器就寫出來了。

參考博客手寫Tomcat

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章