Maybe specify a valid third argument:
ULONG_PTR ck = 999;
. . . GetQueuedCompletionStatus(iocp, &my_DWORD, &ck, &my_WSAOVERLAPPED_1, INFINITE);
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Explain, please, what am I doing wrong?
I am trying to make a simple client based on IOCP:
But, i always get the gatelasterror error 998 when calling GetQueuedCompletionStatus. I just can't understand - what I'm doing wrong. :(
Maybe specify a valid third argument:
ULONG_PTR ck = 999;
. . . GetQueuedCompletionStatus(iocp, &my_DWORD, &ck, &my_WSAOVERLAPPED_1, INFINITE);
int main()
{
//-------------------------------------------------------------------Create port IO-------------------------------------------------------------
int Number_Threads = 4;
HANDLE My_handle_IOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, Number_Threads);
//------------------------------------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------Create thread for IOCP-------------------------------------------------------
std::vector<HANDLE>my_vector_Thread;
for (int i = 0; i < Number_Threads; i++)
{
my_vector_Thread.push_back(CreateThread(0, 0, (LPTHREAD_START_ROUTINE)&My_func_for_Thread, My_handle_IOCP, 0, 0));
}
//---------------------------------------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------Initialization Winsock-----------------------------------------------------------
WORD my_version_Winsock = MAKEWORD(2, 2);
WSADATA my_wsadata_struct;
int my_WSAStartup = WSAStartup(my_version_Winsock, &my_wsadata_struct);
//------------------------------------------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------Create WSASocket-----------------------------------------------------------------
SOCKET my_WSASocketA = WSASocketA(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
//--------------------------------------------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------
std::string myString_IP = "XX.XXX.XX.XXX";
//--------------------------------------------------------------------------
sockaddr my_sockaddr;
my_sockaddr = { 0 };
my_sockaddr.sa_family = 2; // AF_INET.
inet_pton(AF_INET, myString_IP.c_str(), &my_sockaddr.sa_data[2]);
my_sockaddr.sa_data[1] = 80; //http port
//--------------------------------------------------------------------------
int status_WSAConnect = WSAConnect(my_WSASocketA, &my_sockaddr, sizeof(my_sockaddr), NULL, NULL, NULL, NULL);
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------Bind socket and port--------------------------------------------------------
My_handle_Create_IOCP = CreateIoCompletionPort((HANDLE)my_WSASocketA, My_handle_IOCP, NULL, 0);
//-------------------------------------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------Send message to serever with WSASend-----------------------------------
WSABUF my_WSABUF;
std::string request_text_string = "GET / HTTP/1.1\r\nHost: government.ru\r\nConnection: keep-alive\r\n\r\n";
my_WSABUF.buf = &request_text_string[0];
my_WSABUF.len = request_text_string.size();
WSAOVERLAPPED my_WSAOVERLAPPED;
my_WSAOVERLAPPED = { 0 };
int my_WSASend = WSASend(my_WSASocketA, &my_WSABUF, 1, NULL, 0, &my_WSAOVERLAPPED, NULL);
if (my_WSASend == SOCKET_ERROR)
{
std::cout << "WSASend == SOCKET_ERROR:" std::endl;
}
if (my_WSASend == 0)
{
std::cout << "WSASend == 0: no error" std::endl;
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
}
void My_func_for_Thread(HANDLE iocp)
{
DWORD my_DWORD=666;
PULONG_PTR my_CompletionKey = 0;
WSAOVERLAPPED* my_WSAOVERLAPPED_1;
int my_GetLastError;
while (1)
{
BOOL my_BOOL_GetQueuedCompletionStatus = GetQueuedCompletionStatus(iocp, &my_DWORD, my_CompletionKey, &my_WSAOVERLAPPED_1, INFINITE);
my_GetLastError = GetLastError();
if (my_BOOL_GetQueuedCompletionStatus == FALSE)
{
std::cout << "GetQueuedCompletionStatus== FALSE" << std::endl;
}
}
}
It sounds like you're running into a tricky issue with your IOCP-based client. The error code 998 indicates an "Invalid Access to Memory Location," which could be caused by several things. Here are a few steps to help diagnose and fix the problem:
GetQueuedCompletionStatus
are correct, especially the OVERLAPPED structure and any pointers.GetQueuedCompletionStatus
to ensure all requirements and best practices are followed.If you can share a code snippet, it might help pinpoint the issue more accurately. Don't get discouraged; IOCP can be complex, but you're on the right track by seeking help.