简体   繁体   中英

Behavior of Select and FD_SET When Fd is Bigger than 1024

As far as I know, select only supports no more than 1024 sockets. But a process can own 65535 sockets which means most of the socket numbers are bigger than 1024, so I have three questions:

Q1. What will happen if passing socket numbers bigger than 1024 to FD_SET()?
Q2. What will happen if passing fd_set whose socket numbers are all bigger than 1024 to select()?
Q3. On Linux Fedora with kernel 2.6.8, x86 64bit, will exceptions be thrown in Q1 and Q2?

An fd_set is an array of bits, only manipulated with FD_* macros because C doesn't have a "bit" type. (The type is officially opaque, and could be implemented a different way - in fact winsock does implement it differently - but all unix-like OSes use the array of bits.)

So this code:

fd_set my_fds;
....
FD_SET(1024, &my_fds);

has the same problem as this code:

char my_fds[1024];
....
my_fds[1024] = 1;

assuming FD_SETSIZE is 1024.

You will be overwriting whatever comes after the fd_set in memory, causing a segfault if you're lucky, more subtle errors if you're not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM