in reply to Sockets and Output

Here's one for you. The first snippet is the server, the second one is the client.

#!c:/perl/bin/perl -w $|++; use strict; use IO::Socket::INET; # Initialize the server my $sock = new IO::Socket::INET ( LocalHost => '127.0.0.1', LocalPort => '7070', Proto => 'tcp', Listen => 1, ReuseAddr => 1 ) or die "Server could not initialize!\n\n"; # Autoflush for verisons before 1.18 $sock->autoflush(1); print "The server has successfully initialized.\n\n"; # Allow another client to connect after the # current client disconnects from the server. while ( my $new_sock = $sock->accept() ) { print "A client has connected to the server.\n"; # Example output to the client for my $i (1 .. 5) { sleep 1; print $new_sock $i, "\n"; } close $new_sock; }

#!c:/perl/bin/perl -w $|++; use strict; use IO::Socket::INET; # Number of times to attempt a server connection my $max_attempts = $ARGV[0] || 10000; my $sock; for my $i (1 .. $max_attempts) { $sock = IO::Socket::INET->new( PeerAddr => '127.0.0.1', PeerPort => '7070', Proto => 'tcp' ); if (defined $sock) { print "Connection to server established.\n\n"; last; } else { print "Attempt $i/$max_attempts failed.\n"; die "Could not connect to the server.\n\n" if $i == $max_attempts; } sleep 2; } # Continually grab input from the server my $byte; print $byte while sysread($sock, $byte, 1); close $sock;