readLine()循环不会退出,直到远程客户端关闭连接 [英] readLine() loop not exiting until remote client closes connection

查看:109
本文介绍了readLine()循环不会退出,直到远程客户端关闭连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了Java SocketServer的问题,我正在构建一个非常基本的Java处理Web服务器。

I'm having a problem with a Java SocketServer, i'm building a very basic Java handled Web Server.

所以我正在创建一个套接字服务器,这是run方法中的代码,因为我已经使服务器线程化了。
我遇到的问题是服务器代码似乎冻结为而((line = reader.readLine())!= null)直到远程客户端关闭连接。我正在使用chrome插件ARC(高级REST客户端)进行测试。

So i'm creating a socket server, this is the code from the run method as i have made the server threaded. 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. I'm using the chrome plugin ARC (Advanced REST Client) to do the testing with.

   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

This is the 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){}
}


推荐答案


我遇到的问题是服务器代码似乎冻结为而((line = reader.readLine())!= null)直到远程客户端关闭连接。

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.

这不是问题:这是正确的指定行为。

That's not a problem: it is the correct, specified behaviour.

问题是您没有正确实现HTTP。您需要熟悉RFC 2616及其后续版本,尤其是有关内容长度添加传输编码的部分。此代码没有任何相关知识。

The problem is that you aren't implementing HTTP correctly. You need a good knowledge of RFC 2616 and its successors, particularly the parts about content length add transfer encoding. This code doesn't exhibit any knowledge of it whatsoever.

例如,为什么在处理请求之前发送HTTP 200?您如何知道请求处理器不想返回不同的代码?

And why for example are you sending HTTP 200 before processing the request? How do you know the request processor won't want to return a different code?

这篇关于readLine()循环不会退出,直到远程客户端关闭连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆