Home > Software engineering >  Why Io overlapping WSAWaitForMultipleEvents always returns 0?
Why Io overlapping WSAWaitForMultipleEvents always returns 0?

Time:10-21

 # include & lt; Winsock2. H> 
# include & lt; iostream>
# include & lt; Vector>
# include & lt; Mswsock. H>
using namespace std;
# define PORT 4567
# define MAXBUFFER 1024
# pragma comment (lib, "ws2_32. Lib")

Typedef struct _SOCKOBJ
{
Int nOperationType;
The SOCKET sAccept;
OVERLAPPED ol;
Char Buff [MAXBUFFER];
Char * lpBuff;
DWORD dwLen;
_SOCKOBJ ()
{
ZeroMemory (& amp; Ol, sizeof (OVERLAPPED));
ZeroMemory (Buff, sizeof (MAXBUFFER));
NOperationType=0;
SAccept=INVALID_SOCKET;
LpBuff=Buff;
DwLen=0;
Ol. HEvent=WSACreateEvent ();
}
} * PSOCKOBJ SOCKOBJ;

DWORD dwTotal=0, dwIndex=0;
Vector MSKObj;
HANDLE events [WSA_MAXIMUM_WAIT_EVENTS];

Void InitSock ()
{
WSADATA WSADATA;
LPFN_ACCEPTEX lpfnAcceptEx;
To: : WSAStartup (MAKEWORD (2, 2), & amp; WSAData);
The SOCKET sListen=: : WSASocket (AF_INET, SOCK_STREAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED);
Sockaddr_in in_service;
: : ZeroMemory (& amp; In_service, sizeof (sockaddr_in));
In_service. Sin_family=AF_INET;
In_service. Sin_port=: : ntohs (4567);
In_service. Sin_addr. S_un. S_addr=: : inet_addr (" 192.168.1.102 ");
Int ret=bind (sListen, (sockaddr *) & amp; In_service, sizeof (sockaddr_in));
If (ret==INVALID_SOCKET)
{
cout <"The bind error" & lt; <"\ n";
}
Listen (sListen, 5);

GUID GuidAcceptEx=WSAID_ACCEPTEX;
DWORD dwBytes;
(sListen WSAIoctl SIO_GET_EXTENSION_FUNCTION_POINTER,
& GuidAcceptEx, sizeof (GuidAcceptEx), & amp; LpfnAcceptEx, sizeof (lpfnAcceptEx), & amp; DwBytes, NULL, NULL);

for(int i=0; i<5; I++)
{
PSOCKOBJ obj=new SOCKOBJ ();
Obj - & gt; NOperationType=1;
Obj - & gt; SAccept=: : WSASocket (AF_INET, SOCK_STREAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED);
LpfnAcceptEx (sListen, obj - & gt; SAccept, obj - & gt; LpBuff,
MAXBUFFER - 2 * (sizeof (sockaddr_in) + 16),
Sizeof (sockaddr_in) + 16,
Sizeof (sockaddr_in) + 16,
& Obj - & gt; DwLen,
& Obj - & gt; Ol);
Obj mSKObj. Push_back (*);
Events [dwTotal++]=obj - & gt; Ol. HEvent;
}

While (true)
{
//dwIndex each time returns 0
DwIndex==: : WSAWaitForMultipleEvents (dwTotal, events, FALSE, WSA_INFINITE, FALSE);
cout <"Code:" & lt; If (dwIndex==WSA_WAIT_FAILED)
{
cout <" WSAWaitForMultipleEvents failed! \ n ";
}
DwIndex=dwIndex - WSA_WAIT_EVENT_0;
For (int I=dwIndex; i{
DwIndex=WSAWaitForMultipleEvents (1, & amp; Events [I], FALSE, 0, FALSE);
If (dwIndex==WSA_WAIT_TIMEOUT)
{
continue;
}
: : WSAResetEvent (events [I]);
DWORD dwTrans=0;
DWORD dwFlags=0;
SOCKOBJ obj=mSKObj. At (I);
//bRet always returns False and GetLastError==996
BOOL bRet=: : WSAGetOverlappedResult (obj. SAccept, & amp; Obj. Ol, & amp; DwTrans, FALSE, & amp; DwFlags);
cout <"Receiving data length:" & lt; if(! BRet)
{
DWORD err=: : GetLastError ();
cout <"WSAGetOverlappedResult failed! \ n ";
}
}
}
}

Void main ()
{
InitSock ();
}


-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

//dwIndex each time returns 0

//bRet always returns False and GetLastError==996

CodePudding user response:

996 - WSA_IO_INCOMPLETE
Overlapped I/O event object is not in A state of signalling, the Wi n 3 2 errors are closely related and overlapped I/O operations, in the call Ov W S A G e t e r l A p p e d r e S u l t S function of time, points out that the overlapped I/O operation has not yet been completed,

CodePudding user response:

I know it's overlapped I/O operation has not yet been completed,
But what is the specific I/O operation has been finished, what cause errors,

CodePudding user response:

BOOL WSAAPI WSAGetOverlappedResult (
__in SOCKET s,
__in LPWSAOVERLAPPED lpOverlapped,
__out LPDWORD lpcbTransfer,
__in BOOL fWait,
__out LPDWORD lpdwFlags
);

The Parameters
S
A descriptor identifying the socket. This is the same socket that was specified when the overlapped operation was started by A call to WSARecv, WSARecvFrom, WSASend, WSASendTo, or WSAIoctl.

LpOverlapped
A pointer to A WSAOVERLAPPED structure that was specified when the overlapped operation was started. This parameter must not be A NULL pointer.

LpcbTransfer
32 - bit A pointer to A variable that receives the number of bytes that were later transferred by A send or receive operation, or by WSAIoctl. This parameter must not be A NULL pointer.

FWait
  • Related