以下是我的http服务器的初始代码:
public class Server3 {
public void accept() {
try {
int port = 8081;
ServerSocket serverSocket = new ServerSocket(port);
while (true) {
final Socket clientSocket = serverSocket.accept();
System.out.println("accepted.");
new Thread() {
@Override
public void run() {
try {
BufferedReader in = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
String s;
while ((s = in.readLine()) != null) {
System.out.println(s);
if (s.isEmpty()) {
break;
}
}
// Processing a request
sleep(9000000);
in.close();
clientSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
要练习http流水线操作,我选择了Opera,因为它实现了它并默认启用它。所以我打开两个标签并导航到localhost:8081但是我仍然只收到一个请求:
GET / HTTP/1.1
User-Agent: Opera/9.80 (X11; Linux i686) Presto/2.12.388 Version/12.15
Host: localhost:8081
Accept: text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1
Accept-Language: en-US,en;q=0.9
Accept-Encoding: gzip, deflate
Pragma: no-cache
Cache-Control: no-cache
Connection: Keep-Alive
我也尝试了上述代码的一小部分变体。我在serverSocket.accept()之后添加了另一个对sleep()的调用,并且在那个睡眠期间我从浏览器中发出了两个请求,以防它们使用相同的套接字。但结果是一样的。
我应该如何修复此代码,以便我可以从同一个浏览器触发两个请求,服务器接收它们而不发送响应(以便使用http流水线)?