简体   繁体   English

保持TCPSocket开放:服务器还是客户端的责任?

[英]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.

相关问题 Ruby TCPSocket服务器-我可以告诉客户端连接的主机吗? - Ruby TCPSocket Server - Can I tell to what host a client was connecting? 测试Ruby TCPSocket服务器 - Test Ruby TCPSocket server 使用tcpsocket的tutorialspoint的简单Web浏览器 - tutorialspoint's simple web browser using tcpsocket Ruby:使用Ruby TCPSocket在特定URI路径上建立客户端连接 - Ruby: Establish client connection on particular URI path with ruby TCPSocket 为什么红宝石不能在我的Fedora 20盒子上打开TCPSocket? - Why can't ruby open a TCPSocket on my Fedora 20 box? 当服务器被杀死时,Ruby TCPSocket没有注意到它 - Ruby TCPSocket doesn't notice it when server is killed 在给定超时后从 Ruby 的 TCPSocket::gets 逃脱的最佳方法是什么? - What's the best way to escape from Ruby's TCPSocket::gets after a given timeout? 使用TCPSocket通过Ruby连接到Rails Server。 获得“所需长度的WEBrick :: HTTPStatus :: LengthRequired” - Connect to a Rails Server via Ruby using the TCPSocket. Getting “Length Required WEBrick::HTTPStatus::LengthRequired” 使用Ruby的TCPSocket-Class发送原始字节数组的最简单方法 - simplest way to send raw Byte-arrays using Ruby's TCPSocket-Class 对于订户和状态更新,通知订户是谁的责任? - With subscribers and status updates, who's responsibility is it to notify subscribers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM