aspose file tools*
The moose likes Java in General and the fly likes detect disconnetced socketchannel on writes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "detect disconnetced socketchannel on writes" Watch "detect disconnetced socketchannel on writes" New topic
Author

detect disconnetced socketchannel on writes

James Kirk
Greenhorn

Joined: Dec 12, 2011
Posts: 7
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

Joined: Sep 20, 2010
Posts: 3649
    
  17

Hi James, welcome to CodeRanch!

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

Joined: Dec 12, 2011
Posts: 7
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

Joined: Mar 17, 2011
Posts: 8427
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

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

Joined: Dec 12, 2011
Posts: 7
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: detect disconnetced socketchannel on writes