#include "stdafx.h"
#include <Winsock2.h>
#pragma comment(lib,"Ws2_32.lib")struct handleData
{
    SOCKET clientsocket;
    SOCKADDR_IN clientaddr;
    int nOperatorType;
};struct iOData 
{
    OVERLAPPED theoverlapped;
    WSABUF buff;
    char recvbuff[MAXBYTE];
};void InitWinSock()
{
    WORD wVersionRequested;
    WSADATA wsaData;
    int err;
    wVersionRequested = MAKEWORD( 2, 2 );
    
    err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 ) 
    {
        return;
    }
    if ( LOBYTE( wsaData.wVersion ) != 2 ||
        HIBYTE( wsaData.wVersion ) != 2 ) {
        WSACleanup( );
        return; 
    }
}int  GetLocalMoShineInfo(SYSTEM_INFO& sysinfo)
{
    GetSystemInfo(&sysinfo);
    int nCPU = sysinfo.dwNumberOfProcessors;
    return nCPU;
}
DWORD WINAPI threadfun(LPVOID lparam)
{
    bool bRet;
    DWORD dwNumRead;
    handleData handledata;
    iOData         overlappdata;
    HANDLE hCompletion = (HANDLE)lparam;
    LPOVERLAPPED lpOver = NULL;
    while (TRUE)
    {
        bRet = GetQueuedCompletionStatus(hCompletion,&dwNumRead,(LPDWORD)&handledata,(LPOVERLAPPED*)&overlappdata,INFINITE);
        if (bRet==0&&lpOver!=NULL)
        {
            //closesocket(handledata.clientsocket);
            printf("&Egrave;&iexcl;°ü&Ecirc;§°&Uuml;!");
        }
        else if (bRet!=0)
        {
            printf("&Egrave;&iexcl;&micro;&frac12;&Ograve;&raquo;&cedil;&ouml;°ü!");
        }
    }
    return 0;
}int main(int argc, char* argv[])
{
    InitWinSock();
    SOCKET Listens = socket(AF_INET,SOCK_STREAM,0);    if (Listens==INVALID_SOCKET)
    {
        printf("&acute;&acute;&frac12;¨&Igrave;×&frac12;&Oacute;×&Ouml;&Ecirc;§°&Uuml;!");
        return 0;
    }    sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(7000);
    addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");    if (bind(Listens,(sockaddr*)&addr,sizeof(addr)))
    {
        printf("°ó&para;¨&Ecirc;§°&Uuml;!");
        return 0;
    }
    HANDLE hIoCptport = CreateIoCompletionPort(INVALID_HANDLE_VALUE,NULL,0,0);
    if (hIoCptport==NULL)
    {
        printf("&acute;&acute;&frac12;¨IO&Iacute;ê&sup3;&Eacute;&para;&Euml;&iquest;&Uacute;&Ecirc;§°&Uuml;!");
        return 0;
    }
    SYSTEM_INFO sysinfo;
    int nCPU = GetLocalMoShineInfo(sysinfo);
    int nThread = nCPU*2+2;
    for (int i = 0;i<nThread;i++)
    {
        HANDLE handle = CreateThread(NULL,0,threadfun,(LPVOID)hIoCptport,0,0);
        if ( handle )
        {
            CloseHandle(handle);
        }
    }
    listen(Listens,SOMAXCONN);    printf("服务器开始监听``¤\r\n");    while (TRUE)
    {
        struct handleData* pKey = NULL;
        sockaddr_in clientaddr;
        int clientaddlength = sizeof(clientaddr);
        SOCKET clientsock = accept(Listens,(sockaddr*)&clientaddr,&clientaddlength);        printf("有客户端进入!\r\n");
        if (clientsock==INVALID_SOCKET)
        {
            printf("&frac12;&Oacute;&Ecirc;&Otilde;&Ecirc;§°&Uuml;!");
            return 0;
        }
        pKey = new handleData();        if ( pKey )
        {
            pKey->clientsocket = clientsock;            pKey->clientaddr = clientaddr;            //pKey->nOperatorType = OP_READ;
        }        CreateIoCompletionPort((HANDLE)(pKey->clientsocket),hIoCptport,(DWORD)pKey,0);
        iOData* poverlapped = new iOData();        if (poverlapped)
        {
            poverlapped->buff.buf  = poverlapped->recvbuff;
            poverlapped->buff.len = MAXBYTE;
        }
        DWORD dwRecv = 0;
        DWORD dwFlag = 0;        WSARecv(pKey->clientsocket,&(poverlapped->buff),1,&dwRecv,&dwFlag,&(poverlapped->theoverlapped),NULL);        int nError = GetLastError();
    }
    return 0;

程序能执行到有客户端进入,客户端我用连接上以后,用send发了信息,这边没反应,在GetQueuedCompletionStatus卡着了··为什么啊?

解决方案 »

  1.   

    threadfun 里也没有对数据的处理程序啊
      

  2.   

    客户端我用连接上以后 ,还有投递一个WSARecv,否则接收不到数据。
      

  3.   

    threadfun 里处理完新连接后,要在其上投递一个WSARecv
      

  4.   


    但是我连threadfun中输出都没输出,说明没有到那个地方啊··
      

  5.   

    SOCKET Listens = socket(AF_INET,SOCK_STREAM,0);
    改成
    SOCKET Listens = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, WSA_FLAG_OVERLAPPED);
    应该用重叠I/O
      

  6.   

    WSARecv(pKey->clientsocket,&(poverlapped->buff),1,&dwRecv,&dwFlag,&(poverlapped->theoverlapped),NULL);
    你可以看看这个函数的返回值是什么,是不是SOCKET_ERRO
    并且调用WSAGetLastError()返回值是不是WSA_IO_PENDING ,如果不是就是这里有什么问题,然后再根据返回值看看有什么毛病。
      

  7.   

    threadfun里面也没有包含所有可能的情况:if (bRet==0&&lpOver!=NULL)
      {
      //closesocket(handledata.clientsocket);
      printf("&Egrave;&iexcl;°ü&Ecirc;§°&Uuml;!");
      }
      else if (bRet!=0)
      {
      printf("&Egrave;&iexcl;&micro;&frac12;&Ograve;&raquo;&cedil;&ouml;°ü!");
      }你可以在if那设个断点看看GetQueuedCompletionStatus到底有没有返回
      

  8.   

    GetLocalMoShineInfo是machine吧,我的god