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;

In reply to Re: Sockets and Output by Coruscate
in thread Sockets and Output by maxl90

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.