0

I am sending message over socket as follows:

socket = new Socket(serverAddr, SERVERPORT);

PrintWriter out = new PrintWriter(new BufferedWriter(
                        new OutputStreamWriter(socket.getOutputStream())),
                        true);
out.println(str);

But after a while the messages are not delivered, and no exception is thrown (socket.isClosed, socket.isBound - both return true. ). How can i find if socket is still really alive?

Additional Information
Whenever the Server Socket (receiver) restarts, the Client Socket (sender) becomes stale - but there is no exception when messages are sent via this stale Client-Socket (they are simply lost).

So, how do i detect on the sender side that Client Socket is stale, so that i can initiate a reconnect?

Solution
PrintWriter's checkError() may be used to determine write success or failure.

Jasper
  • 8,440
  • 31
  • 92
  • 133
  • Ping it and wait for a timeout. – Vince Oct 08 '15 at 17:50
  • Vince> How does one ping (as in java) ? – Jasper Oct 08 '15 at 17:52
  • 1
    Simply send some data. That's all [pinging](https://en.m.wikipedia.org/wiki/Ping_(networking_utility)) is. You send some bytes to see if it has reached the destination, see how long it takes to get back. You'll probably wind up getting an exception, which is the usual case. To avoid these situations, have the client inform the server it's disconnecting before actually disconnecting. Other than that, there is no way to properly determine whether a client is still connected to you (as mentioned in the duplicate) – Vince Oct 08 '15 at 17:53
  • Vince> And how exactly does the sending program know if the data has reached the destination (which is just a server socket on the other side) - not sure what you mean by get back? – Jasper Oct 08 '15 at 17:56
  • 1
    The clients should ping the server in a pulse fashion, informing it they're still connected. If the server has not been pinged for a while, it means something has interrupted the connection. Check out [this](http://stackoverflow.com/questions/865987/do-i-need-to-heartbeat-to-keep-a-tcp-connection-open) answer about using a heartbeat to determine dead connections. The client pings the server ensuring it's still alive, as well as notifies the server that the client is still there. The server will then send the data back as a confirmation. – Vince Oct 08 '15 at 18:06
  • Vince> When the client socket becomes stale (this happens say when Server Socket was restarted), i am not able to detect that at the client socket end - there is no exception (messages r sent and lost). Only if i recreate the client socket, the connection works. Now i do not know when to recreate. Any ideas? – Jasper Oct 09 '15 at 07:49
  • @Jasper I don't know what 'when the `ServerSocket` was restarted' means, but there is nothing you can do to a `ServerSocket` that will affect any other `Socket` in the same process. – user207421 Oct 09 '15 at 08:39
  • @Jasper If `isClosed()` returns true, *you* closed it. IMHO you should avoid `PrintWriter`in network programming, as it swallows exceptions you need to know about. Use `BufferedWriter`. You can't reconnect a socket. You have to create a new one. – user207421 Oct 09 '15 at 21:18
  • EJB> Yes that's true.. i did finally got around this using: PrintWriter.checkError() it returns true, if there is a problem with socket, but after a couple of writes (not immediately). – Jasper Oct 10 '15 at 04:23
  • @EJP> I will try the BufferedWriter approach. I wonder if there r any downsides to using that, as i have seen this PrintWriter approach in quite a few places. – Jasper Oct 10 '15 at 04:29
  • The only downside is that you don't get any `print()` or `println()` methods. I can't answer for what other people may do, but if you're engaging in any kind of network protocol, `PrintWriter` and `PrintStream` are totally inadequate. – user207421 Oct 10 '15 at 05:46

0 Answers0