IPC: Connect for each request or keep socket open?

元气小坏坏 提交于 2019-12-07 08:46:15

问题


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?


回答1:


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).




回答2:


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.



来源:https://stackoverflow.com/questions/6176829/ipc-connect-for-each-request-or-keep-socket-open

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!