#!/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; } #### $ ./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