Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个 ruby 套接字客户端连接到将随时关闭连接的服务器。这是我的代码:
@conn = TCPSocket.new address[0], address[1] while xml = @conn.recv(4096) // do something end
如何在这里检测到套接字关闭?
您可能已经为自己回答了这个问题,但为了记录......
recv 方法阻塞,直到至少有一个字节可用,或者套接字关闭。因此,您可以循环直到 recv 返回空字符串:
@conn = TCPSocket.new address[0], address[1] while true xml = @conn.recv(4096) break if xml.empty? // do something end