in reply to Problems with IO::Select

Please, post a minimal, working example so we can play with it (or in this case, a not working example ;)). If you're lucky, you'll find the problem yourself. Don't forget to check How do I post a question effectively? out.

--
David Serrano

Replies are listed 'Best First'.
Re^2: Problems with IO::Select
by dirtdart (Beadle) on Jul 02, 2006 at 20:29 UTC
    OK. Here goes. This is going to be a LOT briefer than the full code because at this point the program itself is turning out to be a lot bigger than I anticipated. Hopefully, it will give you an idea of what I'm doing, though.
    use strict; use IO::Socket; use IO::Select; #Just opens the configuration file and stuffs all the values into the +%config hash my %config = get_config($ARGV[0] ? $ARGV[0] : "/etc/MailScanManager/Ma +ilScanManager.conf"); my $sel = new IO::Select; #$config{connections} is an array of hashes containing information abo +ut each listening socket #the server should create foreach my $connection (@{$config{connections}}) { if($connection->{socket_type} & IP_SOCKET) { if(!$connection->{socket_address}) { $connection->{socket_addr +ess} = INADDR_ANY; } if(!$connection->{socket_port}) { $connection->{socket_port} = + 7000; } $connection->{socket} = new IO::Socket::INET(Listen => 1, Loca +lPort => $connection->{socket_port}, LocalAddr => $connection->{socke +t_address}) || die("Could not create socket $!");; $sel->add($connection->{socket}); } if($connection->{socket_type} & UNIX_SOCKET) { if(!$connection->{address}) { $connection->{address} = "/var/r +un/MailScanManager/msm.sock"; } unlink($connection->{address}); $connection->{socket} = new IO::Socket::UNIX(Type => SOCK_STRE +AM, Listen => 5, Local => $connection->{address}) || die("Could not c +reate socket $!"); chmod 777, $connection->{address}; $sel->add($connection->{socket}); } } while(my @ready = $sel->can_read) { foreach my $fh (@ready) { #Determine if it's a connection attempt or data sent by a client and a +ct appropriately } }

    I have run this code using TCP/IP sockets and it works perfectly. Select sits and waits for one of the sockets to be readable and then reacts correctly. It only seems to act up when I'm using it with a Unix socket. I haven't tried a combination of the two. I also haven't tried a different path for the Unix socket, but as I stated earlier, it's getting created and the permissions on the directory and file are set to 777, so I don't think that's the problem.

      The request was to "Please, post a minimal, working example so we can play with it". What you posted is neither minimal (we shouldn't need get_config or a config file) nor working (get_config is missing. A sample config file is missing. IP_SOCKET is not defined. UNIX_SOCKET is not defined.)

      The following works (can_read doesn't return) for me. Does it do the same for you? If so, your problem has nothing to do with the socket or select.

      use strict; use warnings; use IO::Socket; use IO::Select; my $sel = IO::Select->new(); my $address = "mysock"; unlink($address); my $sock = IO::Socket::UNIX->new( Type => SOCK_STREAM, Listen => 5, Local => $address ) or die("Unable to create socket $!\n"); chmod 777, $address; $sel->add($sock); while(my @ready = $sel->can_read) { warn; }

        I assumed, when asked for a sample, that one would have meant a sample of the program I'm working with, not just some other program I made up. Next time I guess I'll know.

        However, the above code does the exact same thing. as soon as it reaches can_read, it craps out with no discernable error and no output.