简体   繁体   中英

client/server connection closing causes loop error

I got to stage where client and server communicate, sending messages from and to each other.

The problem I am having is how to close the connection without causing an error?

If I terminate one of the apps (either server or client) that causes the connection to be lost, and then it causes the loop that is waiting for input to loop indefinitely and showing null's.

I tried closing sockets, buffers and even the thread, didn't work.

This is the client side

public void onClick(View view) {
    try {
        EditText et = (EditText) findViewById(R.id.EditText01);
        String str = et.getText().toString();
        PrintWriter out = new PrintWriter(new BufferedWriter(
                new OutputStreamWriter(socket.getOutputStream())), true);
        out.println(str);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } 
}

class ClientThread implements Runnable {

    @Override
    public void run() {

        try {
            InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
            socket = new Socket(serverAddr, SERVERPORT);
            CommunicationThread commThread = new CommunicationThread(socket);
            new Thread(commThread).start();
        } catch (UnknownHostException e1) {
            e1.printStackTrace();
            } catch (IOException e1) {
            e1.printStackTrace();
                } 
    }
}

This is the server side

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

    }
}

Both use these classes:

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

                //***HERE EXTRA BIT FOR THE SERVER


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

class updateUIThread implements Runnable {
    private String msg;
    public updateUIThread(String str) {
        this.msg = str;
    }

    @Override
    public void run() {
        text.setText(msg);
        }
}

the only difference is the server has this bit where it says above ***HERE EXTRA BIT FOR THE SERVER

        PrintWriter out = new PrintWriter(new BufferedWriter(
                new OutputStreamWriter(socket.getOutputStream())), true);
        out.println("Message recieved");

so basically, client connects, server accepts, then client sends message, servers receives message and shows it, and then sends "Message received" to the client, and the client shows it.

All this works fine, but once the connection is lost, they hang on showing null repeatedly, and I have to force the app to close.

You aren't checking for end of stream. If readLine() returns null, the peer has closed the connection, and you must do likewise and stop reading.

It's hard to believe you really need a new thread for every line to update the UI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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