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