这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » STM32 » 网络编程设置时间超时

共1条 1/1 1 跳转至

网络编程设置时间超时

高工
2018-08-22 08:23:59     打赏
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);




共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]