In Perl, given two IO :: Socket, how to connect the input of the 1st socket to the 2nd output and vice versa?

Suppose I made two connections in Perl using IO::Socket

. The first has socket $s1

and the second has socket $s2

.

Any ideas how I can tie them together so that everything received from $s1

is sent to $s2

and everything received from $s2

is sent to $s1

?

I cannot figure out how to do this. I don't know how to connect them. I would expect to do something like $s1->stdin = $s2->stdout

and $s2->stdin = $s1->stdout

, but there are no such constructs in Perl.

Please help me!

Thank you Boda Sido.

+2


a source to share


2 answers


If you are dealing with binary data, you need to know which chunks of size to read and write. Let's say you are dealing with 512 byte chunks:

my $buffer;
while (read $s1 => $buffer, 512) { # read up to 512 bytes
    print $s2 $buffer;
} 

      

I'm not sure what pipe

works with sockets, but if so:

pipe $s1 => $s2;
pipe $s2 => $s1;

      



"can work. I don't have much experience with the function pipe

.

Edit:

As mentioned in the comment, you are trying to create an HTTP proxy. CPAN already has several modules that can do this for you. A quick search will appear:

+6


a source


What about



$s2->print( $_ ) while <$s1>;

      

+2


a source







All Articles