Network Programming--select Introduction

Source: Internet
Author: User
Tags function prototype

I. Introduction to the SELECT function

Select is generally used in the socket network programming, in the process of network programming, often encounter many blocking functions, network programming using the Recv, recvfrom, connect functions are blocking functions, when the function can not be executed successfully, the program will always be blocked here, The following code cannot be executed. This means that non-blocking programming is required, and non-blocking programming can be implemented using the SELCET function.
The Selcet function is a round-robin function, that is, when the loop asks for a file node, you can set the time-out and the timeout time to skip the code and continue execution.

Select (), determines the state of one or more sets of interfaces, this function is used to determine the state of one or more sets of interfaces, for each socket, the caller can query its readability, writable and error state information, with the fd_set structure to represent a set of waiting to check the socket, when the call returns, This structure holds a subset of the set of interface groups that satisfy certain conditions, and select () returns the number of socket interfaces that satisfy the condition.

Multiplexing is usually implemented with select, which means that multiple file descriptors can be monitored simultaneously;

Here is the function prototype for select:

/* According to posix.1-2001 */2  #include <sys/select.h>3  4  int Select (int Nfds, fd_set *readfds, Fd_ Set *writefds, Fd_set *exceptfds, struct timeval *timeout);

  

Here is a specific explanation:

The first parameter: int Nfds---> is an integer value that refers to the range of all file descriptors in the collection, that is, the maximum value of all file descriptors plus 1

The second parameter: Fd_set *READFDS----> is used to check a set of readable file descriptors.

The third parameter: Fd_set *WRITEFDS----> A file descriptor to check for a set of writable characters.

Fourth parameter: Fd_set *EXCEPTFDS----> used to check if file descriptor is abnormal

Fifth parameter: sreuct timeval *timeout---> is a time structure used to set the time-out period

Timeout: The maximum wait time, or null for a blocking operation

Return value of the Select function
Negative value: Select Error
Positive value: Indicates that some files are readable or writable
0: Wait timeout, no writable or incorrect files

Here are some of the functions and structures used with select

void fd_clr (int fd, fd_set *set);//Clears a set of file descriptors int  fd_isset (int fd, fd_set *set);//Adds a file descriptor to a specified file descriptor collection void FD _set (int fd, fd_set *set);//delete a given file descriptor from the collection, void Fd_zero (Fd_set *set);//Check whether the specified file descriptor in the collection can read and write

struct TIMEVAL structure is used to set the timeout time, the structure can be accurate to seconds and milliseconds

struct Timeval {        time_t         tv_sec;     /* seconds */        suseconds_t    tv_usec;    /* microseconds */};

The following is an example of a select that is commonly used:

#include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <sys/types.h> #include <unistd.h>int Main (int argc, Cahr **argv) {    struct timeval timeout;    int ret = 0;    Fd_set RfDs;     Fd_zero (&rfds);//Empty descriptor set     fd_set (0, &rfds);//Add standard input (stdin) to the collection    fd_set (SOCKFD, &rfds);// Add our socket descriptor to the collection/    * Set timeout time */    timeout.tv_sec = 1;       timeout.tv_usec = 0;    /* Listen whether the socket is readable *    /ret = select (sockfd+1, &rfds, NULL, NULL, &timeout);    if (ret = =-1) {//select connection failed        perror ("select failure\n");        return 1;    }       else if (ret = = 0) {//timeout (timeout)        return 1;    }       if (Fd_isset (PESOCKFD, &rfds)) {//If it is readable                                //recv or recvfrom .......    }       return 0;   }

  

Network Programming--select Introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.