我在套接字選擇上返回-1。但是,只有在我們使用新安裝的sybase數據庫時纔會發生這種情況。對舊數據庫使用此代碼,我沒有得到任何套接字選擇錯誤,一切正常。C在Solaris上的套接字選擇錯誤
在下面的例子HOW_MANY = 2,和timeout_secs = 60
需要注意的是在工作時只要file_limits.rlim_cur下面的代碼爲256。然而,隨着新的數據庫file_limits.rlim_cur = 65,000和套接字選擇返回-1。我試圖將select中的第一個參數硬編碼爲256,但它仍然返回-1。
int socket_activity(int how_many, int *fd, int timeout_secs)
{
int i;
int select_fd;
fd_set read_fds;
fd_set except_fds;
struct timeval timeout;
struct rlimit file_limits;
/*
** Determine the current limits.
*/
if (getrlimit(RLIMIT_NOFILE, &file_limits) != 0)
return(-1);
/*
** Set up the select structures. Initialize the timeout to the specified
** seconds. Only non-negative file descriptors are initialized.
*/
FD_ZERO(&read_fds);
FD_ZERO(&except_fds);
for (i = 0; i < how_many; i++)
if (fd[i] >= 0) {
FD_SET(fd[i], &read_fds);
FD_SET(fd[i], &except_fds);
} /* of if */
timeout.tv_sec = timeout_secs;
timeout.tv_usec = 0;
/*
** Perform the select and check on the results.
*/
select_fd = select(file_limits.rlim_cur,
&read_fds,
NULL,
&except_fds,
&timeout);
if (select_fd > 0) {
/*
** Scan the list of file descriptors and return which file
** descripitor show activity. Only check non-negative file descriptors.
*/
for (i = 0; i < how_many; i++)
if ((fd[i] >= 0) &&
(FD_ISSET(fd[i], &read_fds)))
return(fd[i]);
/*
** No file descriptor showed activity so return zero to indicate
** that a timeout occured.
*/
return(0);
} /* of if */
else
/*
** Simply return the return value from select (the function will
** return a 0 on timeout or a -1 on error).
*/
return(select_fd);
} /* of function */
我假設你的'for'循環應該有一些括號嗎? – yano
如果'select'返回-1,你應該調用'perror'來找出失敗的原因。 – dbush
可能你已經超過了'FD_SETSIZE'?請參閱http://stackoverflow.com/questions/7976388/increasing-limit-of-fd-setsize-and-select –