插座是否应该保持打开或拆除
可能的重复:
我应该在每次交易后关闭套接字 (TCPIP) 吗?
假设我有某种类型的进程间通信,可以使用套接字进行工作。
我的进程是否应该建立连接并保持打开状态(每个客户端 1 个线程或类似)在需要时发送数据?或者是简单地建立连接、发送我想要的数据、关闭它并再次进入等待状态的更好方法?
通常解决这个问题的方法是什么?
Possible Duplicate:
Should I close a socket (TCPIP) after every transaction?
Lets say I have some type of interprocess communication that works using sockets.
Should my processes establish a connection and hold it open (1 thread per client or similar) sending data when it needs; or is a better approach to simply establish a connection, send the data I wish, close it and enter my wait state again?
What is usually the approach to this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我问了同样的问题,所有三个回复都表示将其保留。我同意了,它似乎对我有用。
我应该在每次交易后关闭套接字 (TCPIP) 吗?
I asked this same question and all three responses said to leave it open. I went with that, and it seems to be working for me.
Should I close a socket (TCPIP) after every transaction?
本地套接字没有太多开销,因为它们跳过 TCP/IP 堆栈并使用命名管道实现。保持套接字打开和关闭不会有太大区别。
Local sockets do not have much overhead since they skip the TCP/IP Stack and are implemented using named pipes. Keeping a socket open and close wouldn't make much of difference.
任何资源,无论是文件、套接字、数据库连接还是硬件设备,都需要时间并使用处理器和内存来打开,因为它必须获取资源、计算安全访问并进行一些簿记。
每条消息之间的打开和关闭只会浪费CPU和内存资源。
长时间保持打开状态也很危险,但您必须保持打开状态并确定最佳超时值,以便在任何一端死亡时自动关闭它。
Any resource, be it file, socket, database connection or hardware device, take time and uses processor and memory to open because it has to fetch resources, compute security access and do some bookkeeping.
Opening and closing between every message will just waste CPU and memory resources.
Keeping it open for long time is also dangerous as well but you have to leave it open and decide best timeout value to close it automatically when any one end has died.