当前位置:网站首页>Recv function with timeout
Recv function with timeout
2022-06-22 20:05:00 【Tao song remains the same】
Weekend. , In pure C/C++ Write a server , Simple :
#include <winsock2.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(1, 1);
WSAStartup( wVersionRequested, &wsaData );
SOCKET sockClient = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(8888);
int ret = connect(sockClient, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
printf("connect ret is %d\n", ret);
fd_set rfds;
struct timeval timeout = {4, 0};
FD_ZERO(&rfds);
FD_SET(sockClient, &rfds);
ret = select(-1, &rfds, NULL, NULL, &timeout);
printf("select ret is %d\n",ret);
if(0 > ret)
{
printf("select error\n");
return -1;
}
if(0 == ret)
{
printf("time out\n");
return -2;
}
if(FD_ISSET(sockClient, &rfds))
{
char recvBuf[200] = "x";
ret = recv(sockClient, recvBuf, 100, 0);
if(0 > ret)
{
printf("recv error");
return -3;
}
if(0 == ret)
{
printf("recv ret is %d, buf is %s\n", ret, recvBuf);
return -4;
}
printf("%s\n", recvBuf);
}
while(1);
closesocket(sockClient);
WSACleanup();
return 0;
}With timeout recv function .
边栏推荐
- University of Calgary | recommendation system based on Reinforcement Learning
- 1.2----- mechanical design tools (CAD software) and hardware design tools (EDA software) and comparison
- B树代码(C语言)
- [deeply understand tcapulusdb technology] tcapulusdb table management - delete table
- 卡尔加里大学|基于强化学习的推荐系统综述
- Implementation of balanced binary tree with C language
- Search, insert and delete of binary sort tree
- 散列表(哈希表)
- 使用 Order by 与 rownum SQL 优化案例一则
- [deeply understand tcapulusdb technology] tcapulusdb transaction management - error troubleshooting
猜你喜欢
随机推荐
希尔排序
Implementation of balanced binary tree with C language
[in depth understanding of tcapulusdb technology] tcapulusdb operation and maintenance doc
DynamicDatabaseSource,在应用端支持数据库的主从
[deeply understand tcapulusdb technology] view the online operation of tcapulusdb
Summer Challenge [FFH] Hongmeng machine learning journey from scratch NLP emotion analysis
【深入理解TcaplusDB技术】如何启动TcaplusDB进程
【深入理解TcaplusDB技术】TcaplusDB新增机型
使用 Order by 与 rownum SQL 优化案例一则
【深入理解TcaplusDB技术】入门Tcaplus-JDBC开发
510000 prize pool invites you to join the war! The second Alibaba cloud ECS cloudbuild developer competition is coming
[deeply understand tcapulusdb technology] cluster management operation
Modify the antd tree component so that its subclasses are arranged horizontally.
[deeply understand tcapulusdb technology] tcapulusdb table management - delete table
iVX无代码挑战五秒游戏制作
堆排序(原理加代码)
B tree code (C language)
一文带你读懂内存泄露
0.1----- process of drawing PCB with AD
MySQL数据库DQL查询操作









