Three methods for network timeout Detection

Source: Internet
Author: User



Yu,Hua Qing vision embedded college lecturer.


spark network timeout

In network communication, many operations will cause process blocking. In this case, we need to set the time and forcibly return the result after the time to avoid infinite blocking of the process without data.


network timeout

Here we will summarize three methods for network timeout Detection:revelation online network connection timeout


sirius network timeout

Set the socket property so_rcvtimeo through setsockopt


Struct timeval T = {5, 0}
If (setsockopt (listenfd, sol_socket, so_rcvtimeo, & T, sizeof (t) =-1 ){
Perror ("setsockopt ");
Return-1;
}
Memset (& peeraddr, 0, sizeof (peeraddr ));
Len = sizeof (peeraddr );
If (connfd = accept (listenfd, (struct sockaddr *) & peeraddr, & Len) =-1 ){
Printf ("errno = % d: % s \ n", errno, strerror (errno ));
If (errno = eagain ){
Printf ("timeout \ n ");
Return-1;
}
}


2. Set a parameter of the select function for timeout Processing


Struct timeval T = {3, 0 };
While (1 ){
......
T. TV _sec = 3;
T. TV _usec = 0;
If (ret = select (maxfd + 1, & RDFS, null, null, & T) =-1 ){
Perror ("select ");
Return-1;
}
......
}


3. Set a timer to capture the sigalrm signal for time-out control

timeout or other network error

Struct sigaction Act;
Sigaction (sigalrm, null, & Act); // gets the sigalrm signal attribute
Act. sa_handler = handler; // sets the processing function of the sigalrm signal.
Sigaction (sigalrm, & act, null); // you can specify the sigalrm signal attribute.
Alarm (3); // set the timer to 3 seconds
While (1 ){
If (connfd = accept (listenfd, (struct sockaddr *) & peeraddr, & Len) =-1 ){
If (errno = eintr ){
Printf ("timeout \ n ");
Return-1;
}
}


If no data arrives within 3 seconds after the timer, the kernel generates a sigalrm signal to interrupt the current operation. We know that the signal capturing function can be set using the signal function or the sigaction function. However, only the sigaction function can be used here, because the interrupted operation will be re-executed after the signal processing function set by signal is executed.


how to fix nba live mobile network timeout




This article from the "embedded learning world" blog, please be sure to keep this source http://farsight.blog.51cto.com/1821374/1563349



Three methods for network timeout Detection


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.