Note on the use of select in Linux

Source: Internet
Author: User

There are many problems with using select programming today. I have never studied it carefully before.
The following problems have occurred:
1 When receiving recvfrom, it is provided to the last parameter called by the system. The client address structure and its length must be initialized. If it is 0, it cannot receive data.
2 The first parameter of select is the maximum value of all monitored file descriptors plus 1, rather than the number of monitored file descriptors plus 1.
3. Do not monitor frequently available file descriptors. If you do so, select will always return.
4. After the SELECT statement is returned, if the monitored file descriptor does not change its state, the file descriptor is cleared to 0 in fd_set. Therefore, in the SELECT statement, every time we enter, We need to reset the file descriptor we are interested in.
5 if the SELECT statement uses a time-out operation, the timer will be modified every time a SELECT statement is returned, and the timer is set to the remaining time. Therefore, if a timer is used, the timer will be reset every time a cycle is entered. Pselect does not have this problem.

 

The following is the sample code of a select-based UDP reflection server in Linux:

 

 # Include <sys/select. h> <br/> # include <sys/types. h> <br/> # include <sys/time. h> <br/> # include <unistd. h> <br/> # include <stdio. h> <br/> # include <error. h> <br/> # include <stdio. h> <br/> # include <sys/socket. h> <br/> # include <ARPA/inet. h> <br/> # include <stdlib. h> <br/> # include <assert. h> <br/> int <br/> main () <br/> {<br/> int sockfd = socket (af_inet, sock_dgram, 0 ); <br/> assert (sockfd> 0); </P> <p> struct sockaddr_in local; <br/> Local. sin_family = af_inet; <br/> Local. sin_port = htons (8000); <br/> Local. sin_addr.s_addr = inaddr_any; <br/> int err = BIND (sockfd, (struct sockaddr *) & Local, sizeof (local )); <br/> assert (ERR = 0); <br/> fd_set sets [3]; <br/> for (INT I = 0; I <3; I ++) <br/>{< br/> fd_zero (& sets [I]); <br/>}</P> <p> timeval TV; <br/> char Buf [1024]; <br/> while (1) <br/>{</P> <p> fd_set (sockfd, & sets [0]); </P> <p> // register <br/> TV. TV _sec = 1; <br/> // monitor and handle <br/> int NFDs = select (sockfd + 1, & sets [0], & sets [1], & sets [2], & TV); <br/> If (NFDs <0) <br/>{< br/> perror ("select "); <br/> exit (-1); <br/>}< br/> printf ("evnet num = % d/N", NFDs ); <br/> // handle <br/> for (INT I = 0; I <NFDs; I ++) <br/>{< br/> struct sockaddr_in remote; <br/> int Len; <br/> socklen_t sock_len = sizeof (remote); <br/> If (fd_isset (sockfd, & sets [0]) // sockfd can read <br/>{< br/> Len = recvfrom (sockfd, Buf, 0, (struct sockaddr *) & remote, & sock_len ); <br/> Buf [Len] = '/0'; </P> <p> write (1, Buf, Len ); <br/> printf ("/N"); <br/> sendto (sockfd, Buf, Len, 0, (struct sockaddr *) & remote, sock_len ); </P> <p >}</P> <p >}// for </P> <p >}// while </P> <p>}

 

 

Below is a python UDP reflection Client

Import socket <br/> S = socket. socket (socket. af_inet, socket. sock_dgram, 0) <br/> STR = raw_input ("imput your string:") <br/> Server = ('2017. 168.133.239 ', 8000) <br/> while STR: <br/> S. sendto (STR, server) <br/> data, Server = S. recvfrom (1024) <br/> Print 'recvform', server, 'Data: ', data <br/> STR = raw_input ("imput your string :") </P> <p> S. close ()

 

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.