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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |