nonblock recvfrom

2022-09-15 15:42:41 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

if (-1 == fcntl(iSocketfd, F_SETFL, O_NONBLOCK)) { printf(“fcntl socket error!n”); return -1; } int iSocketLen = sizeof(struct sockaddr_in);

/* set recvfrom from server timeout */ struct timeval tv; fd_set readfds; for(;;) { tv.tv_sec = TIME_OUT_TIME; tv.tv_usec = 0; FD_ZERO(&readfds); FD_SET(iSocketfd, &readfds); if (select(iSocketfd 1,&readfds,NULL, NULL, &tv) > 0) { iRecvNum = recvfrom(iSocketfd,(void *)stRecvBuf,sizeof(*(stRecvBuf)),0, (struct sockaddr *)stRemote,&iSocketLen); if (-1 == iRecvNum) { printf(“received data error!n”); } printf(“iRecvNum is %dn”,iRecvNum); } else { printf(“timeout!there is no data arrived!n”); //return -1 } }

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/163456.html原文链接:https://javaforall.cn

0 人点赞