Skip to content

Instantly share code, notes, and snippets.

@dyoder
Created August 28, 2008 01:19
Show Gist options
  • Save dyoder/7629 to your computer and use it in GitHub Desktop.
Save dyoder/7629 to your computer and use it in GitHub Desktop.
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