[英]Keeping TCPSocket open: server or client's responsibility?
I have been reading examples online about Ruby's TCPSocket and TCPServer, but I still don't know and can't find what's the best practice for this. 我一直在网上阅读有关Ruby的TCPSocket和TCPServer的示例,但我仍然不知道,也找不到最佳的做法 。 If you have a running TCPServer, and you want to keep the socket open across multiple connections/clients, who should be responsible in keeping them open, the server or the clients?
如果您有一个正在运行的TCPServer,并且您想让套接字在多个连接/客户端之间保持打开状态,那么服务器或客户端应该由谁来负责使其保持打开状态?
Let's say that you have a TCPServer running: 假设您有一个正在运行的TCPServer:
server = TCPServer.new(8000)
loop do
client = server.accept
while line = client.gets
# process data from client
end
client.puts "Response from server"
client.close # should server close the socket?
end
And Client: 和客户:
socket = TCPSocket.new 'localhost', 8000
while line = socket.gets
# process data from server
end
socket.close # should client close the socket here?
All of the examples I have seen have the socket.close
at the end, which I would assume is not what I want as that would close the connection. 我看到的所有示例都在结尾处有
socket.close
,我认为这不是我想要的,因为那样会关闭连接。 Server and clients should maintain open connection as they will need to send data back and forth. 服务器和客户端应保持开放连接,因为它们将需要来回发送数据。
PS: I'm pretty a noob on networking, so just kindly let me know if my question sounds completely dumb. PS:我对网络很陌生,所以请告诉我我的问题听起来是否完全愚蠢。
The server is usually responsible for keeping the connections open because the client (being the one connecting to the server) can break the connection at anytime. 服务器通常负责使连接保持打开状态,因为客户端(连接到服务器的客户端)可以随时断开连接。
Servers are usually in charge of everything that the client doesn't care about. 服务器通常负责客户端不关心的所有事情。 A video game doesn't really care about the connection to the server as long as it's there.
只要存在,视频游戏就不会真正在乎与服务器的连接。 It just wants its data so it can keep running.
它只需要其数据,以便它可以继续运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.