From your description, you collect data from the remote port, which seems to indicate that the data is sent in 1 direction only. and that would seem to be simple.

Your redirector script, would seem to be a basic "multi-echo server", simplified by the fact that all clients are listening. The big problem is writing everything inside a while(1) loop which constantly reads the datain socket, checks for new local connections, and echos all the datain to existing local connections.

Without actually writing a working script, this is how I would proceed (step by step). This is untested. I'm not sure if you can use the same port number for your local connections, as for the remote. It would seem you could, but they are different in this example.

#!/usr/bin/perl use warnings; use strict; use IO::Socket; use IO::Select; my @sockets; my $machine_addr = '192.168.0.1'; #your local machine my $remote_addr = '192.168.0.9'; #data supplier #setup datain socket as a client connection my $data_sock = new IO::Socket::INET( PeerAddr=>$remote_addr, PeerPort=>1300, Proto=>'tcp', ); die "Could not connect: $!" unless $data_sock; #setup server for local sockets for local echoing my $main_sock = new IO::Socket::INET( LocalAddr=>$machine_addr, LocalPort=>1200, Proto=>'tcp', Listen=>3, Reuse=>1, ); die "Could not connect: $!" unless $main_sock; print "Starting Server\n"; my $readable_handles = new IO::Select(); $readable_handles->add($main_sock); $readable_handles->add($data_sock); while (1) { ($new_readable) = IO::Select->select($readable_handles, undef, undef +, 0); foreach my $sock (@$new_readable) { #detect and handle new local connections if ($sock == $main_sock) { $new_sock = $sock->accept(); $readable_handles->add($new_sock); } else { #read if it's from data_sock and echo to all except data_sock if ($sock == $data_sock){ my $buf = <$sock>; if ($buf){ print "$buf\n"; my @sockets = $readable_handles->can_write(); @sockets = grep { $_ ne $data_sock } @sockets; #print $sock "You sent $buf\n"; foreach my $sck(@sockets){print $sck "$buf\n";} } else { $readable_handles->remove($sock); close($sock); } } } } } print "Terminating Server\n"; close $main_sock; close $data_sock; getc();

I'm not really a human, but I play one on earth. flash japh

In reply to Re: Port duplicator by zentara
in thread Port duplicator by tweetiepooh

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.