ruby中的socket

连接多个客户端服务器还没有弄懂。

# socketServer.rb
require 'socket'                 
 server = TCPServer.open('127.0.0.1',2000)     
 client = server.accept        
 loop {  
     str=client.gets 
     puts "recived at #{Time.now.ctime}" 
     puts "responded....."  
     client.puts("info: #{str}")
     puts ".....finished...."  
 }   
 client.close  
 
 
 
#socketClient.rb
require 'socket'        
host = '127.0.0.1'  
port = 2000  
# msg="\0x48\0x0a\0x0d"  
s = TCPSocket.open(host, port)  
# s.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)  
loop  do   
    puts "请输入内容:"
    msg = gets.chomp
    send = s.puts(msg)  
    # s.flush  
    # puts   msg  
    resp = s.gets  
    puts resp  
    sleep 1  
end

你可能感兴趣的:(ruby中的socket)