为什么IOCP的结构创建完成之后,都要在激活一下呢?
例如:
1. 产生工作线程;
2. IOCP和文件句柄绑定;
3. 激活;    CreateWorkerThreads(ghCompletionPort);    //
    // Loop forever accepting requests new connections
    // and starting reading from them.
    //
    for (;;)
    {
        struct ContextKey *pKey;        clientAddressLength = sizeof(clientAddress);
        newsocket = accept(listener,
                            (struct sockaddr *)&clientAddress,
                            &clientAddressLength);
        if (newsocket < 0)
        {
            FatalError("accept() Failed");
            return EXIT_FAILURE;
        }        // Create a context key and initialize it.
        // calloc will zero the buffer
        pKey = calloc(1, sizeof(struct ContextKey));
        pKey->sock = newsocket;
        pKey->ovOut.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
        // Set the event for writing so that packets
        // will not be sent to the completion port when
        // a write finishes.
        pKey->ovOut.hEvent = (HANDLE)((DWORD)pKey->ovOut.hEvent | 0x1);        // Associate the socket with the completion port
        CreateIoCompletionPort(
                (HANDLE)newsocket,
                ghCompletionPort,
                (DWORD)pKey,   // No key
                0              // Use default # of threads
            );×××××××××××××××××××××××××××××××××××××
        // Kick off the first read
        bResult = ReadFile(
                    (HANDLE)pKey->sock,
                    pKey->InBuffer,
                    1,
                    &numRead,
                    &pKey->ovIn
                );
×××××××××××××××××××××××××××××××××××××
    }

解决方案 »

  1.   

    你没有明白iocp的最基础思想。
    iocp解决问题的方式。
    你去药店买药,始终都有一个营业员在哪里守候着等待客户的到来(iocp里这叫投递io操作)当你进入药店跟营业员交流你想要的药物最后你买到想要的药后离开。完成整个业务过程。但是如果一个药店没有营业员在哪里等候顾客而是换成顾客来了后需要自己呼叫营业员这个效率就很低了。所以你可以把iocp看做是一个药店而你需要的做的就是事先预备出来一些“ 营业员”来等待顾客的进入(就是投递io操作)随后你需要建立若干个线程来检测究竟哪些营业员完成了工作。可能某些比喻不恰当。但基本思路大致如此。
      

  2.   

    完成端口也可以这样理解哦将你所有需要关注的socket全部和完成端口句柄绑定,然后所有的发送和接收包都有完成端口去管理,完成后会接收到通知。
    这里提到的需要关注的socket,当然也包括你后来accept的套接字哦,都需要使用 CreateIoCompletionPort去绑定