Skip to content

Instantly share code, notes, and snippets.

@jamis
Created Aug 26, 2008
Embed
What would you like to do?
Demonstration of how to use Net::SSH::BufferedIo independently of Net::SSH
require 'net/ssh/buffered_io'
require 'socket'
client = TCPSocket.new("localhost", 1234)
client.extend(Net::SSH::BufferedIo)
loop do
readers, writers = IO.select([client], [client], nil, 1)
next if readers.nil?
if readers.any?
if client.fill.zero?
puts(client.read_available.inspect) if client.available > 0
puts "server terminated connection"
break
elsif client.available > 60
puts(client.read_available.inspect)
end
end
if writers.any?
client.send_pending
end
client.enqueue([Time.now.to_i].pack("N"))
end
require 'net/ssh/buffered_io'
require 'socket'
server = TCPServer.new("localhost", 1234);
server.extend(Net::SSH::BufferedIo)
sockets = [server]
loop do
readers, writers, = IO.select(sockets, sockets, nil, 1)
next if readers.nil?
readers.each do |reader|
if reader == server
client = server.accept
client.extend(Net::SSH::BufferedIo)
sockets << client
puts "[#{client.object_id}] client connected"
elsif reader.fill.zero?
if reader.available > 0
puts "[#{reader.object_id}] #{reader.read_available.inspect}"
end
puts "[#{reader.object_id}] client closed"
sockets.delete(reader)
writers.delete(reader)
else
if reader.available >= 30
data = reader.read_available
puts "[#{reader.object_id}] #{data.inspect}"
reader.enqueue("[#{data.length}]")
end
end
end
writers.each do |writer|
writer.send_pending
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment