开发者

select() max sockets

开发者 https://www.devze.com 2023-02-17 14:17 出处:网络
Just more asynchronous stuff! Alright, so I now have a working asynchronous socket program for my main chatting application, and it\'s working really well! However I have one concern..

Just more asynchronous stuff!

Alright, so I now have a working asynchronous socket program for my main chatting application, and it's working really well! However I have one concern..

While using select() what is the maximum number of file descrip开发者_开发技巧tors that I can use in each set? I've read about a limit of 1024...

If that limit is indeed hard coded and I can't FD_SETSIZE the limit any higher, should I spawn another thread once I reach that limit? Or something else? Is this even a concern?


Yes, the FD_SETSIZE has a limit of 1024. You can easily check that by looking at the select.h header. People have tried to increase the limit, but the reports vary from "working" to "crashing" after a while. If you need that many connections, use poll instead.

A very good article to read.


If you are programming under a Posix compliant system, you should be able to use the poll() function instead of select() and this will do away with the limit that you mention. Alternatively, you can call select() multiple times in succession but be certain to use a relatively short timeout.


For really large numbers of sockets look into using a library like libevent.

The library can abstract several OS-specific advanced features like /dev/poll, kqueue, epoll, and event ports. With these you can handle really vast numbers of connections.


You don't say what OS you're using, but for most, if you want to use file descriptors above 1024 with select, you can #define FD_SETSIZE to be a larger number BEFORE #including sys/socket.h. Unfortunately, this doesn't work on Linux.

0

精彩评论

暂无评论...
验证码 换一张
取 消