Select function in socket programming

前端 未结 5 1825
情歌与酒
情歌与酒 2020-12-03 07:44

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

5条回答
  •  Happy的楠姐
    2020-12-03 08:48

    I like description at gnu.org:

    Sometimes a program needs to accept input on multiple input channels whenever input arrives. For example, some workstations may have devices such as a digitizing tablet, function button box, or dial box that are connected via normal asynchronous serial interfaces; good user interface style requires responding immediately to input on any device. [...]

    You cannot normally use read for this purpose, because this blocks the program until input is available on one particular file descriptor; input on other channels won’t wake it up. You could set nonblocking mode and poll each file descriptor in turn, but this is very inefficient.

    A better solution is to use the select function. This blocks the program until input or output is ready on a specified set of file descriptors, or until a timer expires, whichever comes first.

提交回复
热议问题