IO Completion Ports - error 998

Andreyka_ 136 Reputation points
2021-05-15T14:45:00.803+00:00

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. :(

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,653 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,760 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 118K Reputation points
    2021-05-15T17:29:36.11+00:00

    Maybe specify a valid third argument:

    ULONG_PTR ck = 999;
    . . . GetQueuedCompletionStatus(iocp, &my_DWORD, &ck, &my_WSAOVERLAPPED_1, INFINITE);

    1 person found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. Andreyka_ 136 Reputation points
    2021-05-15T14:49:00.297+00:00
    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;
                     }
    
    //-----------------------------------------------------------------------------------------------------------------------------------------------------------
    
    }
    
    0 comments No comments

  2. Andreyka_ 136 Reputation points
    2021-05-15T14:54:24.057+00:00
    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;
                }
    
          }
    }
    
    0 comments No comments

  3. stephon110 150 Reputation points
    2024-08-02T19:10:01.0633333+00:00

    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:

    1. Verify Parameters: Ensure all parameters passed to GetQueuedCompletionStatus are correct, especially the OVERLAPPED structure and any pointers.
    2. Initialize OVERLAPPED Structure: Make sure the OVERLAPPED structure is properly initialized before use. Uninitialized structures can cause memory access errors.
    3. Check Buffer Validity: Ensure the buffers associated with your I/O operations are valid and remain in scope until the I/O operation completes.
    4. Synchronize Access: If multiple threads are accessing shared resources, ensure proper synchronization to avoid race conditions and memory access violations.
    5. Error Handling: Add detailed error handling and logging around your I/O operations to capture and analyze any errors leading up to the 998 error.
    6. Review Documentation: Double-check the MSDN documentation for 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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.