Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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]
[LanX]: OH
[LanX]: you are talking about signatures?
[GotToBTru]: yes, it turns out I am
[LanX]: http://perldoc. html#Prototypes
[GotToBTru]: that page, strangely enough, never mentions that construct
[LanX]: ; is for optional prototypes
[LanX]: & is codeblock @ array
[GotToBTru]: okay, that is there in the text, not in literal use in any of the examples
[GotToBTru]: the text suggest it is redundant before @
[LanX]: propably ... I also notice new stuff

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2017-01-18 21:16 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (164 votes). Check out past polls.