我想创建不应该关闭的保持连接。我尝试了以下方式,但它在打印日期后关闭。
public class SimplePHTTPServer {
public static void main(String args[]) throws IOException {
ServerSocket server = new ServerSocket(1122);
System.out.println("Listening for connection on port 1122 ....");
while (true) {
try (Socket socket = server.accept()) {
Date today = new Date();
String httpResponse = "HTTP/1.1 200 OK\r\n\r\n" + today;
socket.getOutputStream().write(httpResponse.getBytes("UTF-8"));
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
下面通过 curl 测试的是实际输出:
curl -i -X GET -H “连接:保持活动” http://localhost:1122/
HTTP/1.1 200 正常
2018 年 8 月 2 日星期四 18:42:30 IST
预期结果是:
curl -i -X GET -H “连接:保持活动” http://localhost:1122/
HTTP/1.1 200 正常
2018 年 8 月 2 日星期四 18:42:30 IST
2018 年 8 月 2 日星期四 18:47:30 IST
2018 年 8 月 2 日星期四 18:52:30 IST
... 很快
如何创建保持连接?