• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

detect disconnetced socketchannel on writes

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: https://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
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic