在远程客户端关闭连接之前,readLine() 循环不会退出
readLine() loop not exiting until remote client closes connection
我在使用 Java SocketServer 时遇到问题,我正在构建一个非常基本的 Java 处理的 Web 服务器。
所以我正在创建一个套接字服务器,这是来自 运行 方法的代码,因为我已将服务器线程化。
我遇到的问题是服务器代码似乎冻结为 while((line = reader.readLine()) != null)
,直到远程客户端关闭连接。我正在使用 chrome 插件 ARC(高级 REST 客户端)进行测试。
public void start() throws ServerException{
this.running = true;
try{
this.server = new ServerSocket(this.port);
}catch(IOException ex){
System.err.println("Failed to create Server Port is inuse!");
throw new ServerException("Unable to bind to port '" + this.port + "'", ex);
}
while(!isStopped()){
Socket client = null;
try{
client = this.server.accept();
}catch(IOException ex){
if(isStopped()) {
return;
}
throw new ServerException("Error accepting client connection", ex);
}
new Thread(
new ServerWorker(client, this)
).start();
}
}
这是 ServerWorker
public void run(){
try {
InputStream input = clientSocket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
OutputStream output = clientSocket.getOutputStream();
long time = System.currentTimeMillis();
ArrayList<String> lines = new ArrayList<String>();
String line;
while((line = reader.readLine()) != null){
lines.add(line);
}
String[] msg = new String[lines.size()];
msg = lines.toArray(msg);
output.write("HTTP/1.1 200\r\n".getBytes());
output.write("\r\n".getBytes());
new HandleConnection(msg).begin(output);
reader.close();
output.close();
System.out.println("Request processed: " + time);
} catch (IOException e) {
}
}
最后是处理程序:
public void begin(OutputStream output){
for(int i = 0; i < lines.length; i++){
System.out.println(lines[i]);
}
try{
output.write("{\"Response\":\"Ok\"}\r\n".getBytes());
}catch(IOException e){}
}
The problem i'm having is that the server code seems to freeze as while((line = reader.readLine()) != null)
until the remote client closes the connection.
这不是问题:这是正确的指定行为。
问题是您没有正确实施 HTTP。您需要对 RFC 2616 及其后续版本有很好的了解,尤其是关于内容长度添加传输编码的部分。这段代码没有展示任何关于它的知识。
为什么要在处理请求之前发送 HTTP 200?您怎么知道请求处理器不想 return 不同的代码?
我在使用 Java SocketServer 时遇到问题,我正在构建一个非常基本的 Java 处理的 Web 服务器。
所以我正在创建一个套接字服务器,这是来自 运行 方法的代码,因为我已将服务器线程化。
我遇到的问题是服务器代码似乎冻结为 while((line = reader.readLine()) != null)
,直到远程客户端关闭连接。我正在使用 chrome 插件 ARC(高级 REST 客户端)进行测试。
public void start() throws ServerException{
this.running = true;
try{
this.server = new ServerSocket(this.port);
}catch(IOException ex){
System.err.println("Failed to create Server Port is inuse!");
throw new ServerException("Unable to bind to port '" + this.port + "'", ex);
}
while(!isStopped()){
Socket client = null;
try{
client = this.server.accept();
}catch(IOException ex){
if(isStopped()) {
return;
}
throw new ServerException("Error accepting client connection", ex);
}
new Thread(
new ServerWorker(client, this)
).start();
}
}
这是 ServerWorker
public void run(){
try {
InputStream input = clientSocket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
OutputStream output = clientSocket.getOutputStream();
long time = System.currentTimeMillis();
ArrayList<String> lines = new ArrayList<String>();
String line;
while((line = reader.readLine()) != null){
lines.add(line);
}
String[] msg = new String[lines.size()];
msg = lines.toArray(msg);
output.write("HTTP/1.1 200\r\n".getBytes());
output.write("\r\n".getBytes());
new HandleConnection(msg).begin(output);
reader.close();
output.close();
System.out.println("Request processed: " + time);
} catch (IOException e) {
}
}
最后是处理程序:
public void begin(OutputStream output){
for(int i = 0; i < lines.length; i++){
System.out.println(lines[i]);
}
try{
output.write("{\"Response\":\"Ok\"}\r\n".getBytes());
}catch(IOException e){}
}
The problem i'm having is that the server code seems to freeze as
while((line = reader.readLine()) != null)
until the remote client closes the connection.
这不是问题:这是正确的指定行为。
问题是您没有正确实施 HTTP。您需要对 RFC 2616 及其后续版本有很好的了解,尤其是关于内容长度添加传输编码的部分。这段代码没有展示任何关于它的知识。
为什么要在处理请求之前发送 HTTP 200?您怎么知道请求处理器不想 return 不同的代码?