views:

548

answers:

2

Hi,

My code looks like this, this is Server code (Runnable)

public void run() {
    while (true) {
        try {
            System.out.println("Waiting for client...");
            this.socket = serverSocket.accept();
            System.out.println("Client accepted");
            while (true) {
                readCommand();
                try {
                    Thread.sleep(2);
                } catch (Exception e) {
                }
            }
        } catch (Exception ex) {
            Main.error("Server stopped for current host", ex);
        }
    }
}

My problem is: when the client closes the connection, he still waiting to read the command in readCommand(); until a command was sent and an EOFException will be thrown. This is my readCommand method:

private void readCommand() throws Exception {
    Object o = new ObjectInputStream(socket.getInputStream()).readObject();
    if (o instanceof Command) {
        ((Command) o).execute();
        return;
    }
    if (o instanceof Recorder) {
        System.out.println("Recording received");
        ((Recorder) o).executeRecord();
        return;
    }
    if (o instanceof MyKeyEvent) {
        ((MyKeyEvent) o).execute();
        return;
    }
}

So I think before reading he have to check if the connection is open. I hope I've explained well. Maybe bad English (14 years old, Dutch speaking).

Edit: If I evaluate the run-method code, the EOFException is trown in the try-catch-body. But he stops accepting clients.

Thanks

+2  A: 

An EOFException is exactly what I'd expect if the client closes the connection. So I'm not quite sure what your problem actually is. Perhaps you need to distinguish between an expected client disconnect and an unexpected client disconnect. In which case you should perhaps send some sort of End-of-message object to mark the end of transmission ?

If you want to check if info is available, there's an available() method on the stream. I don't think you need your Thread.sleep(2), btw, since I would expect a read on a valid input stream to hang in the absence of data.

Brian Agnew
`Thread.sleep(2)` is needed for the program
Martijn Courteaux
The `End-of-connection` messge was the solution thank you.
Martijn Courteaux
That's good. Glad to know it's working now.
Brian Agnew
A: 

You can also "just" add another try-catch to catch just the EOFException

public void run() {
    while (true) {
        try {
            System.out.println("Waiting for client...");
            this.socket = serverSocket.accept();
            System.out.println("Client accepted");
            try {
                while (true) {
                    readCommand();
                    try {
                        Thread.sleep(2);
                    } catch (Exception e) {
                    }
                }
            } catch (EOFException ex) {
                System.out.println("client closed");
            }
        } catch (Exception ex) {
            Main.error("Server stopped for current host", ex);
        }
    }
}

sure, the End-of-command is much smarter/elegant...
[]]

Carlos Heuberger