TCPServer

server

require'socket'

server = TCPServer.new 2000# Server bind to port 2000

loop do

  whiletrue

    client = server.accept    # Wait for a client to connect

    client.puts "Now is #{Time.now}"

    puts client.gets

    #client.puts gets

  end

  #client.close

end

client

require'socket'

while a=gets

  s = TCPSocket.new 'localhost', 2000

  s.puts 'I am ' + a

  line = s.gets # Read lines from socket

  puts line         # and print them

  s.close  

end

原文地址:https://www.cnblogs.com/qinyan20/p/3732744.html