gpt4 book ai didi

c++ - 在 WSADuplicateSocket 上发送多条消息时会发生什么?

转载 作者:行者123 更新时间:2023-11-28 06:39:41 25 4
gpt4 key购买 nike

我正在寻找有关在已复制的 WSA 套接字上同时发送时会发生什么情况的一些信息?那安全吗?你能指出任何具体的文档吗?

我是否需要在进程之间有某种消息来告知哪个进程为发送事件?我想我必须为接收做这件事。

任何人都可以举例说明这部分文档的含义吗?

Notification on shared sockets is subject to the usual constraints of WSAAsyncSelect and WSAEventSelect. Issuing either of these calls using any of the shared descriptors cancels any previous event registration for the socket, regardless of which descriptor was used to make that registration. Thus, a shared socket cannot deliver FD_READ events to process A and FD_WRITE events to process B. For situations when such tight coordination is required, developers would be advised to use threads instead of separate processes.

最佳答案

作为documentation状态:

The descriptors that reference a shared socket can be used independently for I/O. However, the Windows Sockets interface does not implement any type of access control, so it is up to the processes involved to coordinate their operations on a shared socket. Shared sockets are typically used to having one process that is responsible for creating sockets and establishing connections, and other processes that are responsible for information exchange.

如果您有两个进程同时在共享套接字上发送数据,它们将相互重叠。就像单个进程中的两个线程同时发送到同一个套接字一样。所以你需要协调发送以避免重叠。例如,您可以为此使用一个共享的命名互斥体。

至于你问的引述,如果你阅读相关文档,应该是不言自明的:

WSAAsyncSelect() function

Issuing a WSAAsyncSelect for a socket cancels any previous WSAAsyncSelect or WSAEventSelect for the same socket. For example, to receive notification for both reading and writing, the application must call WSAAsyncSelect with both FD_READ and FD_WRITE, as follows:

rc = WSAAsyncSelect(s, hWnd, wMsg, FD_READ|FD_WRITE);

It is not possible to specify different messages for different events. The following code will not work; the second call will cancel the effects of the first, and only FD_WRITE events will be reported with message wMsg2:

rc = WSAAsyncSelect(s, hWnd, wMsg1, FD_READ);
rc = WSAAsyncSelect(s, hWnd, wMsg2, FD_WRITE);

WSAEventSelect() function

Issuing a WSAEventSelect for a socket cancels any previous WSAAsyncSelect or WSAEventSelect for the same socket and clears the internal network event record. For example, to associate an event object with both reading and writing network events, the application must call WSAEventSelect with both FD_READ and FD_WRITE, as follows:

rc = WSAEventSelect(s, hEventObject, FD_READ|FD_WRITE);

It is not possible to specify different event objects for different network events. The following code will not work; the second call will cancel the effects of the first, and only the FD_WRITE network event will be associated with hEventObject2:

rc = WSAEventSelect(s, hEventObject1, FD_READ);
rc = WSAEventSelect(s, hEventObject2, FD_WRITE); //bad

因此,如果进程 S 与进程 AB 共享一个套接字,则不能有 A 监听 FD_READ 事件,B 监听 FD_WRITE 事件,反之亦然。这是一个孤注一掷的交易。

关于c++ - 在 WSADuplicateSocket 上发送多条消息时会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26186600/

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