UDP uses select to implement timeout retransmission

Source: Internet
Author: User

Int maxfdp;
Fd_set FDS;
Struct timeval timeout = {1, 0}; // set select to wait 3 seconds and 3 seconds for polling. If it is not blocked, set it to 0.
While (1)
{

Sendto (SKT, sendline, sizeof (vip_hdr) + 100, 0, (Sa *) saddr, sizeof (SA ));
Fd_zero (& FDs); // clear the set for each loop. Otherwise, the descriptor changes cannot be detected.
Fd_set (SKT, & FDs); // Add Descriptor
Maxfdp = SKT + 1; // Add 1 to the maximum value of the descriptor
Switch (select (maxfdp, & FDS, null, null, & timeout) // select usage
{
Case-1:
Exit (-1 );
Break;
Case 0:
Timeout. TV _sec = 1;
Timeout. TV _usec = 0;
Printf ("\ nhave not recieve net_info packet, send request again! ");
Continue;
Break;
Default:
If (fd_isset (SKT, & FDs) // test whether the SKT is readable, that is, whether there is data on the network.
{
// Printf ("\ n ########### this is ICT 1.1 ################## ########");
N = recvfrom (SKT, recvline, maxline, 0, null, & Len );
Printf ("\ n ########### this is after Recv ####% U ############## ########", n );
Break; // exit the while loop if data is read.
}

}
}

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.