本帖最后由 CCDDzclxy 于 2013-08-27 17:06:25 编辑

解决方案 »

  1.   

    Return Value
    If no error occurs and the receive operation has completed immediately, WSARecv returns zero. In this case, the completion routine will have already been scheduled to be called once the calling thread is in the alertable state. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError. The error code WSA_IO_PENDING indicates that the overlapped operation has been successfully initiated and that completion will be indicated at a later time. Any other error code indicates that the overlapped operation was not successfully initiated and no completion indication will occur.
      

  2.   

    没看明白,我上面 死循环的地方,WSARecv 的返回值是0 啊...
      

  3.   

    你第一次正确接收后就陷在Recv_Real的while循环了,根本没有跳出来!
    你的 WSARecv(g_skt, &wsabuf, 1, &dwRecv, &dwFlags, NULL, NULL); 无非有两个结果,要么是SOCKET_ERROR,很显然不是;要么是其他,但是其他情况你根本没有break跳出来,也就是你的Recv_Real结束不了,怎么再继续投递呢?
      

  4.   

    事实说话,不罗嗦
    Recv_Real 中添加一些 打印信息,如下:void Recv_Real(char* _pRecv) // 真正的接收操作
    {
    printf("Recv_Real  in\n");
    while(1)
    {
    WSABUF wsabuf = {0}; 

    wsabuf.buf = _pRecv; 
    wsabuf.len = RECV_LEN; 
    memset(_pRecv, 0, RECV_LEN); 

    DWORD dwRecv = 0; 
    DWORD dwFlags = 0; 

    int iRet = WSARecv(g_skt, &wsabuf, 1, &dwRecv, &dwFlags, NULL, NULL); 
    if (SOCKET_ERROR == iRet)
    {
    int iErr = WSAGetLastError();
    // 非阻塞 套接字 出现 WSAEWOULDBLOCK 错误,
    // 说明已经没有数据了,则退出循环
    if (iErr == WSAEWOULDBLOCK) 
    {
    printf("WSARecvFrom(0) err : WSAEWOULDBLOCK\n"); 
    break;
    }
    else
    {
    printf("WSARecvFrom(1) err : %d\n", iErr); 
    break; 
    }
    }
    else
    {
    // 打印出接收到的数据 及 其长度
    printf("dwFlags : %d\n", dwFlags);
    printf("=====> OK (%d) : \n%s\n", dwRecv, _pRecv); 
    printf("\n");
    if (dwRecv == 0)
    { Sleep(1000); }
    }
    }
    printf("Recv_Real  out\n");
    }
    现象:明显 第一次 Recv_Real 之后 ,有 “WSARecvFrom(0) err : WSAEWOULDBLOCK” 和“Recv_Real  out” 出现的嘛 ...!!!
      

  5.   

    msdn WSARecv 例子中说:
    // If 0 bytes are received, the connection was closed
    http://msdn.microsoft.com/en-us/library/windows/desktop/ms741688(v=vs.85).aspx
      

  6.   

    recv到0,就表示对端断开了,只是小写的这个recv是靠返回值,大写的这个WSARecv是靠buffer返回的长度