wizbancp has asked for the wisdom of the Perl Monks concerning the following question:

Hi!
It's possible to redirect the data came from a port to another port?
let me explain: i want to do a web radio with WinAmp & shoutcast server, for personal and friend use ; i want also use a code similar to that of perl mp3server tutorial for insert a initial jinglemusic when the user connect.
My idea is to use the "mp3serverlike" code to play the jingle and then redirect to the principal shoutcast server using the first as a tunnel
ex:
shoutcast server open on port 8100
mp3server server open on port 8000

the user connect to the port 8000, after the jingle the mp3 server send the data from port 8100 to port 8000.

it's possible to do a similar thing?

excuse always for my english!:-)
<----------------->
Feel the Dark Power of Regular Expressions...

Replies are listed 'Best First'.
Re: From socket to socket
by kabeldag (Hermit) on Feb 15, 2007 at 10:20 UTC
    The short answer is yes.

    Update:

    If you want to fix up this code which I just knocked up, feel free. It's almost there. Just not all the way.
    Maybe somebody else can find the problem. I have gotto fix some other stuff though.
    Anyway, you should get the idea and hopefully you or somebody else can post a *completely working piece of code
    so you can just go right ahead and use it*, or fix up the broken part of the following code.

    Code Below blot:

    If you output the data to file, then open it from your media player it works fine, it's just a small fault connection/handshake issue on the socket that doesn't allow the media player to take the binary data after it connects.

    Go for your lives anybody. Like I said, I am busy with other projects that are more pressing.

    Cheers.
    use IO::Socket::INET; my $server; my $client; my $stream_sock_data = ""; pipe($reader, $writer); if($parent_pid = fork()) { # parent get_stream(); }else{ # child start_server(); } sub start_server { $server = IO::Socket::INET->new( LocalPort => 9999, Proto => 'tcp', Listen => '8' ) or die $!; while(my $client = $server->accept) { $resp = <$client>; if($resp =~ /GET/i) { print $client "HTTP\/1.0 OK 200\n\n"; $can_write = 1; while($can_write == 1) { $stream_sock_data = <$reader>; $can_write = print $client $stream_sock_data; } close($client); print "Client disconnected ...\n"; } } } sub get_stream { my $stream_sock = IO::Socket::INET->new( PeerPort => 8000, PeerAddr => "x.x.x.x", Proto => 'tcp' ) or die $!; print $stream_sock "GET /blahblahblah HTTP\/1.0\n\n"; $resp = <$stream_sock>; if($resp =~ /OK/i) { print "Connected to stream server ...Getting data ...\n"; while($stream_sock_data = <$stream_sock>) { print $writer $stream_sock_data; } } }
      Great!
      thanks! i'm going to study your code =:^)
      I Hope to solve the problem

      <----------------->
      Feel the Dark Power of Regular Expressions...