Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

IO::Socket::INET file handler

by Razvanica (Novice)
on Aug 22, 2007 at 08:59 UTC ( [id://634278]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks,
I would like to know if it is possible to find the file handler from an IO::Socket::INET object.
The thing is that I have a server which creates child processes to deal with different messages and I need to communicate with the same client from two different processes. I tried to send the socket object through a pipe but the second process is considering it a string and I get
Can't locate object method "send" via package "IO::Socket::INET=GLOB(0x1987cf4)" (perhaps you forgot to load "IO::Socket::INET=GLOB(0x1987cf4)"?)
So I thought I should send only the file handler. Is it a good idea?
Thanks.

Replies are listed 'Best First'.
Re: IO::Socket::INET file handler
by mattk (Pilgrim) on Aug 22, 2007 at 09:47 UTC

    There are two ways of doing this:
    - fork() after you accept(), which gets you one child per connection, or
    - threads - create multiple worker threads and pass the file descriptor in.

    Since a socket isn't really a shared resource, there are restrictions on how you can connect multiple ends to it. A socket is just a file descriptor, and a process can't get access to another processes's FDs (unless it has root privileges). When you fork, the child process already has the filehandle open, and can use it without any problems, but since you can't get any further access to the parent, where the accept()s are going on, you have to die once you're finished with the current client.

    Threads however, all live in the same PID, they just each have their own copy of the perl runtime. They can get at the parent's FDs by passing the file descriptor number around, which you get by running fileno($socket).

    I posted a thread a couple of months ago here, which ended up with some really helpful example code from BrowserUk: Resource pools and concurrency
      Thanks mattk
      In fact I am using fork() on Windows (my bad). However, my problem reduces to something like this:
      My server:
      use IO::Socket; use IO::Select; my $socket= new IO::Socket::INET ( LocalHost => 'localhost', LocalPort => 9000, Proto => 'tcp', Listen => SOMAXCONN, Reuse => 1); if (not defined $socket) { die "no server"; } my $sel= IO::Select->new($socket); print "Waiting for connections on port 9000\n"; my @ready; my $client; my $message; my $new; while (@ready= $sel->can_read) { foreach $client (@ready) { #client speaking if ($client == $socket) { #new client -> add client print "new client\n"; my $add= $client->accept; $sel->add($add); print "added\n"; } else { #old client print "old client \n"; $client->recv($message,256); print $client."\n"; $client->send($client); $client->recv($new, 256); print $new."\n"; $new->send("ok"); } } }

      My client:
      my $sock; my $message; my $socket= new IO::Socket::INET ( PeerHost => 'localhost', PeerPort => 9000, Proto => 'tcp'); if (not defined $socket) { die "no server"; } $socket->send("something"); $socket->recv($sock, 256); $socket->send($sock); $socket->recv($message, 256);
      And this gives me something like:
      Waiting for connections on port 9000 new client added old client IO::Socket::INET=GLOB(0x1b19628) IO::Socket::INET=GLOB(0x1b19628) Can't locate object method "send" via package "IO::Socket::INET=GLOB(0x1b19628)" (perhaps you forgot to load "IO:Socket::INET=GLOB(0x1b19628)"?) at server.pl line 39

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2024-03-28 19:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found