Java: Socket close connection and ObjectInputStream?

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?

Up vote 0 down vote favorite share g+ share fb share tw.

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 java sockets connection objectinputstream link|improve this question edited Oct 12 '09 at 18:51OscarRyz48.6k1392195 asked Oct 12 '09 at 18:38Martijn Courteaux11.7k31968 92% accept rate.

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.

Thread. Sleep(2) is needed for the program – Martijn Courteaux Oct 12 '09 at 18:55 The End-of-connection messge was the solution thank you. – Martijn Courteaux Oct 12 '09 at 19:04 That's good.

Glad to know it's working now. – Brian Agnew Oct 12 '09 at 19:14.

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...

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions