上代码 server
require "socket"
server = TCPServer.new('127.0.0.1', 20000)
print "Server started\n"
loop do
Thread.start(server.accept) do |s|
# puts s.to_hash
print(s, " is accepted\n")
str="hello world"
begin
puts "sending ... " +str
s.send(str)
#s.write(str)
sleep 2
rescue Exception => e
print s
print(s, " is gone\n")
s.close
end
end
end
如果是 send 用任意客户端连接就会报错 :远程主机强迫关闭了一个现有的连接 但是用 write 就可以,这两个方法有什么区别吗?