简体   繁体   English

客户端断开连接时服务器挂起

[英]Server hangs when client disconnects

i am creating a multiple-client/server app whenever any client disconnects from my server it just hangs. 每当任何客户端从我的服务器断开连接时,我都将挂起一个多客户端/服务器应用程序。 how can i set any condition that will tell me print some message whenever any client disconnects from the server here is my server code 我如何设置任何条件,只要有任何客户端与服务器断开连接,这将告诉我打印一些消息,这是我的服务器代码

class ServerThread implements Runnable {

    public void run() {
        Socket socket = null;
        try {
            System.out.println("server starting.......");
            serverSocket = new ServerSocket(SERVERPORT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        while (!Thread.currentThread().isInterrupted()) {

            try {
                System.out.println("Ready to accept.......");
                socket = serverSocket.accept();

                System.out.println(" client Connected with ip address =" +socket.getRemoteSocketAddress().toString());

                CommunicationThread commThread = new CommunicationThread(socket);
                new Thread(commThread).start();

            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("catch block");

            }

        }
    }
}

class CommunicationThread implements Runnable {

    private Socket clientSocket;

    private BufferedReader input;

    public CommunicationThread(Socket clientSocket) {

        this.clientSocket = clientSocket;

        try {

            this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void run() {


        while (!Thread.currentThread().isInterrupted()) {

            try {

                String read = input.readLine();

                updateConversationHandler.post(new updateUIThread(read));

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

any help will be appreciated 任何帮助将不胜感激

It's not clear whether you mean disconnect because the conversation was over (ie: everything completed successfully) or the disconnect is because of some network problems (or the client canceled the request). 目前尚不清楚您是要断开连接是因为对话已结束(即,一切都已成功完成),还是断开连接是由于某些网络问题(或客户端取消了请求)。

If it's the first case, then it's easy: the protocol you are using (your own, or http, or whatever) is in charge of defining how to determine that a conversation was over. 如果是第一种情况,那就很简单:您使用的协议(您自己的协议,http或其他协议)负责定义如何确定对话是否结束。 If that situation arises, then you just close the socket. 如果出现这种情况,则只需关闭插座即可。

If it's the second case, then you'd have to have an algorithm in place to determine whether or not the connection must be closed. 如果是第二种情况,则必须使用适当的算法来确定是否必须关闭连接。 For instance, by implementing a timeout, or a slow-read threshold. 例如,通过实施超时或慢速读取阈值。 Take a look at the Socket's javadoc for instructions on how to set a timeout. 查看Socket的javadoc,了解有关如何设置超时的说明。

It's also worth noting that it's fine to create your own servers when you want to practice or learn something, but you'd be better off using an existing solution, like vert.x or a slimmed down version of Wildfly, for instance. 还值得注意的是,当您想练习或学习一些东西时创建自己的服务器是可以的,但是最好使用现有的解决方案,例如vert.x或精简版的Wildfly。 The overhead of such servers is very low, nowadays, while still providing very robust networking capabilities. 如今,此类服务器的开销非常低,同时仍提供非常强大的联网功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM