1

I made an java-application which has a client- and a server-side. Both sides communicate via sockets. This works well until my server application is killed by something and can't close or shutdown the serversocket.

The client does not seem to notice the broken connection and just hangs itself while trying to read the next object.

I also tried sending a test object from the client every 5 seconds to detect that the server is offline, but that also does not work.

I might have to mention this only occurs when running the server app on Windows and the client on Linux (Ubuntu in VirtualBox). Windows-Windows works fine. Netstat even gives me an ESTABLISHED on Linux, although I already killed the server.

Client code:

requestSocket = new Socket("192.168.1.3", 1234);
out = new ObjectOutputStream(new CipherOutputStream(requestSocket.getOutputStream(), ec));

in = new ObjectInputStream(new CipherInputStream(requestSocket.getInputStream(), dc));

new Thread() {
    public void run() {
        while(true) {
            try {
                out.writeObject(obj);
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("sent");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {}
        }
    }
}.start();

Server code:

serverSocket = new ServerSocket(1234);
socket = serverSocket.accept();
out = new ObjectOutputStream(new CipherOutputStream(clientSocket.getOutputStream(), ec));
in = new ObjectInputStream(new CipherInputStream(clientSocket.getInputStream(), dc));

//do-while-reading on the socket[...]

I read multiple threads which told me how to detect a lost connection on the server side, but found none for the client side or the answers did not work for me.

c4tz
  • 21
  • 1
  • 7
  • 1
    From my understanding, if the server side listening socket it still bound, either the app didn't actually exit completely when it crashed, if it had the port should have been freed up by the OS, or a child process of the server is still running. If the latter is true, its possible for the port to still be bound, even though the process that bound it has terminated, because the child process(es) are still running. Does your server spawn child process? If the server has actually exited, you should receive an exception by attempting to write to the outputstream on the socket client side. – Mark W Oct 09 '14 at 19:34
  • Netstat on windows does not show any connection after I close the sever application. So I guess the port is not bound anymore, am I right? Also, yes I spawn a child thread/task via javafx for every new connection. – c4tz Oct 09 '14 at 19:56
  • @blkchockr Windows [can hold connections open](http://superuser.com/a/217353/156822) when there is still data to send. Have you tested Linux-Linux? – nemo Oct 09 '14 at 20:35
  • I did not test Linux-Linux yet, as my Server needs at least JDK 8u40 to run due to using some javafx stuff and I didn't look into installing early release version on Linux yet. In the post you mentioned the author is talking about a few minutes and having data left to be read, but I flush my stream after every write and also waited for almost an hour. Will test Linux-Linux as soon as I get to it, but I also want to be able to have cross-platform connections! – c4tz Oct 09 '14 at 21:03

2 Answers2

0

Set a read timeout on the socket, of suitable duration, enough to include all normal transfers, and catch SocketTimeoutException.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • This is not really an option for me, as the input usually is made on the server application. Because of this scheme, I need to have a constant connection to the server and try to reconnect as soon as it is lost. – c4tz Oct 10 '14 at 14:35
  • OK, so if you're only writing, catch `IOException` and take it as a signal that the connection is hosed. The only `IOException` that doesn't mean that is `SocketTimeoutException`. – user207421 Oct 13 '14 at 09:29
  • My problem was that I didn't get any Exception at all. Then stream just hung himself up while reading, not noticing there was no connection anymore. – c4tz Oct 13 '14 at 10:22
0

The problem seemed to be the VM. When testing it on my Laptop with Manjaro Linux, everything worked as it should have in the beginning!

Thank you for your contributions anyway. :)

c4tz
  • 21
  • 1
  • 7