PhillyR has asked for the wisdom of the Perl Monks concerning the following question:
When I connect a client1 to the proxy it connects and waits for stream1. When stream1 server initates the connection, stream1 starts flowing to client1. Later when client2 connects, the proxy initiates the connection to the second server and stream2 starts. Stream2 goes to client2 but stream1 stops. I need stream1 to continue to flow. What am I doing wrong?#!/usr/bin/perl -w use IO::Socket; use IO::Select; $stream1_prt = 5555; $stream2_prt = 5556; $client1_prt = 1100; $client2_prt = 1101; $my_ip = '192.168.1.12'; # Create Stream 1 listen $stream1_lsn = new IO::Socket::INET (LocalAddr => $my_ip, LocalPort => $stream1_prt, Type => SOCK_STREAM, Proto => "tcp", Reuse => 1, Blocking => 0, #part of issue? Listen => 5) or die "Stream 1 listen socket couldn't be create: $@\n"; # Create Client 1 listen $client1_lsn = new IO::Socket::INET (LocalAddr => $my_ip, LocalPort => $client1_prt, Type => SOCK_STREAM, Proto => "tcp", Reuse => 1, Blocking => 0, #part of issue? Listen => 5) or die "Client 1 listen socket couldn't be create: $@\n"; # Create Client 2 listen $client2_lsn = new IO::Socket::INET (LocalAddr => $my_ip, LocalPort => $client2_prt, Type => SOCK_STREAM, Proto => "tcp", Reuse => 1, Blocking => 0, #part of issue? Listen => 5) or die "Client 2 listen socket couldn't be create: $@\n"; # Add listen sockets to select $sockets = new IO::Select(); $sockets->add($stream1_lsn); $sockets->add($client1_lsn); $sockets->add($client2_lsn); #Go into infinite loop, handling connections - how do I end this? while(@ready = $sockets->can_read) { #got data foreach $socket (@ready) { # find which socket sent data $sck_prt = $socket->sockport; $pr_prt = $socket->peerport; #used by sockets sending data #New Stream1 if ($socket == $stream1_lsn) { $stream1_sock = $stream1_lsn->accept(); #accept stream 1 $sockets->add($stream1_sock); #add socket to select $stream1_hst = $stream1_sock->peerhost; print "New stream1 from IP [$stream1_hst]\n"; } #New client1 elsif ($socket == $client1_lsn) { $client1_sock = $client1_lsn->accept(); #accept client 1 $sockets->add($client1_sock); #add socket to select $client1_hst = $client1_sock->peerhost; print "New client for stream 1 @ IP [$client1_hst]\n"; } #New client2 elsif ($socket == $client2_lsn) { $client2_sock = $client2_lsn->accept(); #accept client 2 $sockets->add($client2_sock); #add socket to select $client2_hst = $client2_sock->peerhost; print "New client for stream 2 @ IP [$client2_hst]\n"; #Connection to second server running locally $stream2_sock = new IO::Socket::INET( PeerAddr => 'localhost', PeerPort => $stream2_prt, Type => SOCK_STREAM, Proto => "tcp") or die "Stream2 socket couldn't be created: $@\n"; $sockets->add($stream2_sock); #add socket to select } #Stream2 data to all client2's elsif(defined($stream2_sock)) { #may not be initiated yet if($socket == $stream2_sock) { # NEXT LINE IS BLOCKING --------------------------- if(defined($buf1=<$socket>)) { #put stream2 into buf1 @writers1 = $sockets->can_write; foreach writer1 (@writers1) { $wrt_sp1 = $writer1->sockport; if ($wrt_sp1 == $client2_prt) { print $writer1 $buf1; #send stream2 to each client2 } } } } #Stream1 data to all Client1's elsif(defined($pr_prt)) { #used this port due to multiple $stream1_sock connections #THIS LINE DOESN'T EXCUTE IF STREAM2 IS ACTIVE ----------- if(defined($buf=<$socket>)) { #put stream1 into buffer @writers = $sockets->can_write; foreach $writer (@writers) { $wrt_sp = $writer->sockport; if($sck_prt == $stream1_prt && $wrt_sp == $client1_prt) { print $writer $buf; #send stream1 to all client1's } } } } else { print "Closing socket\n"; #Needed if clients close connection $sockets->remove($socket); close($socket); } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problem handling 2 simultaneous socket streams
by BrowserUk (Patriarch) on Sep 15, 2011 at 14:55 UTC | |
| |
|
Re: Problem handling 2 simultaneous socket streams
by ikegami (Patriarch) on Sep 15, 2011 at 14:55 UTC | |
|
Re: Problem handling 2 simultaneous socket streams
by zentara (Cardinal) on Sep 15, 2011 at 15:26 UTC | |
by PhillyR (Acolyte) on Sep 29, 2011 at 17:59 UTC | |
by zentara (Cardinal) on Sep 29, 2011 at 18:20 UTC | |
by PhillyR (Acolyte) on Sep 29, 2011 at 19:04 UTC | |
by zentara (Cardinal) on Sep 30, 2011 at 10:55 UTC | |
| |
|
Re: Problem handling 2 simultaneous socket streams
by osbosb (Monk) on Sep 15, 2011 at 17:48 UTC | |
by ikegami (Patriarch) on Sep 15, 2011 at 17:58 UTC | |
|
Re: Problem handling 2 simultaneous socket streams
by salva (Canon) on Sep 16, 2011 at 06:40 UTC |