TCP 網路超時 select 及 recv 實現
阿新 • • 發佈:2018-11-08
fd_set set; struct timeval st_time; st_time.tv_sec = 3 * 3; st_time.tv_usec = 0; while (1) { strcpy(sendbuf, "hello every"); FD_ZERO(&set); FD_SET(sock_cli, &set); printf("before select. \n"); int ret = select(sock_cli + 1, &set, (fd_set *)0, (fd_set *)0, &st_time); if (ret == 0) { printf("select timeout.\n"); } else if (ret < 0) { printf("select error.\n"); break; } else if (ret == 1) { if (FD_ISSET(sock_cli, &set)) { ret = recv(sock_cli, recvbuf, sizeof(recvbuf),0); ///接收 if (ret <= 0) { printf("server disconnect..\n"); break; } } }