插座是否应该保持打开或拆除

发布于 2024-11-01 02:55:04 字数 478 浏览 0 评论 0原文

可能的重复:
我应该在每次交易后关闭套接字 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

メ斷腸人バ 2024-11-08 02:55:04

我问了同样的问题,所有三个回复都表示将其保留。我同意了,它似乎对我有用。

我应该在每次交易后关闭套接字 (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?

悟红尘 2024-11-08 02:55:04

本地套接字没有太多开销,因为它们跳过 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.

凯凯我们等你回来 2024-11-08 02:55:04

任何资源,无论是文件、套接字、数据库连接还是硬件设备,都需要时间并使用处理器和内存来打开,因为它必须获取资源、计算安全访问并进行一些簿记。

每条消息之间的打开和关闭只会浪费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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文