Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Trapping socket error in client when server goes away

by perlcapt (Pilgrim)
on Oct 20, 2004 at 15:14 UTC ( #400887=note: print w/replies, xml ) Need Help??

in reply to Trapping socket error in client when server goes away

For checking the state of a socket, I prefer to the system call version of select RBITS, WBITS, EBITS, TIMEOUT. It requires a little more setup using vectors and such, but the result is finer control, IMHO. -ben (perlcapt)
# socket is already setup, and client accepted # set up masks for use by select my ($rin,$win,$ein) = ("","",""); my ($rout,$wout, $eout) = ($rin, $win, $ein); vec($rin, fileno($client),1) = 1; vec($win, fileno($client),1) = 1; $ein = $rin | $win; # -- code deleted -- # wait until the client is ready for writing if(select(undef,$wout = $win,undef,$timeout)) { # <$client> or sysread } # -- code deleted -- # or # wait until client is ready for writing to if(select(undef,$wout=$win,undef,$timeout)) { # print client or syswrite }

Replies are listed 'Best First'.
Re^2: Trapping socket error in client when server goes away
by thospel (Hermit) on Oct 20, 2004 at 15:23 UTC
    Use of select does not solve the original problem, but just replaces it with a race condition. A socket can get closed between select returning writability and you doing the write. In that case he will run into exactly the same problem, the program will exit with a broken pipe message. It will just be more rare since you made the problem window smaller.

    Also, if you go select based, do not use <> (readline) and print (ok, you can if you are very careful). select only guarantees one read or write respectively (and even that one isn't absolutely guaranteed) These buffered operations can use multiple reads and writes.

      So in other words, expect the read/write to fail. That way, if/when it does, the program won't die

      No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://400887]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2017-12-12 05:11 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (326 votes). Check out past polls.