Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 套接字读取()和就绪()互锁,客户端断开连接检测_Java_Sockets_Tcp_Client_Disconnect - Fatal编程技术网

Java 套接字读取()和就绪()互锁,客户端断开连接检测

Java 套接字读取()和就绪()互锁,客户端断开连接检测,java,sockets,tcp,client,disconnect,Java,Sockets,Tcp,Client,Disconnect,好的,我的问题很简单。我试图进行简单的聊天,但我觉得检测断开连接的客户端和服务器是必须的。当我使用simple时,Chat工作正常(没有这种检测): if (this.in.ready()) //preinitialized buffered reader this.dataIn=this.in.readLine(); 我浏览了这里发布的许多网站/问题,我读到,ready()应该被删除,因为它阻止了一切,这可能是真的,因

好的,我的问题很简单。我试图进行简单的聊天,但我觉得检测断开连接的客户端和服务器是必须的。当我使用simple时,Chat工作正常(没有这种检测):

                if (this.in.ready())  //preinitialized buffered reader
                    this.dataIn=this.in.readLine();
我浏览了这里发布的许多网站/问题,我读到,
ready()
应该被删除,因为它阻止了一切,这可能是真的,因为当我删除这个
ready()
时,我的聊天不再工作,但是它启用了客户端断开连接的检测

为了达到我的目标,我需要测试
BufferedReader
是否通过
readLine()
接收null,但这也不能正常工作

            if (this.in.readLine()!=null){ //1. check if client disconnected
                if (this.in.ready())       //2/
                    this.dataIn=this.in.readLine(); //3.
            }                    
            else
                this.notice="Client disconnected!";
现在,当我应用上面给出的代码时会发生什么。如果(1)正在阻塞内部
ready()
(第2行),则为初始值,这是通过套接字(3)读取实际消息发送所需的

另一个“订单”不起作用:

            if (this.in.ready()){
                this.dataIn=this.in.readLine();
            }
            else if (this.in.readLine()!=null)
                this.notice="Client disconnected!";
这一个也不允许通过套接字发送消息

*是的,发送/接收在单独的线程中实现

*BufferedReader仅初始化一次

线程源代码(如果有人需要它来查看):

最糟糕的是,我要么正确地检测到客户端断开连接,要么进行工作聊天


有谁能告诉我,为了把这两者结合起来,我应该做些什么?非常感谢您的帮助。

考虑使用
java.io.ObjectInputStream
java.io.ObjectOutputStream
。在单独的工作线程中使用blocking
read()
方法,并循环直到返回-1或引发异常。来回发送字符串对象。这样,您还可以使用换行符发送消息

    @Override
public void run() {
    try {
        if (this.isServer){            
            this.initServer();
            this.initProcessData(sSocket);
        } 
        else if (!this.isServer){
            this.initClient();
            this.initProcessData(clientSocket);
        }            
        this.initDone=true;

    } catch (IOException ex) {
        Logger.getLogger(NetClass.class.getName()).log(Level.SEVERE, null, ex);
    }

    while(this.initDone){
        try {
            Thread.sleep(100);
            if ((!this.dataOut.isEmpty())&&(this.dataOut!="")){                
                this.out.println(this.dataOut);
                this.out.flush();
                this.dataOut = "";
            }
            if (this.in.ready())
                this.dataIn=this.in.readLine();                   
        }
        catch (IOException ex) {
            Logger.getLogger(NetClass.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (InterruptedException ex) {  
            this.initDone=false;
                Logger.getLogger(NetClass.class.getName()).log(Level.SEVERE, null, ex);
        }

        //System.out.println(this.notice);
    }

}