Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select function in socket programming

Can anyone tell me the use and application of select function in socket programming in c?

like image 809
user484457 Avatar asked Nov 13 '10 06:11

user484457


People also ask

How does select () work?

select() works by blocking until something happens on a file descriptor (aka a socket). What's 'something'? Data coming in or being able to write to a file descriptor -- you tell select() what you want to be woken up by.

What is the use of select function in networking?

The Select function is used to select between TCP and UDP sockets. This function gives instructions to the kernel to wait for any of the multiple events to occur and awakens the process only after one or more events occur or a specified time passes.

What does select () do in C?

The select() function indicates which of the specified file descriptors is ready for reading, ready for writing, or has an error condition pending.

What does select do in Linux?

select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of command written in select block and then ask again to enter a number, if a wrong option is entered it does nothing.


3 Answers

The select function allows you to check on several different sockets or pipes (or any file descriptors at all if you are not on Windows), and do something based on whichever one is ready first. More specifically, the arguments for the select function are split up into three groups:

  • Reading: When any of the file descriptors in this category are ready for reading, select will return them to you.

  • Writing: When any of the file descriptors in this category are ready for writing, select will return them to you.

  • Exceptional: When any of the file descriptors in this category have an exceptional case -- that is, they close uncleanly, a connection breaks or they have some other error -- select will return them to you.

The power of select is that individual file/socket/pipe functions are often blocking. Select allows you to monitor the activity of several different file descriptors without having to have a dedicated thread of your program to each function call.

In order for you to get a more specific answer, you will probably have to mention what language you are programming in. I have tried to give as general an answer as possible on the conceptual level.

like image 23
Talia Avatar answered Oct 07 '22 07:10

Talia


The select() function allows you to implement an event driven design pattern, when you have to deal with multiple event sources.

Let's say you want to write a program that responds to events coming from several event sources e.g. network (via sockets), user input (via stdin), other programs (via pipes), or any other event source that can be represented by an fd. You could start separate threads to handle each event source, but you would have to manage the threads and deal with concurrency issues. The other option would be to use a mechanism where you can aggregate all the fd into a single entity fdset, and then just call a function to wait on the fdset. This function would return whenever an event occurs on any of the fd. You could check which fd the event occurred on, read that fd, process the event, and respond to it. After you have done that, you would go back and sit in that wait function - till another event on some fd arrives.

select facility is such a mechanism, and the select() function is the wait function. You can find the details on how to use it in any number of books and online resources.

like image 109
Ziffusion Avatar answered Oct 07 '22 07:10

Ziffusion


select() is the low-tech way of polling sockets for new data to read or for an open TCP window to write. Unless there's some compelling reason not to, you're probably better off using poll(), or epoll_wait() if your platform has it, for better performance.

like image 4
Chris Avatar answered Oct 07 '22 07:10

Chris