示例下载
      Win32重叠I/O(OverLapped I/O)的机制允许发起一个操作,然后在操作完成之后接受到信息。对于那种需要很长时间才能完成的操作来说重叠操作,有着很重要的意义。我们可以在发起一个操作后,在监听线程中等待操作完成,而主线程可以做其他的事情。
      在Win2000上,提供的可扩展的I/O模型就是使用完成端口。完成端口对在Windows的内部做了优化,提供了更高的效率。
      可以把完成端口看成一个系统维护的队列,操作系统将完成的重叠操作通知消息放到完成端口队列,由于暴露“操作完成”,所以命名为“完成端口”。
         应用完成端口的步骤:
                  1.创建完成端口句柄:
                        HANDLE hIocp;
                        hIocp=CreateCompletiom(
                                                      INVALID_HANDLE_VALUE,
                                                      NULL,
                                                      (ULONG_PRT)0,
                                                       0);
                        if(   hIocp == NULL){
                                          //创建失败。
                        }
                  2.关联SOCKET 描述符与完成端口句柄
                        SOCKET s;
                        s=socket(AF_INET,SOCKE_STREAM,0);
                        if (s == INVALID_SOCKET){
                              ///SOCKET 创建失败处理      
                           }
                        if( CreateCompletionPort((HANDLE)s, hIocp,(ULONG_PRT)0,0) == NULL){
                              //关联失败
                           }
                  3.创建监听线程., 监听线程可以创建多个,根据具体需要而定。
                           CreateThread(CoPortThread,......);
                  4.监听线程函数:
                        DWORD CoPortThread(LPVOID lpParam){
                                    while(true){
                                             bResult = GetQueuedCompletionStatus(ghCompletionPort, &dwNumRead, (DWORD*)&pCntx, &lpOverlapped, INFINITE);

                                            if (bResult == FALSE && lpOverlapped == NULL)
                                               {                     
                                                                  FatalError("WorkerThread - GetQueuedCompletionStatus()错误.\n");
                                                        }
                                            else if (bResult == FALSE && lpOverlapped != NULL)
                                                     {
                                                                                 fprintf(stderr,"用户非正常退出.\n"); 
                                                        }
                                               else if (dwNumRead == 0)
                                                        {    
                                                                                          fprintf(stderr, "用户已经退出.\n");
                                                                                          fprintf(stderr, "------------------.\n");
                                                              }
                                                 else
                                                              {
                                                                        printf("recv data from client: %s\n", pCntx->InBuffer);
                                                                  
                                                                 }
                                     }
                              }