timeout - How to make recv call non blocking and make it to wait only 5 second
问题描述
How to make recv call non blocking and make it to wait only 5 second!
// Receive until the peer closes the connection
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 with error: %d\n", WSAGetLastError());
} while( iResult > 0 );
解决方案
fd_set readfds;
struct timeval count;
FD_ZERO(&readfds);
FD_SET(sockfd, &readfds);
select(0, &readfds, 0, 0, &count);
if (FD_ISSET(sockfd, &readfds))
{
if ((bytes=recv(sockfd, buffer2, 200, 0)) == -1) {
return 0;
}
}
推荐阅读
- oracle - Devart.Data.Oracle.EFCore - 如何使用序列来设置 PK 列值?
- ms-access - 带有 7 个集合或 1 个数组的 MS Access 更新表?
- python - 将列表解析为numpy数组时如何去掉括号
- javascript - 在 javascript 或 jQuery 的选择框数组中设置 (Nth) 元素的选定值
- core-audio - 如何混合音频文件并上传到服务器
- css - 如何剪辑图像并使其响应?
- android - react-native错误找不到变量:样式
- java - 构建多图的正确方法
- kendo-grid - 如何在 Angular Kendo Grid 中格式化日期
- php - OUTPUT 数组中的单独 1 个键