2

I have created a server-client project where the server keeps listening and prints the information. However, when i shutdown the client, the server remains open. The problem is that I need to insert this into another application, and, if the server does not close at first, the application will not open unless i kill the process in that port (but this is not an option to me). What should I do to properly close the server once the client disconnects?

Here is the code:

Server:

public class Server {

    public static void main(String[] args) {
        Connection conn = new Connection();
        new Thread(conn).start();
    }

    private static class Connection implements Runnable {
        @Override
        public void run() {
            try (ServerSocket serverSocket = new ServerSocket(5005)) {
                Socket socket = serverSocket.accept();

                listener(socket);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        private void listener(Socket socket) throws IOException {
            DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
            DataInputStream inputStream = new DataInputStream(socket.getInputStream());
            boolean alive = true;

            while (alive) {
                try {
                    outputStream.writeUTF(new Scanner(System.in).nextLine());
                    System.out.println(inputStream.readUTF());
                } catch (IOException ex) {
                    ex.printStackTrace();
                    alive = false;
                }
            }
        }
    }
}

Client:

public class Client {
    public static void main(String[] args) {
        try (Socket socket = new Socket("localhost", 5005)) {
            DataInputStream inputStream = new DataInputStream(socket.getInputStream());
            DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());

            while (socket.isConnected()) {
                System.out.println("Incoming data: " + inputStream.readUTF());

                outputStream.writeUTF(new Scanner(System.in).nextLine());
                outputStream.flush();
            }

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

Since now, thank you very much!

  • You might want to set a read timeout on the socket, so the server won't wait for something to read forever. – Kayaman Jan 02 '18 at 18:14
  • Ok, but how can I do this? – André Spironello Jan 02 '18 at 18:21
  • Look at methods in `Socket` class. – Kayaman Jan 02 '18 at 18:23
  • 1
    You cannot solve this by using serverSocket.setSoTimeout() method. Your problem is at your Scanner(System.in).nextLine() line. System waits until the user input come so even your client is disconnected it still waits. It waits not because of your ServerSocket, it is all about nextLine() command. – anL Jan 02 '18 at 19:02
  • `socket.isConnected()` does not magically become false when the peer disconnects. You will get an `EOFException` or a connection reset in this code. – user207421 Jan 02 '18 at 23:19

1 Answers1

0

The thing that force the system wait and not close is this line at your Server.java :

outputStream.writeUTF(new Scanner(System.in).nextLine());

Once it starts waiting the user input, it waits forever along the life time of the instance although your client is disconnected.

So what you can do ? You can create another thread that makes periodic "ENTER" inputs (if you insist using new Scanner(System.in)) for example input per 5 seconds. After the enter, or any other meaningful input, if you decide this is not from your client, don't write it to the client and wait user input again (if your client still connected !). If your client is not connected, just finish your loop.

Please check Java Robot class and this example

anL
  • 1,073
  • 1
  • 11
  • 31