Skip to content

Instantly share code, notes, and snippets.

@dyoder
Created August 27, 2008 23:53
Show Gist options
  • Save dyoder/7613 to your computer and use it in GitHub Desktop.
Save dyoder/7613 to your computer and use it in GitHub Desktop.
require 'socket'
require 'rubygems'
require 'monitor'
include Socket::Constants
lock = Mutex.new ; buf = []
client = Connection.new( 2201, '10.45.10.219' ).listen
server = Connection.new( 2202, '10.45.10.219' ).connect
stop = false
class YPCSocket
def initialize( port, ip )
@socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
@address = Socket.pack_sockaddr_in( port, ip )
end
def close ; @socket.close ; end
def listen
@socket.bind( @address ).listen( 1 )
end
def connect
@socket.connect( @address )
end
def accept
Connection.new( @socket.accept.first )
end
class Connection
def initialize( socket )
@socket = socket
end
def read ; @socket.gets.chomp ; end
end
end
def read
connection = client.accept
while ( data = connection.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
Thread.start { read }
Thread.start { write }
client.close ; server.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment