Mình viết một chương trình c về socket trên Linux(Debian). Nhưng sau khi chạy thì select lúc nào cũng return 1 mà không phải là time out 5s.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
int
main(void)
{
fd_set rfds;
struct timeval tv;
int retval;
int sock;
sock = socket(AF_INET , SOCK_STREAM , 0 );
printf("SockNo %d\n", sock);
FD_ZERO(&rfds);
FD_SET(sock, &rfds);
/* Wait up to five seconds. */
tv.tv_sec = 5;
tv.tv_usec = 0;
retval = select(FD_SETSIZE, &rfds, NULL, NULL, &tv);
/* Don't rely on the value of tv now! */
if (retval == -1)
perror("select()");
else if (retval)
printf("Data is available now.\n");
else
printf("No data within five seconds.\n");
close(sock);
exit(EXIT_SUCCESS);
}
Có gì sai ở đây không ạ? Vì theo mình hiểu thì có sự kiện đến thì select mới return 1 thôi, còn ở đây mình chưa thao tác gì với socket cả?
Ai có thể giúp mình được không ạ?
- - - Nội dung đã được cập nhật ngày 29-08-2016 lúc 10:06 AM - - -
Helpppppppppppppppppppppp