Linux Network Programming: The basic implementation of select programming ideas

Source: Internet
Author: User

First, open the network communication port, choose the TCP protocol

LISTENFD = socket (af_inet,sock_stream,0);

Second, initialize the SERVADDR, and make the network byte sequence conversion

Bzero (&servaddr,sizeof (SERVADDR));

servaddr.sin_family = af_inet;

SERVADDR.SIN_ADDR.S_ADDR = htonl (Inaddr_any);

Servaddr.sin_port = htons (Serv_port);

Third, bind IP and port

Bind (LISTENFD, (struct sockaddr *) &servaddr,sizeof (SERVADDR));

Iv. setting the number of monitoring links

Listen (listenfd,20);

V. Initialize CLIENT[I] to record the file descriptor returned by accept

for (i = 0; i < fd_setsize; i++)

Client[i] =–1;

Vi. Constructing Select monitoring file descriptor set

Fd_zero (&allset);

Fd_set (Listenfd,&allset);

Seven, cyclic blocking listener file descriptor

while (1) {

1. The Select Monitor signal set is newly set in each cycle, and the number of file descriptors ready to be returned by select

RSet = Allset;

Nready = select (Maxfd+1,&rset,null,null,null);

2. Determine whether the file description character Listenfd 1, if yes, process the new link request

if (Fd_isset (Listenfd,&rset)) {

}

3. If not, test which clients has data ready

for (i = 0; I <= maxi; i++) {

}

}

Linux Network Programming: The basic implementation of select programming ideas

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.