user1480139
user1480139

Reputation: 383

closing client let the server crash

I've a simple client and server on android. Everything works fine except when I close the client app then the server stops working the app closes.

I think it's about not closing the socket. But when I close the socket in the client the server still stops working.

I'm running a thread on the server. This is my server code:

this.serverThread = new Thread(new ServerThread());
        this.serverThread.start();

    }


    @Override
    protected void onStop() {
        super.onStop();
        try {
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    class ServerThread implements Runnable {

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

                try {

                    socket = serverSocket.accept();

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

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

    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();
                }
            }
        }

    }

    class updateUIThread implements Runnable {
        private String msg;
        private boolean feedback = false;

        public updateUIThread(String str) {
            msg = str;
        }

        @Override
        public void run() {
            parseCommand();
            if(feedback)
            {
                textFeedback.setText(msg);
                feedback = false;
            }
            else
            {
                textTv.setText(msg);
            }
        }

Upvotes: 0

Views: 171

Answers (1)

user1480139
user1480139

Reputation: 383

if(msg != null)
   parseCommand();

and in

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

        try {

            String read = input.readLine();

            if(read == null)
            {
                clientSocket.close();
                Log.d("Test","SOCKET CLOSED");
                return;
            }

            updateConversationHandler.post(new updateUIThread(read));

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

Upvotes: 1

Related Questions