当前位置:网站首页>带超时的recv函数
带超时的recv函数
2022-06-22 18:33:00 【涛歌依旧】
周末了,用纯C/C++写了一个服务端,简单:
#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;
}实现了带超时的recv函数。
边栏推荐
- 元宇宙怎么就这么火,市场喊起来的10万亿是吹嘘还是真相?
- [in depth understanding of tcapulusdb technology] tcapulusdb model
- 84. (cesium chapter) movement of cesium model on terrain
- Assign values to objects
- 产品几何技术规范(GPS) 线性尺寸公差ISO代号体系
- 冒泡排序、选择排序、直接插入排序
- 误用append案例一则
- Openpnp使用过程的一些问题记录
- Download files through Base64 (convert Base64 to BLOB)
- C #, introductory tutorial -- a little knowledge about function parameter ref and source program
猜你喜欢
![[nfs failed to mount problem] mount nfs: access denied by server while mounting localhost:/data/dev/mysql](/img/15/cbb95ec823cdde5fb8f032dc45cfc7.png)
[nfs failed to mount problem] mount nfs: access denied by server while mounting localhost:/data/dev/mysql

Altium Designer中off grid pin解决方法

编译报错:/usr/bin/ld: /usr/local/lib/libgflags.a(gflags.cc.o): relocation R_X86_64_32S against `.rodata‘

华为云招募工业智能领域合作伙伴,强力扶持+商业变现

Nrf51822 peripheral learning

Modify the antd tree component so that its subclasses are arranged horizontally.

0.1-----用AD画PCB的流程

Install some office tools

Solution of off grid pin in Altium Designer

C #, introductory tutorial -- a little knowledge about function parameter ref and source program
随机推荐
Some problem records of openpnp using process
MySQL多表操作
MySQL数据库DML操作练习
Metu stability and operation and maintenance guarantee scheme
[in depth understanding of tcapulusdb technology] new models of tcapulusdb
Compilation error: /usr/bin/ld: /usr/local/lib/libgflags a(gflags.cc.o): relocation R_ X86_ 64_ 32S against `. rodata‘
How to judge whether text is an array in the slot
从11小时到25秒--还有优化空间吗?
树和森林的遍历
Damp 3D printer consumables
技术管理进阶——你了解成长的全貌吗?
【深入理解TcaplusDB技术】TcaplusDB事务管理——错误排查
2. what is mechanical design?
第一篇 热身--隐式类型转换还是其他?
Modify the antd tree component so that its subclasses are arranged horizontally.
Matlab calling API
C语言实现平衡二叉树
美图稳定性和运维保障方案
误用append案例一则
产品几何技术规范(GPS) 线性尺寸公差ISO代号体系