代码如下:
         long rc;
char buf[1024]; int count = 0;
bool bNewPacket = false;
int curLen = 0, packetLen = 0;
while(1)
{
fd_set fds;
fds.fd_count = 1;
fds.fd_array[0] = m_socket;//这个已经为有效值
timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;

int iSelectRet = select(0, &fds, NULL, NULL, &tv);
if (iSelectRet == SOCKET_ERROR)
{
printf("Select error:%d\n", WSAGetLastError());
break;
} if( __WSAFDIsSet(m_socket, &fds) == 0 )
continue;

if(bNewPacket)
{
rc = recv(m_socket, buf+curLen, packetLen-curLen, 0);
curLen += rc; if(curLen == packetLen)
{
//Do something here...
bNewPacket = FALSE;
curLen = 0;
packetLen = 0;
count++;
}
else
continue;
}
else
{
rc = recv(m_socket, buf+curLen, 2, 0);
if (rc == SOCKET_ERROR)
{
printf("接收数据错误:%d\n", WSAGetLastError());
break;
} curLen += rc;
if(curLen == 2)
{
bNewPacket = true;
packetLen = *(short*)buf;
}
}

}