在网络正常的情况下,基本上都是可以接收到数据的,
但如果是用拨号连接的,如果接收的是多个包,
则后面有些包可能会收不到?
这是为什么???也没有发现因为超时而返回的问题,连接也没有断. int nBytesThisTime;
time_t stOldTime = time(NULL);
time_t stCurTime;
memset(szRecvBuf,'\0',nMaxLen);
int nLen = pstSocket->Receive(szRecvBuf,4);
if(nLen <4 )
return 0; (这里也不会执行到)
else
szRecvBuf[4] = 0; int nPduLen=*((int*)szRecvBuf);
if(nPduLen > nMaxLen)
return 0; nLen = 0;
while (nLen<nPduLen)
{
nBytesThisTime=pstSocket->Receive(szRecvBuf+4+nLen,nPduLen);
if(nBytesThisTime >0)
nLen+=nBytesThisTime;
stCurTime = time(NULL);
if(abs(difftime(stCurTime,stOldTime)) >50)
return 0; (这里也不会执行到)
}

return nPduLen +4;

解决方案 »

  1.   

    具体的解决方法可以看看这个帖子
    http://community.csdn.net/Expert/TopicView3.asp?id=5149398
      

  2.   

    int nLen = pstSocket->Receive(szRecvBuf,4);
    if(nLen <4 )
    return 0; (这里也不会执行到)==>> 这里是有可能执行到的!
    else
    szRecvBuf[4] = 0;上面那句有可能执行到的, 因为Receive返回的数据大小不一定是你期待的大小。还有szRecvBuf[4] = 0;这句什么意思,没看懂。
      

  3.   

    我在这里设置了断点,执行到这里的时候,
    nLen 一直是0,从来没有大于0过.
      

  4.   

    你说错了吧, 你应该想说  " nLen一直是4"如果你想nLen<4很简单, 对方发送1个字节, 之后sleep 10秒, 之后再发送, 之后再sleep 10秒.你看看nLen还能次次是4不?会有这种情况是因为  TCP是流式协议, 还有Nagle算法, 不家网络延迟等问题造成的.