18,356
社区成员
发帖
与我相关
我的任务
分享
do {
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if ( iResult > 0 )
printf("Bytes received: %d\n", iResult);
else if ( iResult == 0 )
printf("Connection closed\n");
else
printf("recv failed: %d\n", WSAGetLastError());
} while( iResult > 0 );
typedef struct timeval {
long tv_sec;
long tv_usec;
} timeval;
一楼,弱弱的问一下:
timeval TimeOut;
TimeOut.tv_sec=0;
TimeOut.tv_usec=2000; //这个应该是指2000微妙吧,也就是2毫秒,也就是0.002秒?
FD_ZERO(&fdRead);
FD_SET(ConnectSocket,&fdRead);
int ret=::select(0,&fdRead,NULL,NULL,&TimeOut);
可以写在循环外面吗do {
fd_set fdRead;
timeval TimeOut;
TimeOut.tv_sec=0;
TimeOut.tv_usec=2000; //2S
FD_ZERO(&fdRead);
FD_SET(ConnectSocket,&fdRead);
int ret=::select(0,&fdRead,NULL,NULL,&TimeOut);
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if ( iResult > 0 )
printf("Bytes received: %d\n", iResult);
else if ( iResult == 0 )
printf("Connection closed\n");
else
printf("recv failed: %d\n", WSAGetLastError());
} while( iResult > 0 );