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!

In reply to Re^2: Problem extracting socket from fileno by Anonymous Monk
in thread Problem extracting socket from fileno by Anonymous Monk

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.