#!/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); } } }