in reply to Re: Problem extracting socket from fileno
in thread Problem extracting socket from fileno

Ok I've rewriten your script as follows -
#!/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>) { print "DUMP:\n"; use Data::Dumper; print Dumper($_); print "DUMP END\n"; if (/hello/) { print $client "nice to meet you\n" } elsif (/quit/) { print $client "bye\n"; } 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 $sock1 "hello\n"; $resp = <$sock1>; print $resp; print $sock1 "quit\n"; $resp = <$sock1>; print $resp; exit; }
And when I print out the values for sock1 I get -
sock1 = IO::Socket::INET=GLOB(0xd9327c) fileno(sock1) = 4 autoflush = ON sock2 = GLOB(0xe424b4) fileno(sock2) = 4 autoflush = OFF DUMP: $VAR1 = 'hello '; DUMP END nice to meet you DUMP: $VAR1 = 'quit '; DUMP END bye
But if I print for socket2 (by changing the last five lines of the script) I get -
sock1 = IO::Socket::INET=GLOB(0xd93504) fileno(sock1) = 4 autoflush = ON sock2 = GLOB(0xe42714) fileno(sock2) = 4 autoflush = OFF DUMP: $VAR1 = 'hello '; DUMP END nice to meet you DUMP: $VAR1 = 'nice to meet you '; DUMP END ?? DUMP: $VAR1 = 'quit '; DUMP END
Where is that extra message comming from. Am I not really flushing?
Thanks again!

Replies are listed 'Best First'.
Re^3: Problem extracting socket from fileno
by almut (Canon) on Mar 20, 2009 at 14:00 UTC

    Generally, what you're trying to do here is only borderline supported (if at all!). There's a reason why sockets aren't created with open — the underlying file descriptor is only part of what the OS needs to operate a socket.  And I'd wager it's one of those subtle differences (to regular file handles) that are responsible for the problems you're observing. (This is also known as "In Unix everything is a file ...unless it isn't" ;) such as sockets.)

    In this specific case, it looks like the output the server has written ("nice to meet you") is immediately fed back to itself, so its own output gets in the way of new input...  (I can't replicate this behaviour here, btw, but that's probably just due to minor timing differences on my system, or whatever).

    In short, it's probably best to give up that route, and try to find some other way of implementing it.  What's the bigger picture of things you're trying to accomplish in the end?  Maybe people can suggest alternative approaches...