gpt4 book ai didi

windows - 重叠套接字 IO : WSAGetOverlappedResult Fails with 996 Errorcode

转载 作者:可可西里 更新时间:2023-11-01 10:18:30 25 4
gpt4 key购买 nike

我正在维护一个旧的 Windows 应用程序并且有一个我无法解决的问题。在新硬件上,在各种操作系统下,我们开始看到过去非常稳定的地方从未见过的错误。它打开一个 UDP 套接字并将其配置为广播 UDP 数据。然后它会传输数据。该代码使用重叠 IO,因此它有一个线程在等待重叠 IO 事件的线程中等待。套接字是这样打开的:

// Create datagram socket
if (!SocketDgCreate(&m_SDGScan, pstAppState->szScannerIP, (short)nPort)){
StatusMessage(MSG_ERR, "socket create failed on '%s' Port %u\r\n",
pstAppState->szScannerIP, nPort);
return false;
}

// Configure datagram socket for broadcast
int nSockOpt = TRUE;

if (setsockopt(m_SDGScan,
SOL_SOCKET,
SO_BROADCAST,
(LPSTR)&nSockOpt,
sizeof(nSockOpt)) == SOCKET_ERROR){
StatusMessage(MSG_ERR, "socket broadcast failed\r\n\t%s\r\n",
WSAGetLastErrorStr());
return false;
}

// Allow the socket to be bound to an address already in use.
if (setsockopt(m_SDGScan,
SOL_SOCKET,
SO_REUSEADDR,
(LPSTR)&nSockOpt,
sizeof(nSockOpt)) == SOCKET_ERROR){
StatusMessage(MSG_ERR, "socket reuse address failed\r\n\t%s\r\n",
WSAGetLastErrorStr());
return false;
}

在新硬件上,当我们打开套接字时,我们会收到 Microsoft 定义的错误 996:

Overlapped I/O event object not in signaled state. The application has tried to determine the status of an overlapped operation which is not yet completed. Applications that use WSAGetOverlappedResult (with the fWait flag set to FALSE) in a polling mode to determine when an overlapped operation has completed, get this error code until the operation is complete. Note that this error is returned by the operating system, so the error number may change in future releases of Windows.

使用 winsock 发送数据的代码似乎通过处理错误代码并验证它不是挂起来做正确的事情。

int nSendRet = WSASendTo(m_SDGScan, 
&m_astSendBuff[m_nSendCtr],
1,
&nNumSentImmed,
0,
(LPSOCKADDR)&m_scanDGDestAddr, sizeof(SOCKADDR),
&m_scanDGOverlapped,
NULL); // Use event signal upon completion.

if (nSendRet == 0){
// Sent immediately
++m_nSendCtr;
if (m_nSendCtr == m_nNumQueued) {
m_nSendCtr = m_nNumQueued = 0;
}
}else if (nSendRet == SOCKET_ERROR) {
// This could just be an overlap in progress state!
int err = WSAGetLastError();
if (err == WSA_IO_PENDING) {
// If the error indicates I/O pending then the Overlapped operation
// was successfull and will complete later!
SetEvent( m_hScanDGEvent );
}

实际触发错误的代码在它自己的线程中,看起来像这样:

UINT CXyz::Thread(LPVOID){

bool fEventSelect = true;
WSANETWORKEVENTS networkEvents;

DWORD dwOvlRslt = 0;
DWORD dwWaitVal;
DWORD dwNumSent;

// Turn on the XYZ datagram stuff.
// Load event into overlapped I/O structures
m_scanDGOverlapped.hEvent = m_hScanDGEvent;
ResetEvent(m_hScanDGEvent);

// Setup the events.
const int nNumEvents = 2;
HANDLE ahEvents[nNumEvents];
ahEvents[0] = m_hScanDGEvent; // XYZ datagrams.
ahEvents[1] = m_hThreadStopEvent;

// Thread now alive!
m_fThreadAlive = true;

// Associate scan transmit event with socket, but no state yet (will be overridden)

if (WSAEventSelect(m_SDGScan, m_hScanDGEvent, FD_READ) == SOCKET_ERROR) {
StatusMessage(MSG_ERR, "XYZ DG event select failed 1 [%s]\r\n",
WSAGetLastErrorStr());
}

// Thread loop processing...
bool bDone = false;
dwWaitVal = WaitForMultipleObjectsEx(nNumEvents, ahEvents, FALSE, INFINITE, FALSE);

// Acq buffer done or delay timer expired
switch (dwWaitVal)
{
case WAIT_FAILED: // Bad,
StatusMessage(MSG_ERR, "XYZ thread aborting [%d]\r\n", GetLastError());
bDone = true;
break;

case WAIT_OBJECT_0: // XYZ datagrams.
// Clears internal records and resets event object

// Clears internal records and resets event object
if (WSAEnumNetworkEvents(m_SDGScan, m_hScanDGEvent, &networkEvents) ==
SOCKET_ERROR){
StatusMessage(MSG_ERR, "XYZ WSAEnumNetworkEvents() failed [%s]\r\n",
WSAGetLastErrorStr());
bDone = true;
}else if (!WSAGetOverlappedResult(
m_SDGScan,
&m_scanDGOverlapped,
&dwNumSent,
FALSE,
&dwOvlRslt)){
// Get status of last send
StatusMessage(MSG_ERR, "WSAGetOverlappedResult() failed [%s]\r\n",WSAGetLastErrorStr());
}else{
// Handle next packet code omitted
}

case (WAIT_OBJECT_0+1): // Shutdown.
bDone = true;
break;

default:
break;
}

最佳答案

错误代码 996 是 WSA_IO_INCOMPLETE,如您所述,记录如下:

The application has tried to determine the status of an overlapped operation which is not yet completed. Applications that use WSAGetOverlappedResult (with the fWait flag set to FALSE) in a polling mode to determine when an overlapped operation has completed, get this error code until the operation is complete.

事实上,您将 fWait 参数设置为 FALSE,因此该错误是完全正常的行为。由于您正在轮询状态,因此您必须在循环中调用 WSAGetOverlappedResult(),直到它停止报告 WSA_IO_INCOMPLETE

否则,将fWait 参数设置为TRUE,让它阻塞调用线程,直到I/O 操作完成。

关于windows - 重叠套接字 IO : WSAGetOverlappedResult Fails with 996 Errorcode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35208259/

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