Created
August 28, 2008 01:19
-
-
Save dyoder/7629 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'socket' | |
include Socket::Constants | |
lock = Mutex.new ; buf = [] | |
client = TCPServer.new( 2201 ) | |
server = TCPSocket.new( '10.x.x.x', 2202 ) | |
def read | |
begin | |
socket = client.accept_nonblock | |
rescue Errno::EAGAIN, Errno::ECONNABORTED, Errno::EPROTO, Errno::EINTR | |
IO.select([serv]) | |
retry | |
end | |
while ( data = socket.read && data != 'END' ) | |
lock.synchronize { buf << data } | |
end | |
stop = true | |
end | |
def write | |
until( stop ) do | |
lock.synchronize { data = buf.pop unless buf.empty? } | |
server.puts( data ) if data | |
end | |
end | |
threads = [] | |
threads << Thread.start { read } | |
threads << Thread.start { write } | |
sleep 1 while threads.find { | thread | thread.alive? } | |
client.close ; server.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment