Friday, January 28, 2011

Echo Socket (single thread, single connection)

#!/usr/bin/env ruby
require 'socket'
port = (ARGV[0] || 7777).to_i

TCPServer.open('localhost', port) do |svr|
  loop do #do-while loop
    sk = svr.accept

    input = sk.gets
    puts "RECV >>>#{input.chomp}<<<"
    sk.puts input
    puts "SENT >>>#{input.chomp}<<<"
    sk.close
    break if input.downcase.chomp == 'quit'
  end #end do-while loop
end

No comments:

Post a Comment