detect disconnetced socketchannel on writes

Hi there,
I am writing to a socketchannel whose receiving peer may disconnect in between writes. The problem now is, I can still write small amouts* of data to the channel without an error, even if the receiving side is not connected anymore. So my question is: can I safely detect if a connection has been dropped without an extra acknowledge mechanism?

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.

James Kirk

Greenhorn

Posts: 7

posted 6 years ago

Stephan van Hulst wrote:Hi James, welcome to CodeRanch!

I don't think you can. Is there a reason you want to do this anyway?

Yes of course. I need to detect failing peers. As I regularly send data to them anyway, I would like to use this communication to ensure the receiver actually is still there. It basically works, but you have to send big enough data so all the involved buffers get flushed. I want to avoid a scenario where I have to send a confirmation back if the message is small.

James Kirk wrote:Yes of course. I need to detect failing peers. As I regularly send data to them anyway, I would like to use this communication to ensure the receiver actually is still there. It basically works, but you have to send big enough data so all the involved buffers get flushed. I want to avoid a scenario where I have to send a confirmation back if the message is small.

I don't know if it helps, but this from the SocketChannel docs:
"Shutting down the output side of the channel by invoking the shutdownOutput method of an associated socket object will cause further writes on the channel to throw a ClosedChannelException."
There is also isConnected(), but not having used the class myself I don't know whether it's what you want.

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here

Stephan van Hulst

Saloon Keeper

Posts: 8311

148

posted 6 years ago

Yes, but he wants to detect failing peers, not peers that are finished with what they're doing.

James, there is no way you can do this in Java. You could write complex native code that checks whether you received TCP ACKs, if that is even possible, but it wouldn't be worth it.

Maybe what you can do is have some sort of heartbeat. At intervals send a message to a client and the client has to respond within a certain amount of time. This of course is not an elegant solution. And if your application already sends data regularly, sending an application level acknowledgment is roughly equivalent.

So you need to figure out for yourself whether you want to do these acknowledgments or not. You can still detect whether clients have dropped if you send data regularly, and the connection will just fail after a while. This should be good enough for statistics. If you want more security than that, you will have to use an ack mechanism.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.

James Kirk

Greenhorn

Posts: 7

posted 6 years ago

Stephan van Hulst wrote:Yes, but he wants to detect failing peers, not peers that are finished with what they're doing.

James, there is no way you can do this in Java. You could write complex native code that checks whether you received TCP ACKs, if that is even possible, but it wouldn't be worth it.

Maybe what you can do is have some sort of heartbeat. At intervals send a message to a client and the client has to respond within a certain amount of time. This of course is not an elegant solution. And if your application already sends data regularly, sending an application level acknowledgment is roughly equivalent.

So you need to figure out for yourself whether you want to do these acknowledgments or not. You can still detect whether clients have dropped if you send data regularly, and the connection will just fail after a while. This should be good enough for statistics. If you want more security than that, you will have to use an ack mechanism.

Thanks for your suggestion. This is my wore case solution, because I want to avoid any extra traffic.