gpt4 book ai didi

windows - 如何硬关闭 Winsock UDP 套接字?

转载 作者:行者123 更新时间:2023-12-03 12:06:00 30 4
gpt4 key购买 nike

我需要立即关闭未发送数据的 UDP 套接字。
TCP 套接字有 SO_LINGER 参数,但我没有找到任何关于 UDP 的信息。
它在 Windows 上。

提前致谢。

更新 0:

我给出了这个问题的背景。我有应用程序第一个线程打开/绑定(bind)/关闭套接字,第二个线程向它发送数据报。
在某些情况下,关闭套接字 (errorcode = 0) 后,bind 函数会返回错误代码 10048“地址已在使用中”。我发现 close() 执行端口后仍然使用(通过 netstat 命令)。也许我问了不正确的问题,而这种行为的原因是别的?

最佳答案

一旦您的 send() 用于所有应用目的返回,数据包被“发送”。没有像 TCP 那样的发送缓冲区,而且您无法控制 NIC 数据包队列。正常 close()是你所需要的全部。

编辑0:

@EJP,这是来自 UNP 的引述(第 2.11 节“UDP 输出”):

This time, we show the socket send buffer as a dashed box bacause it doesn't really exist. A UDP socket has a send buffer size (which we can change with the SO_SNDBUF socket option, Section 7.5), but this is simply an upper limit on the maximum-sized UDP datagram that can be written to the socket. If an application writes a datagram larget than the socket send buffer size, EMSGSIZE is returned. Since UDP is unreliable, it does not need tp keep a copy of the application's data and does not need an actual send buffer. (The application data is normally copied into a kernel buffer of some form as it passes down the protocol stack, but this copy is discarded by the datalink layer after the data is transmitted.)



这就是我在回答中的意思-您无法控制发送缓冲区-,因此“出于所有应用程序目的”它不存在。

关于windows - 如何硬关闭 Winsock UDP 套接字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11634904/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com