-1

For some reason, our program chooses BLOCKING mode for tcp socket, I known it's a bad design, but I can't make too many modifications now.

The problem is, when peer is powered down, recv call in our program will hang, is there any workaround? Does SO_KEEPALIVE option help or should I have to implement some heart-beat machanism?

Thanks.

dejunl
  • 57
  • 1
  • 8
  • 1
    if you know something Is 'bad design' then fix it! – Mitch Wheat Apr 17 '14 at 04:13
  • 1
    Mitch is right, improving the design will serve you better in the long run... but if you can't do it, you might put a timeout on your send() and recv() calls as shown here: http://stackoverflow.com/a/4182564/131930 – Jeremy Friesner Apr 17 '14 at 04:18
  • It's not a bad design. 'Feel painful' isn't a problem description. You haven't specified a programming language. -1 – user207421 Apr 17 '14 at 08:09

2 Answers2

1

Set a read timeout on the socket. As you haven't specified a language it is impossible to tell you exactly how, but at the base level it is the socket option SO_RCVTIMEO. Make it long enough that it has to be a peer failure if it occurs.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

You can do try the following:

Identify why its difficult to turn the socket to non-blocking mode. What is preventing you from changing bad design? Fear of too much modification and greater entropy that it might break things that work? I would recommend a design fix rather than live with it.

TCP KEEP-ALIVE is an option to determine if the peer is available or not.

If you can modify the peer application code then having a application level exchanging of heart beats should help. But how do you get out of blocked recv() without timer? Hence the next one.

Prevent eternal block on the socket. Have timeouts set in the for send() and recv().

Prabhu
  • 3,443
  • 15
  • 26