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();
In reply to Re: Port duplicator
by zentara
in thread Port duplicator
by tweetiepooh
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |