Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3452314
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:14:33+00:00 2026-05-18T09:14:33+00:00

I am trying to code a small test-server for completion ports. But when I

  • 0

I am trying to code a small test-server for completion ports.
But when I try to call AcceptEx… it always returns WSAEINVAL as the winsock error code…
I don´t really get what was my mistake

http://codepad.org/NEXG3Ssh <- code on codepad

and

StartWinsock();
 cout << "Winsock initiated\n";
 //Get the number of processors
 DWORD ulProcessors = GetNumberOfProcessors();
 cout << "Number of Processors/Threads, that will be used: " << ulProcessors << endl;
 //Create an completion port
 hCompletionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, ulProcessors);
 if(hCompletionPort == NULL)
  ErrorAbort("Could not create completion port");
 cout << "Completion Port created\n";

 //Create threads
 CreateThreads(ulProcessors);
 cout << "Threads created\n";

 //Create socket
 AcceptorSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
 addrinfo *final, hints;
 memset(&hints, 0, sizeof(hints));
 hints.ai_family = AF_INET;
 hints.ai_flags = AI_PASSIVE;
 if(getaddrinfo(NULL,"12345", &hints, &final))
  ErrorAbort("Could not retrieve address information");
 if(bind(AcceptorSock,final->ai_addr, final->ai_addrlen))
  ErrorAbort("Could not bind socket");
 freeaddrinfo(final);
 cout << "Acceptor socket created and bound\nStarting to listen on the acceptor socket\n";
 if(listen(AcceptorSock, 2))
  ErrorAbort("Can´t listen on the socket");

 //Add acceptor socket file handle to be observed by the completion port
 if(CreateIoCompletionPort((HANDLE)AcceptorSock, hCompletionPort, NEW_CONNECTION, 0) != hCompletionPort)
  ErrorAbort("A new completion port has been created instead of using the existing one");
 cout << "Acceptor socket associated with the completion port\n";

 ResumeThreads(2);
 char lpOutputBuf[1024];
 int outBufLen = 1024;
 DWORD dwBytes;
 OVERLAPPED over;
 SOCKET newSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);


 while(true)
 {
  memset(&over, 0, sizeof(over));
  if(AcceptEx(AcceptorSock, newSock, lpOutputBuf, outBufLen - ((sizeof(sockaddr_in) + 16) * 2), sizeof(sockaddr_in)+16, sizeof(sockaddr_in)+16, &dwBytes, &over) == FALSE)
  {
   int x = WSAGetLastError();
   if( x != WSA_IO_PENDING)
    ErrorAbort("Could not acceptex a new connection");
  }
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:14:33+00:00Added an answer on May 18, 2026 at 9:14 am

    Te problem in your code is this:

    while(true)
    {
      memset(&over, 0, sizeof(over));
      if(AcceptEx(AcceptorSock, newSock, lpOutputBuf, outBufLen - ((sizeof(sockaddr_in) + 16) * 2), sizeof(sockaddr_in)+16, sizeof(sockaddr_in)+16, &dwBytes, &over) == FALSE)
      {
        int x = WSAGetLastError();
        if( x != WSA_IO_PENDING)
        ErrorAbort("Could not acceptex a new connection");
      }
    }
    

    The second parameter of the AcceptEx Function (in this case newSock) must be a unconnected and unbound socket, then when a new connection arrives the newSock parameter will be an invalid parameter (because now is connected), to avoid this a New Socket Handle must be created, but the loop must wait until the new connection arrives, to do this a WSAEVENT must be use. The first the function WSAEventSelect must be used to associate the FD_ACCEPT Network event with the WSAEVENT, this mus be done before the creation of the AcceptThread:

    g_ev = WSACreateEvent();
    if (WSA_INVALID_EVENT == g_ev)
      ErrorAbort("Error occurred while WSACreateEvent()";
    if (SOCKET_ERROR == WSAEventSelect(AcceptorSock, g_ev, FD_ACCEPT))
    {
      WSACloseEvent(g_ev);
      ErrorAbort("Error occurred while WSAEventSelect().");
    }
    

    The AcceptThread Then Call The AcceptEx Function and Wait for a New Connection, when a new connection arrives the newSock is Added to the Completion port and a new Socket is created, this thread have only the elements required to accept connections:

    DWORD WINAPI AcceptThread(LPVOID lParam)
    {
      SOCKET AcceptorSock = (SOCKET)lParam, new;
      SOCKET newSock; 
      OVERLAPPED over;    
      DWORD wr;
    
      memset(&over, 0, sizeof(over));
      do
      {
        newSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if(AcceptEx(AcceptorSock, newSock, lpOutputBuf, outBufLen - ((sizeof(sockaddr_in) + 16) * 2), sizeof(sockaddr_in)+16, sizeof(sockaddr_in)+16, &dwBytes, &over) == FALSE)
        {
          int x = WSAGetLastError();
          if (x != ERROR_IO_PENDING)
          {
            cout << "Could not acceptex a new connection" << endl;
            return 1;
          }
          else
          {
            if (WSA_WAIT_TIMEOUT != (wr = WSAWaitForMultipleEvents(1,  &g_ev, FALSE, INFINITE, FALSE)))
            {
              WSAEnumNetworkEvents(AcceptorSock, g_ev, &WSAEvents);
              if ((WSAEvents.lNetworkEvents & FD_ACCEPT) &&  (0 == WSAEvents.iErrorCode[FD_ACCEPT_BIT]))
              {
                if (CreateIoCompletionPort(newSock, hCompletionPort, 0, 0) == NULL)
                {
                  cout << "Error CreateIoCompletionPort" << endl;
                  return 2;
                }
                WSAResetEvent(g_ev);
              }
            }
          }
        }
      }while(wr != WSA_WAIT_EVENT_0);
    }
    

    If any body needs more information this links could be useful:
    http://www.winsocketdotnetworkprogramming.com/winsock2programming/winsock2advancedscalableapp6b.html
    http://www.codeproject.com/KB/IP/SimpleIOCPApp.aspx
    http://msdn.microsoft.com/en-us/magazine/cc302334.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to code TDD style in PHP and one of my biggest
I am trying to code a flowchart generator for a language using Ruby. I
I've been trying to code a Perl script to substitute some text on all
I'm trying to code what I think is a fairly routine AJAX pattern using
I was trying to code along with the PDC2008 Intro to F# video .
I'm trying to code the following HQL query using the Criteria API: var userList
I am currently trying to code my own JS drag and drop script (out
I was running some dynamic programming code (trying to brute-force disprove the Collatz conjecture
To be specific, I was trying this code: package hello; public class Hello {
I am trying the following code: <?php $link = mysql_connect('localhost', 'root', 'geheim'); if (!$link)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.