Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Checking whether or not IO::Socket::accept will block

by Vautrin (Hermit)
on Feb 16, 2004 at 23:12 UTC ( #329477=perlquestion: print w/replies, xml ) Need Help??

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

I am creating a server that needs to take the following steps in the parent thread:

  1. Check whether or not there is a client looking to connect on the local instance of IO::Socket::UNIX by calling my $SOCK = $socket->accept($socket);
  2. If there is anyone around looking to connect, connect them, fork, and create pipes to keep in touch with the parent
  3. If nobody needs to connect then send data to the children through a pipe that it opened up.

The problem I am having is that IO::Socket::accept blocks, so it will always return true. That is to say, if I call it, the program will "pause" until a client connects (even with autoflush true). I want to test whether or not there is anybody looking to connect before accepting a connection. Is this possible? I've googled and read every POD I can find on Sockets and IPC and still can't seem to find a suitable function.

Many thanks in advance,

Vautrin


Want to support the EFF and FSF by buying cool stuff? Click here.

Replies are listed 'Best First'.
•Re: Checking whether or not IO::Socket::accept will block
by merlyn (Sage) on Feb 16, 2004 at 23:26 UTC
    Well, somehow POE manages to do it just fine. You can either use POE (highly recommended if you're wanting to write multi-threading apps in one process/thread) or reverse-engineer the POE code to see how it is done.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      I'm probably just a little slow, but it took me a while to pick up on POE. I do like POE, but for simple projects that need a bit of multiplexed, non-blocking IO, I'd just use IO::Poll or IO::Select (which POE will probably optimize to internally).

      That said, I love abstraction, and POE has some good abstractions in my book. It just depends on your task.

      -- dug
Re: Checking whether or not IO::Socket::accept will block
by chromatic (Archbishop) on Feb 17, 2004 at 00:25 UTC
      I don't think so. I am not trying to worry about the reading in from the sockets and file handles -- that is what the children is for. What I want is a way to call accept -- i.e. allow a client to connect -- without having accept wait until there is a client to connect. It seems like something that must have been dealt with before, because if accept waits for a client to connect before returning the system is potentially stuck in an infinite loop.

      Want to support the EFF and FSF by buying cool stuff? Click here.

        I think you want to take a look at this.

        antirice    
        The first rule of Perl club is - use Perl
        The
        ith rule of Perl club is - follow rule i - 1 for i > 1

Re: Checking whether or not IO::Socket::accept will block
by borisz (Canon) on Feb 17, 2004 at 02:30 UTC
    purely untested, but this is how I whould start.
    use Fcntl qw/O_NONBLOCK/; fcntl ( $sock, O_NONBLOCK, my $xx );
    Boris
        Thats a lot better! Thanks.
        Boris

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://329477]
Approved by jdtoronto
Front-paged by DaWolf
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2022-05-18 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (71 votes). Check out past polls.

    Notices?