To be sure, similar questions have been asked before. I found tye's answer to be most helpful -- though it's from a totally different thread.

I have a socket that I wish to use from two threads. It's not a Listen socket. My &listener dies after a while though. This death is reliable, though you can't really say when before hand. If I do the same test without threads it works fine, so I figure I need to dup the IO::Socket. The problem with tye's "<&".fileno($sock) is that it returns an IO::Handle, and I wish to call the connected() from IO::Socket later.

Please help. Here is a small sample of what I'm trying to do:

my $sock = new IO::Socket::INET( PeerAddr => "igs.joyjoy.net", PeerPort => "6969", Proto => "tcp" ); my $sender = new threads( \&sender => $sock ); my $listener = new threads( \&listener => $sock ); $sender->join; $listener->join; exit; sub sender { my $sock = shift; print $sock $_ while <>; } sub listener { my $sock = shift; while( $sock->connected ) { my $msg = ""; my $sock_addr = recv( $sock, $msg, 1024, 0 ); unless( defined $sock_addr ) { die "socket error"; } SHOW: { local $| = 1; print $msg; } } }

I also realize there are modules like Net::Telnet that do a lot of this stuff for you, but I wish to learn how to do this by hand.

UPDATE (several hours later): It doesn't die at all, the recv() quits stuffing data into $msg though. It doesn't return undef either, as it would if there were an error, and it stays connected() because it starts looping really fast through the while(). I guess I have no idea why the code above doesn't work.

UPDATE II (1/5/6): I changed the code above around a little. This code actually does work as an IGS client. However, very occasionally the recv() will begin to get 0 results back and the while() loop starts firing a billion times a second (never to recover). This happens more frequently with a version where I pass fileno() and $newiosocket->fdopen($fileno, "+>") in each thread instead.


In reply to IO::Socket::INET, threads, and screwy recv() results by jettero

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.