Here's a guess.

With an argument of "pc1" it runs as pc1, otherwise as pc2.
$otherport needs to be changed to your systems, I have only one machine so I had to runs both programs on the same machine.

This should be done with an async library like Corion suggests.

P.S. There is a lot of cheating going on in this, which wouldn't be there with a proper async lib.

#!/usr/bin/perl # http://perlmonks.org/?node_id=1181364 use strict; use warnings; use IO::Socket; use IO::Select; my $listenport; my $othersocket; my $otherport; my $sendinterval; my $pc1 = @ARGV && $ARGV[0] eq 'pc1'; if( $pc1 ) { $listenport = 8010; $sendinterval = 5; $otherport = 'localhost:8000'; } else { $listenport = 8000; $sendinterval = 7; $otherport = 'localhost:8010'; } my $ls = IO::Socket::INET->new( Listen => 10, LocalPort => $listenport, Reuse => 1, ) or die "$@ opening listen socket on port $listenport"; warn "listening on $listenport\n"; my $sel = IO::Select->new($ls); my $nextsend = $sendinterval + time; while(1) { my $delta = $nextsend - time; if( $delta <= 0 ) { if( not defined $othersocket) { if( $othersocket = IO::Socket::INET->new($otherport) ) { warn "opened client at $otherport\n"; $sel->add( $othersocket ); } else { warn "open failed $@\n"; } } if( $othersocket ) { my $msg = $pc1 ? "ADD\n" : "STAT\n"; print $othersocket $msg; print "sent $msg"; } $nextsend = $sendinterval + time; } else { for my $h ($sel->can_read($delta)) { if($h == $ls) { $sel->add(my $new = $h->accept); } elsif(sysread $h, my $in, 1024) { if( $othersocket and $h == $othersocket ) { print "should be ack: $in"; } else { print "got $in"; print $h "ack to $in"; } } else { $othersocket and $h == $othersocket and $othersocket = undef; $sel->remove($h); close $h; print "closed.\n"; } } } } __END__

In reply to Re: Async socket connection by tybalt89
in thread Async socket connection by ljamison

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.