我正在尝试使用Ruby套接字编写聊天程序。当我运行客户端程序时,并检查服务器端;我收到此错误:Transport endpoint is not connected@io_fillbuf - fd:7 (Errno::ENOTCONN)
。为什么我不断收到此错误,并且有更好的方法来做到这一点?
服务器:
require 'socket'
host = 'localhost'
port = 10000
s = TCPServer.new(host, port)
s.accept()
while msg = s.gets
puts "Enter a message: "
input = gets.chomp
if msg
puts "Received: #{msg}"
else
sleep
end
end
客户:
require 'socket'
host = 'localhost'
port = 10000
s = TCPSocket.new(host, port)
puts "Established a connection!"
msg = s.gets()
if msg
puts msg
else
sleep
end