Http服务器版本1.
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class HttpServerV1 {
//http基于tcp实现,基于HTTP基本格式实现
public ServerSocket serverSocket = null;
public HttpServerV1(int port) throws IOException {
serverSocket = new ServerSocket(port);
}
public void start() throws IOException {
System.out.println("服务器启动");
ExecutorService executorService = Executors.newCachedThreadPool();
while (true) {
//1、获取连接
Socket clientSocket = serverSocket.accept();
//2、处理连接
executorService.execute(new Runnable() {
@Override
public void run() {
try {
process(clientSocket);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}private void process(Socket clientSocket) throws IOException {
//由于HTTP是文本协议,用字符流处理
try(BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()))) {
//按照HTTP协议来进行操作
//1、读取请求并解析
// 1.1 解析首行三个部分按照空格切分
String firstLine = bufferedReader.readLine();
String[] firstLineTokens = firstLine.split(" ");
String method = firstLineTokens[0];
String url = firstLineTokens[1];
String version = firstLineTokens[2];
//1.2 解析header 按行读取 按照冒号空格表示键值对
Map headers = new HashMap<>();
String line = "";
while ((line = bufferedReader.readLine()) != null && line.length() != 0) {
//不能使用冒号来区分 referer字段内容可能包含冒号
String[] headerTokens = line.split(": ");
headers.put(headerTokens[0],headerTokens[1]);
}
//
//// 1.3 解析body 先不考虑
System.out.printf("%s %s %s\n",method, url, version);
for (Map.Entry entry : headers.entrySet()) {
System.out.println(entry.getKey()+ ": "+ entry.getValue());
}
System.out.println();
//2、根据请求计算响应返回hello
String resp = "";
if (url.equals("/ok")) {
bufferedWriter.write(version + " 200 OK\n");
resp = "hello";
} else if (url.equals("/notfound")) {
bufferedWriter.write(version + " 404 Not Found\n");
resp = "not found";
}else if (url.equals("/seeother")) {
bufferedWriter.write(version + " 303 see other\n");
bufferedWriter.write("Location: https://blog.csdn.net/yufy0528\n");
resp = "";
} else {
bufferedWriter.write(version+"200 OK\n");
resp = "default";
}// 3. 把响应写回到客户端
bufferedWriter.write("Content-Type: text/html\n");
bufferedWriter.write("Content-Length: " + resp.getBytes().length + "\n");
// 此处的长度, 不能写成 resp.length(), 得到的是字符的数目, 而不是字节的数目
bufferedWriter.write("\n");
bufferedWriter.write(resp);
// 此处这个 flush 就算没有, 问题也不大. 紧接着
// bufferedWriter 对象就要被关闭了. close 时就会自动触发刷新操作.
bufferedWriter.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}public static void main(String[] args) throws IOException {
HttpServerV1 httpServerV1 = new HttpServerV1(9090);
httpServerV1.start();
}
}
【Http服务器版本1.】
推荐阅读
- 画解算法(1.|画解算法:1. 两数之和)
- 【Hadoop踩雷】Mac下安装Hadoop3以及Java版本问题
- ts泛型使用举例
- 探索免费开源服务器tomcat的魅力
- https请求被提早撤回
- HTTP高级(Cookie,Session|HTTP高级(Cookie,Session ,LocalStorage )
- 拒绝可以很艺术,或者很行为艺术。
- C语言的版本比较
- www和https://又被Chrome地址栏隐藏了
- [源码解析]|[源码解析] NVIDIA HugeCTR,GPU版本参数服务器---(3)