c++ Source for select()

Hello guys, i dont have a good understanding of select() and since im working with sockets lately i need some better understanding of it, can u provide me some tutorials or maybe an explaination with the FDSETS etc?

thank's alot
What exactly do you expect here that is not provided by the literally thousands of pages you can find with a "select function tutorial" Google search?

All select does is wait for one of the argument file descriptors to become readable and/or writable -- which is exactly what you need if you are waiting on a socket.
Im sorry i meant the FD_SET & SELECT, that was my bad
There is an example of using the FD_SET macro on the man page

https://man7.org/linux/man-pages/man2/select.2.html

I don't know about any SELECT macro.
select() waits for a set of file descriptors as described above.

select() is a system call, but there are helper macros that assist with managing that set of file descriptors:
FD_SET / FD_CLR / FD_ZERO to prepare the set
FD_ISSET to test if a particular file descriptor is set
Also note:
WARNING: select() can monitor only file descriptors numbers that
are less than FD_SETSIZE (1024)—an unreasonably low limit for
many modern applications—and this limitation will not change.
All modern applications should instead use poll(2) or epoll(7),
which do not suffer this limitation.


So, better use:
https://man7.org/linux/man-pages/man2/poll.2.html
Last edited on
select() isn't ideal, but it's portable and works with any file descriptor. Also, on Windows, it only accepts sockets, as WinSock is not in the Windows kernel. epoll() is Linux only, and only works with sockets, not files.

The reason not to use select() is performance. But for normal network programming, it's OK. It is worth learning as you multiplex blocking sockets and files. Dealing with non-blocking sockets is mostly unpleasant.

EDIT
I found this extract from a manpage.

NOTES
The default size of FD_SETSIZE is currently 1024. In order to accommo-
date programs which might potentially use a larger number of open files
with select(), it is possible to increase this size by having the program
define FD_SETSIZE before the inclusion of any header which includes
<sys/types.h>.
Last edited on
I want to build the server logic for a multiplayer game on WINDOWS not LINUX, and i thought select() would be the right way to go, but since there is so many stuff to send and recive to and from clients
like location,movement coordinates, etc..
u have any suggestions, thank you alot guys im rly greatful for such a forum and its users.

THANK U
Last edited on
If you want to learn network programming, using the sockets library is a good introduction. You'll learn what the problems are, and how they're solved by various libraries.

In general, libraries make programming easier. Good C++ libraries are more common place now, it was a little tricky before.

A good C++ network library is boost asio (Asynchronous I/O). It's portable, and fast, and there's a bit of sample code kicking around, but it's not a great way to understand network programming for a beginner.
Topic archived. No new replies allowed.