Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

detect disconnetced socketchannel on writes

 
James Kirk
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

This old post seems to be related: http://www.coderanch.com/t/552684/java/java/Java-NIO-socket-communication-why

* this may be related to the size of the socket send buffer

Many TIA,
JK
 
Stephan van Hulst
Bartender
Pie
Posts: 5912
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James, welcome to CodeRanch!

I don't think you can. Is there a reason you want to do this anyway?
 
James Kirk
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Cheers,
JK
 
Winston Gutkowski
Bartender
Pie
Posts: 10430
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stephan van Hulst
Bartender
Pie
Posts: 5912
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
James Kirk
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic