Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Answer: How do i detect that the client has disconnected ? (i'm using sysread/write)

( #116071=categorized answer: print w/ replies, xml ) Need Help??

Q&A > network programming > How do i detect that the client has disconnected ? (i'm using sysread/write) contributed by pjf

You can use getpeername() to reliably tell if your socket is connected or not, if it returns undef, you don't have a connection. This is exactly how IO::Socket handles things.

The code that you've written should work, although it will close the socket on any connection, not just the connection going away. In particular, if your socket is in non-blocking mode, syswrite can return errors (EAGAIN) if the other side simply isn't ready for more data.

If you are using non-blocking connections, or multiple connections at once, then the chances are you'll find the built-in function select, or the module IO::Select, very handy for telling when it's time to read/write to your sockets. The Perl Cookbook has a good example of this in section 17.13 (Non-Forking Servers).

Hope you find the above useful. Let me know if it doesn't solve your problem.

Cheers,
Paul

Comment on Answer: How do i detect that the client has disconnected ? (i'm using sysread/write)
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2014-11-28 03:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (192 votes), past polls