I agree - it seems that IO::Select is a good candidate for this task. I'll just explore a few of the other issues involved.

Presumably you want the data from both of these connections to be in the same memory space so you can combine the data in some way. This requirement would preclude the use of fork since data read by one process is not accessible by another process. On the other hand, if you don't need to combine the data from these two connections, then a fork solution is possible and could be simpler than an IO::Select solution. If you tell us more about what your program is going to do with the data is receives from the two connections, we can advise you on whether or not a fork solution is possible.

Here's a fragment of code which illustrates the use of IO::Select. It assumes that the data read from the input handles $h1 and $h2 is line oriented data, and when a complete line is received the appropriate handler is called:

use IO::Select; my $s = new IO::Select; $s->add($h1); $s->add($h2); my (%handler, %buf); $handler{$h1} = sub { ...handle a line from $h1... }; $handler{$h2} = sub { ...handle a line from $h2... }; while ($s->count) { my @ready = $s->can_read(); for my $h (@ready) { my $nr = sysread($h, $buf{$h}, 1024, length($buf{$h})); if ($nr == 0) { # eof detected $s->remove($h); } else { if ($buf{$h} =~ s/\A(.*?)\n//) { $handler{$h}->($1); } } } }

The above fragment of code is generically useful in the following sense: we can accomplish most of what you want to do using the netcat command with the above handling code:

open(my $h1, "nc server.com 12345|"); open(my $h2, "nc server.com 6789|"); # now call the above I/O handler loop

In fact, we can also get graceful reconnects with a simple shell script:

open(my $h1, "while true; do nc server.com 12345; sleep 10; done|"); # ditto for $h2
This approach may have to be tweaked if you need to send data to the server, but it illustrates a potential way of decomposing the problem.

In reply to Re: Two TCP Connections, one script by pc88mxer
in thread Two TCP Connections, one script by deadpickle

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.