renodino has asked for the wisdom of the Perl Monks concerning the following question:

Can't find a definitive answer... I've got a small web server embedded in an app. When I connect from a local browser, I frequently get "The document contains no data" alerts from firefox. So I've injected "sleep 0.1" between sending the page and close()'ing, and the alert goes away. Presumably, the right thing to do is set SO_LINGER, but if I set (via IO::Socket::INET):
$fd->setsockopt(SOL_SOCKET, SO_LINGER, pack('l', 1));
the server hangs. Some google pages indicated Win32 needs shorts, not longs, so I've tried
$fd->setsockopt(SOL_SOCKET, SO_LINGER, pack('S', 1));
No help. And I thought that LINGER required 2 args, so I've tried
$fd->setsockopt(SOL_SOCKET, SO_LINGER, pack('ll', 1, 1));
and
$fd->setsockopt(SOL_SOCKET, SO_LINGER, pack('ss', 1, 1));
...with no success. What am I doing wrong ???

Replies are listed 'Best First'.
Re: Setting SO_LINGER on Windows
by BrowserUk (Patriarch) on Nov 06, 2005 at 05:15 UTC

    This and this may shed some light on how MS think these things should work.

    (And gently point out that Time::HiRes::sleep() takes a real argument )


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Setting SO_LINGER on Windows
by sgifford (Prior) on Nov 05, 2005 at 23:23 UTC
    Unless Windows behaves very differently from Unix, you shouldn't need to set SO_LINGER for this. By default all data will be sent when the socket is close'd. I'd start by looking elsewhere for the bug, and possibly implementing a very very small program to see if it can reproduce the problem. If you can reproduce it in a few lines, post what you come up with.
Re: Setting SO_LINGER on Windows
by caedes (Pilgrim) on Nov 06, 2005 at 04:22 UTC
    I think it should be noted that "sleep" only functions on integer time increments, so most likely it is not the time interval which causes the bug to go away. In order to further diagnose the issue a bit more of your code would be much appreciated.

    - caedes

      I think it should be noted that "sleep" only functions on integer time increments...

      Unless you use Time::HiRes...

      Perhaps I need to rephrase my original question:

      What's the proper way to set SO_LINGER ? Ancient memories of Unix and TLI lead me to believe I need 2 values: a flag to enable, and another value to set the timeout. Based on the perl examples I've surfaced thus far, approx. 50% are doing it the wrong way..