Pipeing Sockets

Hello ALL!

Is there anyway to pipe two sockets? I want to forward everything that comes
from socket1 to socket2 and vice-versa…

[]s

Pablo

···


Pablo Lorenzzoni (Spectra) spectra@debian.org
GnuPG: 0x268A084D at pgp.mit.edu/keyring.debian.org
This message is protected by DoubleROT13 encryption
Attempting to decode it violates the DMCA/WIPO acts

you mean like

while server.gets
client.puts $_
end

···

On Sun, 2003-07-20 at 16:21, Pablo Lorenzzoni wrote:

Hello ALL!

Is there anyway to pipe two sockets? I want to forward everything that comes
from socket1 to socket2 and vice-versa…

s

Pablo

Here’s how I did it in [63989]:

s and out are two sockets

loop do
ready = select [s, out]
ready = ready[0]
ready.each do |x|
case x
when s
data = s.sysread(512)
out.syswrite data
when out
data = out.sysread(512)
s.syswrite data
end
end
end

···

On Sun, Jul 20, 2003 at 01:21:51PM +0900, Pablo Lorenzzoni wrote:

Hello ALL!

Is there anyway to pipe two sockets? I want to forward everything that comes
from socket1 to socket2 and vice-versa…


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

(It is an old Debian tradition to leave at least twice a year …)
– Sven Rudolph

Hello!

I also thought that would work (the construction is beautiful, isn’t it?). The
problem is that gets wait for the Enter key to be hit, and I need to echo
every character instantaneously.

s

Pablo

···

Em Dom 20 Jul 2003 02:23, maillist@bestworldweb.homelinux.com escreveu:

you mean like

while server.gets
client.puts $_
end

On Sun, 2003-07-20 at 16:21, Pablo Lorenzzoni wrote:

Hello ALL!

Is there anyway to pipe two sockets? I want to forward everything that
comes from socket1 to socket2 and vice-versa…

s

Pablo


Pablo Lorenzzoni (Spectra) spectra@debian.org
GnuPG: 0x268A084D at pgp.mit.edu/keyring.debian.org
This message is protected by DoubleROT13 encryption
Attempting to decode it violates the DMCA/WIPO acts

I also thought that would work (the construction is beautiful, isn’t it?). The
problem is that gets wait for the Enter key to be hit, and I need to echo
every character instantaneously.

Well you can always do:

while a = server.read(1)
client.write a
end

It’s not especially efficient, but it will work.

You can’t however do server.read(1024), say, because that will block until
1024 characters have been read. I think 1.8 has a feature which lets you
determine how many characters are available in the socket buffer to read
(but I can’t remember what it is)

Is there anyway to pipe two sockets? I want to forward everything that
comes from socket1 to socket2 and vice-versa…

I guess you’ve discovered this already, but you can use threads to get the
bidirectional operation. This should work (untested):

Thread.new do
while ch = socketa.read(1)
socketb.write ch
end
end
Thread.new do
while ch = socketb.read(1)
socketa.write ch
end
end
… main thread continues at this point

Regards,

Brian.

···

On Sun, Jul 20, 2003 at 07:57:13PM +0900, Pablo Lorenzzoni wrote:

Hello!

That worked (with some proper tweks for what I am doing, of course). Thanx

s

Pablo

···

Em Dom 20 Jul 2003 08:25, Brian Candler escreveu:

I guess you’ve discovered this already, but you can use threads to get the
bidirectional operation. This should work (untested):

Thread.new do
while ch = socketa.read(1)
socketb.write ch
end
end
Thread.new do
while ch = socketb.read(1)
socketa.write ch
end
end
… main thread continues at this point


Pablo Lorenzzoni (Spectra) spectra@debian.org
GnuPG: 0x268A084D at pgp.mit.edu/keyring.debian.org
This message is protected by DoubleROT13 encryption
Attempting to decode it violates the DMCA/WIPO acts