fizzbuzz/EchoTCP/IT347Client.rb

32 lines
583 B
Ruby
Raw Normal View History

2010-06-26 17:30:19 +00:00
#!/usr/bin/env ruby
#http://www.tutorialspoint.com/ruby/ruby_socket_programming.htm
require 'socket'
host, port = ARGV[0..1]
unless host && port
print("USAGE: ./TCPClient.rb host port\n")
exit
end
server = TCPSocket.open(host, port)
@connections = [server, STDIN]
stop = false
loop {
res = select(@connections, nil, nil)
res[0].each do |socket|
if socket == server
puts socket.gets
end
if socket == STDIN
line = socket.gets
server.puts line
stop = ("ADIOS" == line.chomp)
end
break if stop
end
break if stop
}
server.close