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

Re^2: Timeouts: Any alternative to alarm in Win32?

by Anonymous Monk
on Dec 04, 2008 at 02:01 UTC ( [id://727850]=note: print w/replies, xml ) Need Help??


in reply to Re: Timeouts: Any alternative to alarm in Win32?
in thread Timeouts: Any alternative to alarm in Win32?

Okay, so I have now had a chance try this under both Win32 and Linux. The results are the opposite of my original code. This code successfully times out under Win32, but not under Linux. You have to wait 20+ seconds if you connect to a non responsive host.

I suppose I could write two separate subs and key off which OS it's running under. Or incorporate the alarm code (that works under Linux) into this for a belt-and-suspenders approach to breaking the call. So I do have a workable, if not gangly, solution.

But can you see a reason why this shouldn't also work under Linux?

  • Comment on Re^2: Timeouts: Any alternative to alarm in Win32?

Replies are listed 'Best First'.
Re^3: Timeouts: Any alternative to alarm in Win32?
by BrowserUk (Patriarch) on Dec 04, 2008 at 03:36 UTC
    But can you see a reason why this shouldn't also work under Linux?

    Are ioctls platform portable? I have no idea. There is probably a POSIX-architected method for switching the blocking/non-blocking state of a socket. From a quick scan, maybe sockopt() in IO::Socket?

    To be honest, I think I would just use if( $^O == 'mswin32' ) {...} else {...} and have done with it.

    <Aside:>

    When you write cross-platform code, there will always be conditional code, but the lower the level at which you try to apply the conditional: a) the more frequent it will be required; b) the harder it will be to maintain.

    I've seen code (some of it in the perl core, others in perl modules), where every 3 or 4 lines of actual operational code is wrapped in some platform conditional. The result is that sometimes half a source files 'weight' is the compile-time conditions themselves. This is a nonsense.

    Far better to have one file per platform (or flavour of), and a small file that conditionally incorporates the appropriate one at compile-time. Each platform dependant file is free of conditionals and can fulfill its function 'natively' without regard to the constraints of others.

    People do (and will) claim that this violated the once and once only principle, and it does. But the benefits outweight the drawback--especially when it comes to maintainance, which is the "people's beef against it--because the clarity is provides, and the freedom from artificial constraint it avails, far outweights the cost of c&p code re-use.

    (Sigh!}(Sorry merlyn). This is one one those truths that freshly minted CS-majors will not understand, and will argue against vociferously, until they are no longer newly minted, and are encountering their (5th, 15th, 50th) CS-dogma-optimal, every-3rd-line-is-an-OS-conditional piece of code. And then they'll recognise the wisdom behind these words.

    But that takes 5 to 15 years of post-college experience, and those prepared to entertain debates on those (extended) timescales are few and far between these days. In the advertising trade, they attempt to measure and maximise our google;//advertising "attension span" (It's annoying, but illuminating to follow and read a few of those links),.

    Depending upon which links you choose to read, the consesus (wet finger in the air), is that it has dropped from around 12 minutes in the 1960s, to around 5-75 seconds today depending upon context. Computers are very good (and very fast) and following deeply nested sets of rules and conditions. Human beings are not. The programmer that can maintain a sense of which of 2 or 3 conditionals is concurrent at any given time, is a very rare animal. Following 4 or 5 compile-time conditions in a single file in neigh impossible. Six is!

    <Rant off>


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Again, thanks for giving me a solution that will work. I'll put in conditional code if that's what it takes. I have just been surprised and pleased that I've been able to avoid it so far.

      I've got a full GUI program with lots of widgets (in TK) and a lot of network-based functionality that is 95% complete, and working on both platforms using the same code. I wanted to see how close I could get to 100%, and didn't expect to get this far. But even if I've got to add conditional code to a subroutine, all the high-level code is still nice and clean. And that's good enough for me.

      I am highly impressed with the response of the perlmonk community.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://727850]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-20 03:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found