Hello there,
I am trying to create a shared object as wrapper to NET sockets. The idea would be that whatever thread access it, it would be able to read/write in the socket. The problem is that sockets cannot be saved into shared objects. Searching on internet, I found that several people relied on the solution of passing file descriptors, which I am not even sure to have understood it properly ( are not file descriptors meaningful only locally to a process? ). I`ve come up with this code:
package SharedSocket; use strict; use warnings; use threads; use threads::shared; use IO::Socket::INET; use Storable; sub new(){ my $class = shift; die("Not an instance method") if ref($class); my $self = &share(bless({},$class)); $self->{"closed"} = 0; my $socket = new IO::Socket::INET(@_) or die("SharedSocket $!"); $socket->autoflush(1); $self->{"closed"} = 0; $self->{"socket_fd"} = $socket->fileno; print("[SharedSocket::ctor] fd: " . $self->{"socket_fd"} . "\n"); return $self; } sub send{ my ($self, $msg) = @_; die("Not a static method") if ref(!$self); print("[SharedSocket::send] fd: " . $self->{"socket_fd"} . "\n"); open(my $socket, ">&=", $self->{"socket_fd"}) or die("SharedSocket + $!"); Storable::fd_nstore($msg, $socket); } sub receive{ my ($self) = @_; die("Not a static method") if ref(!$self); print("[SharedSocket::receive] fd: " . $self->{"socket_fd"} . "\n" +); open(my $socket, "<&=", $self->{"socket_fd"}) or die("SharedSocket + $!"); my $msg = Storable::fd_retrieve($socket); return $msg; } sub close{ my ($self) = @_; die("Not a static method") if ref(!$self); lock($self); return if($self->{"closed"}); open(my $socket, "+<&=", $self->{"socket_fd"}) or die("SharedSocke +t $!"); $socket->close(); $self->{"closed"} = 1; }
obtaining a bad descriptor error:
[SharedSocket::ctor] fd: 4 [SharedSocket::send] fd: 4 SharedSocket Bad file descriptor at SharedSocket.pm line xx.
Besides passing the internal handle, I guess I need to copy the socket structure somehow?

thanks in advance
kind regards,
s.fox

In reply to Keeping an INET socket inside a shared object by Superfox il Volpone

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.