struct timeval tv; /* timeval and timeout stuff added by davekw7x */ int timeouts = 0; tv.tv_sec = 3; tv.tv_usec = 0; if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof tv)) { perror("setsockopt"); return -1; } if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof their_addr) == -1) { perror("connect"); exit(1); } while (((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) && (++timeouts < 1000)) { /* loop to retry in case it timed out; added by davekw7x */ perror("recv"); printf("After timeout #%d, trying again:\n", timeouts); } printf("numbytes = %d\n", numbytes); buf[numbytes] = '\0'; printf("Received: %s",buf);
共2条
1/1 1 跳转至页
网络编程设置时间超时
共2条
1/1 1 跳转至页
回复
有奖活动 | |
---|---|
【有奖活动——B站互动赢积分】活动开启啦! | |
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |