简体   繁体   English

我应该在每个成功的消息句柄后关闭套接字吗?

[英]Should I close my socket after every successful message handle?

I am writing a program that has a Java Server/Client socket. 我正在编写一个具有Java Server / Client套接字的程序。 There will be many messages sent back and forth, and in some situations, sending a message to the server and waiting for a period of time until the server has sent back a "execute" message. 将来会发送许多消息,并且在某些情况下,向服务器发送消息并等待一段时间直到服务器发回“执行”消息。

Here is what I have planned: 这是我的计划:

  • 1 Server (machine could possibly have antivirus security on it) 1台服务器(机器上可能有防病毒安全)
  • 3 Clients (with room for more clients in future) 3个客户(未来有更多客户的空间)
  • Parallel and Interleaved synchronization being carried out on the server side based up the clients output to the server. 基于客户端输出到服务器,在服务器端执行并行和交错同步。
  • When all machines are ready (in sync), when parallel all clients will be sent an "execute" message, when interleave clients will be sent an "execute" command in sequential order 1 by 1 当所有机器都准备好(同步)时,并行时所有客户端都将发送一个“执行”消息,当交错客户端按顺序发送一个“执行”命令1

I have started to build the program to have this setup above, and once a message is received on the server, the servers performs actions based upon the input and then sends back a message to the client. 我已经开始构建程序以在上面进行此设置,并且一旦在服务器上收到消息,服务器就根据输入执行操作,然后将消息发送回客户端。 I have had problems in the past where messages were not sent or received properly, so my question is: 我过去遇到过没有正确发送或收到邮件的问题,所以我的问题是:

  • Do I keep the socket alive until then end of my program? 在我的程序结束之前,我是否保持套接字存活?
  • Or do I keep the socket open only until a successful transmission (a full handshake) has taken place and then close the socket? 或者,只有在成功传输(完全握手)并关闭套接字之后才打开套接字? Leaving the client to connect again next time it wants to send a message. 下次希望发送消息时让客户端再次连接。

You should certainly keep TCP connections open for as long as possible, but be prepared to create a new one on failure. 您当然应该尽可能长时间地保持TCP连接处于打开状态,但是要准备好在失败时创建新的连接。 You will need to use read timeouts at both ends to detect those. 您需要在两端使用读取超时来检测这些。

Q: Should I open a new socket each connection, or keep it around and re-use it for subsequent connections? 问:我应该为每个连接打开一个新套接字,还是将其保留并重新用于后续连接?

A: "It depends". 答:“这取决于”。

I would encourage you to "Keep it Simple" and simply open new socket as needed ... until you find that you need otherwise. 我鼓励你“保持简单”,只需根据需要打开新的插座......直到你发现自己需要它。

By all means: keep the socket open for as long as you reasonably expect a "dialog" between your client and server. 无论如何:只要您合理地期望客户端和服务器之间存在“对话”,就保持套接字打开。 Don't force the client to establish a new connection if he's likely to want to talk again reasonably quickly. 如果他可能想要快速合理地再次谈话,请不要强迫客户建立新连接。

Finally, take a look at these links regarding "Connection Pooling": 最后,看看有关“连接池”的这些链接:

Whether or not you close the socket after a message depends on the protocol that you use between the server and the clients. 在消息之后是否关闭套接字取决于您在服务器和客户端之间使用的协议。 Probably you define this yourself. 可能你自己定义了这个。 What is probably more important, is that you are able to serve multiple clients in parallel. 更重要的是,您可以并行服务多个客户端。 Therefore, you need to start a separate thread for every client that requests a connection. 因此,您需要为请求连接的每个客户端启动一个单独的线程。 Personally, I made some applications with socket communication. 就个人而言,我用套接字通信做了一些应用程序。 To prevent keeping resources for too long when they are not used, but also not closing and reopening constantly when a connection is heavily used, I added a connection supervisor. 为了防止资源在不使用时保留太长时间,但是在大量使用连接时也不会不断关闭和重新打开,我添加了一个连接主管。 This is yet another thread, that does is started when a connection is opened, and just performs a countdown from a predefined value (eg countdown from 60, decreqsing the value every second for a supervision time of 1 minute). 这是另一个线程,它在连接打开时启动,并且只从预定义值执行倒计时(例如,从60开始倒计时,在1分钟的监督时间内每秒递减一次值)。 When the counter reaches zero, order to close the socket, and terminate that particular thread. 当计数器达到零时,命令关闭套接字,并终止该特定线程。 When a socket is open, and receives a new message, then reset the supervision counter, so the socket will remain open, as long as the time between messages is less than 1 minute. 当套接字打开并收到新消息时,请重置监督计数器,这样只要消息之间的时间少于1分钟,套接字就会保持打开状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM