Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I've created a proxy script to receive 2 streams of data and forward to multiple clients (each client picks which stream they want). I'm having a problem (most likely some blocking) when both streams are active at the same time. My proxy listens for connections from the server sending stream1. My proxy initiates a connection to a second server for stream2 when a client asks for stream2. Confusing? Excellent. Onto the code:
#!/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); } } }
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?

In reply to Problem handling 2 simultaneous socket streams by PhillyR

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-25 08:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found