Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

It depends on what you mean by "closed".

If you want to detect the case where the other end has properly disconnected, it's pretty easy: try to select on the filehandle (the 4-argument form), it will return as being ready, then an attempt to read will return eof.

If you want to include the case where the other end may have just fallen off the 'net without properly disconnecting, you have to actually write some data to the socket. TCP doesn't exchange periodic state messages, so it's impossible to tell if an absent client is disconnected or just silent. Many protocols provide NOOP-type functions for this purpose. The exception is if you use the SO_KEEPALIVE option, in which case you'll be informed of a timeout after an hour or two.

sysread shouldn't give up unless it knows there is data pending, but it receives nothing for a long time. This doesn't tend to happen in real life unless the other end really has gone away for a while (a few minutes). The details may be system dependent; you're getting into TCP/IP Illustrated territory, and I don't feel like re-reading the (verbose) details.

In reply to Re: Detecting a closed socket by sgifford
in thread Detecting a closed socket by Flame

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2021-06-20 21:55 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (95 votes). Check out past polls.