曾經寫過如上這樣的代碼,執行時怎麼也得不到預想的結果!
後來,仔細看man select
On
success, select() and pselect() return the number of file descriptors
contained in the three returned descriptor sets(that is,
the total number of bits that are set in
readfds, writefds, exceptfds) which may be zero if the timeout expires
before anything interesting happens. On error, -1 is returned, and
errno is set appropriately; the sets and timeout become undefined, so do not rely on their contents after an error.
原來第一次select的時候出錯了,導致rd_fd和tm(主要是rd_fd)的值 become undefined,進而導致以後的select調用的失敗!
解決辦法:
在1處(while循環內的開頭),添加如下代碼:
FD_ZERO(&rd_fd);
FD_SET(sockfd, &rd_fd);
tm.tv_sec = 1;
tm.tv_usec = 0;
linxu select 返回值
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.