IPC:为每个请求连接还是保持套接字打开?
我计划使用套接字(本地 TCP)在两个进程(本地运行)之间进行通信。一个进程将充当服务器,但两个进程异步地相互发送消息。我应该在进程的生命周期内保持套接字连接打开,还是为每个请求重新连接?
如果我保持本地套接字打开,可能会出现任何问题吗?
I am planning to use sockets (local TCP) to communicate between two processes (running locally). One process will act as a server, but both processes send messages to each other asynchronously. Should I keep the socket connection open for the lifetime of the processes, or re-connect for each request?
Are there any problems that may occur if I keep a local socket open?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
保持套接字打开。
这是更简单的选项,您不会产生打开新套接字(从客户端角度)或接受新客户端(从服务器角度)的开销。
Keep the socket open.
Its the simpler option and you don't incur in the overhead of opening a new socket (from the client point of view) or accepting a new client (from server point of view).
如果只有一个客户端,那么您应该保持套接字打开,但准备好在发生错误时重新连接(例如,用户可能重置网络接口,因此不能保证连接处于活动状态)。如果您有很多客户端,请在非活动超时一定时间后使用断开连接。这将使您摆脱不活动、挂起或“丢失”(那些已经消失但没有关于套接字关闭的信号的客户端)客户端。
If it's just one client, then you should keep the socket open but be ready to reconnect in case of error (the user could have reset network interfaces for example, so connection is not guaranteed to be alive). If you have many clients, use disconnection after inactivity timeout of certain time. This will let you get rid of inactive, hung or "lost" (the ones that passed away but there was no signal about socket closing form them) clients.