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

I'm sort of new to shared memory and sockets, but i'm trying to write a tcp/ip listener, that reads and parses a message from a client, then stores the new message and the socket object in a shared memory variable.
I keep getting the following error:
Can't store GLOB items at blib/lib/Storable.pm (autosplit into blib/li +b/auto/Storable/_freeze.al) line 180, <GEN1> line 1, at /usr/lib/perl +5/site_perl/5.6.1/IPC/Shareable.pm line 523
Is there a way to store the socket object in the shared memory variable? Here is the code
use IPC::Shareable; use IO::Socket; my $Socket = IO::Socket::INET->new( LocalPort => 8190, Type => SOCK_STREAM, Reuse => 1, Listen => 1 ); if (not defined $Socket){ die "ERROR: cannot listen on port 8190: [$!]\n"; } my %options = ( create => 'yes', exclusive => 0, mode => 0666, destroy => 'yes' ); $IPC2 = tie $ProcessedMessages, 'IPC::Shareable', 'data', { %options } + or die "client: tie failed\n"; $ProcessedMessages = []; ... while($client = $Socket->accept()){ my $msg; $msg = <$client>; chomp $msg; my $pid = fork(); if(not defined $pid){ die "Fork Error: $!\n"; } #Child if($pid == 0){ &Process($msg, $client); exit(); }else{ close($client); } } sub Process{ my $msg = $_[0]; my $client = $_[1]; ...Do Some Message Processing... $IPC2->shlock; my %tmp = ( Message => $msg, Connection => $client ##Problem ); push(@{$ProcessedMessages},\%tmp); $IPC2->shunlock; print $client "$msg\n"; close($client); }

Replies are listed 'Best First'.
Re: Storing Socket Connections in Shared Memory
by Fletch (Bishop) on Jan 30, 2003 at 20:55 UTC

    A socket is not a resource which you can store in shared memory. There is deep UNIX mojo which will allow you to pass open file descriptors between processes, but it's somewhat involved. If you're really stuck on passing filehandles around, get a copy of Stevens' Unix Network Programming and/or Advanced Programming in the Unix Environment (I know the later talks about passing filehandles, and I think the former specifically covers tossing around sockets); you should be able to translate everything to the equivalent operations in perl.

    Consider instead doing something similar to what apache does (the fork'd children each call accept on a socket opened before they were created and shared between all of the children (with apropriate locking to make sure only one calls accept at a time)). Or fork off the child after the connection comes in, in which case it'll just inherit the socket. Or take a look at POE.

Re: Storing Socket Connections in Shared Memory
by perrin (Chancellor) on Jan 30, 2003 at 20:59 UTC
    Storable can't serialize sockets, and all of the Perl shared memory solutions use Storable. You could try doing it in C instead, and linking that in using XS. Otherwise, I think it's impossible.

      Not even C can't serialze sockets, at least not in user space and not to shared memory. Though some Unix systems (those that support STREAMS -- probably most non-BSD-based ones) will let you tell the operating system to stream an open file descriptor over a socket or pipe using ioctl() with the I_SENDFD and I_RECVFD.

                      - tye