PhillyR has asked for the wisdom of the Perl Monks concerning the following question:
First, I'm new to perl
I'm attempting to create a proxy server that will listen to data coming in on one socket (A) and then be able to provide that incoming data to multiple client sockets (Bs) but need some help in implementation.
My problem is I'm going to have 3 loops:
1: Receive data on A
2: Listen for new client connections
3: Provide data received on A to each B client socket
I've tried:
1: Forking with pipes from parent to child and even grandchildren that ended up providing data to the B clients in a round robin fashion
2: Broadcast UDP - clients cannot read from broadcast IP address
Currently I am trying to use IO::Select where the child will add each accepted client socket to an array. In my ideal world the parent would read through each item in the array and write the same data to each socket. But the parent doesn't have access items written by the child in this scenario.
Any ideas or different strategies?use IO::Socket; use IO::Select; use IO::Handle; $A = new IO::SOCKET::INET (LocalHost => 'localhost', LocalPort => 1234, Type => SOCK_STREAM, Proto => "tcp", Reuse => 1, Listen => 5) or die "A Server socket couldn't be created: $@\n"; $B_listen = new IO::SOCKET::INET (LocalHost => 'localhost', LocalPort => 5555, Type => SOCK_STREAM, Proto => "tcp", Reuse => 1, Listen => 5) or die "B Server socket couldn't be created: $@\n"; my $B_clients = new IO::Select(); my $pid = fork(); if ($pid) { #PARENT while($A_sock = $A->accept()) { #Will only have 1 connection while(defined($buf=<$A_sock>)){ my @client_set = $B_clients->can_write; foreach my $client (@client_set) { print $client $buf; } } } } else { #CHILD while($B_sock = $B_listen->accept()) { $B_clients->add($B_sock); } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Proxy Server Help
by locked_user sundialsvc4 (Abbot) on Aug 23, 2011 at 14:32 UTC | |
by PhillyR (Acolyte) on Aug 23, 2011 at 19:38 UTC |