in reply to Problem extracting socket from fileno
As no one has said anything so far, I'll just give it a shot... :)
One potential problem here could be that the "socket" you open to the real socket's file descriptor is not really an IO::Socket::INET object/handle, but rather a regular filehandle. As such it doesn't know about the autoflush setting of the IO::Socket handle (for example), not stored within the descriptor.
Autoflush is automatically turned on by IO::Socket (as of version 1.18). The other handle, however, will not "inherit" this setting, so in the one case (using the original socket handle) it's on, but in the other case it's off. This might account for different behaviour... Proof-of-concept demo:
#!/usr/bin/perl use strict; use warnings; use IO::Socket; my $pid = fork(); die "couldn't fork" unless defined($pid); if ($pid) { # SERVER my $server = IO::Socket::INET->new( Proto => 'tcp', LocalPort => 9999, Listen => 1, ReuseAddr => 1, ); die "couldn't setup server" unless $server; my $client = $server->accept(); $client->autoflush(1); while (<$client>) { if (/hello/) { print $client "nice to meet you\n" } elsif (/quit/) { print $client "bye\n"; last } else { print $client "??\n" } } close $client; } else { # CLIENT sleep 1; # wait for server to get ready my $sock1 = IO::Socket::INET->new( Proto => 'tcp', PeerAddr => "localhost", PeerPort => 9999, ); die "couldn't connect" unless $sock1; open my $sock2, '+<&='.$sock1->fileno() or die $!; printf "sock1 = %s\n", $sock1; printf "fileno(sock1) = %d\n", $sock1->fileno(); printf "autoflush = %s\n\n", $sock1->autoflush() ? "ON":"OFF"; printf "sock2 = %s\n", $sock2; printf "fileno(sock2) = %d\n", $sock2->fileno(); printf "autoflush = %s\n\n", $sock2->autoflush() ? "ON":"OFF"; my $resp; print $sock2 "hello\n"; $resp = <$sock2>; print $resp; print $sock2 "quit\n"; $resp = <$sock2>; print $resp; exit; }
As it is, it should print something like
$ ./751902.pl sock1 = IO::Socket::INET=GLOB(0x7dc230) fileno(sock1) = 4 autoflush = ON sock2 = GLOB(0x604410) fileno(sock2) = 4 autoflush = OFF nice to meet you bye
But if you comment out the line with the $sock2->autoflush(), it will hang before "nice to meet you", because the client's print $sock2 "hello\n"; doesn't make it to the server, due to not being flushed. (Note that ->autoflush() is not a real "getter" method — i.e. it does return the current state, but it's special in that it also turns it on! So, despite the printout "OFF", it is in fact ON after the call... unless you comment out that line — you get the idea.)
I have no real explanation how these circumstances would produce the exact behaviour you observe, but that's roughly where I would start looking (e.g. try $socket->autoflush() after the open $socket, '+<&='...).
If that doesn't help, you could use strace to figure out what's actually being exchanged over the socket.
HTH.
BTW, is there any specific reason you can't simply pass around the original $socket itself, instead of its file descriptor number?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Problem extracting socket from fileno
by Anonymous Monk on Mar 20, 2009 at 06:33 UTC | |
|
Re^2: Problem extracting socket from fileno
by Anonymous Monk on Mar 20, 2009 at 06:25 UTC | |
|
Re^2: Problem extracting socket from fileno
by Anonymous Monk on Mar 20, 2009 at 07:00 UTC | |
by almut (Canon) on Mar 20, 2009 at 14:00 UTC |