ruby Socket 官方例子

#服务器端
require 'socket'

server = TCPServer.new(2000) # Server bound to port 2000

loop
  client = server.accept   #wait for a client to connect
  client.puts "hello !"
  client.puts "Time is #{Time.now}"
  client.close
end
#客户端
require 'socket'

s = TCPSocket.new('localhost',2000)
while line = s.gets     #read lines from socket
  puts line
end

s.close
原文地址:https://www.cnblogs.com/laoquans/p/3182516.html