Timeout wait in "go" blocking mode

Source: Internet
Author: User

Http://www.cnblogs.com/khler/archive/2010/10/27/1863005.html

The "Magical Select function" is widely circulated on the internet, although it is an optional approach, but there are always some prerequisites that do not meet business needs, such as non-blocking mode, the file descriptor for select must be less than the system limit (typically 32), and so on. However, in practice, it is often used in blocking mode to communicate, but do not want to be blocked, such as the recv function, if waiting for a specified time to receive information, it is considered time-out.

Use the SetSockOpt function can achieve this effect, the following paragraph of text from the csnd discussion post, we may refer to the reference, I do not repeat here.

Http://topic.csdn.net/u/20100626/21/7e234d09-76a9-4c0e-94a9-25d524087d87.html?seed=40930018&r=69402126#r_ 69402126

The actual is from: http://blog.csdn.net/newger/archive%20/2008/05/19/2459113.aspx, I also forwarded here, in order to show respect for the original author, suggest everyone to read the original post.

--use setsockopt () to control the timeout of recv () and send ()

in the Send (), recv () process sometimes due to network conditions and other reasons, send and receive can not be expected to proceed, and set the send and receive timeout control:What you need to be aware of in Linux is that the control structure of time is struct timeval instead of an integer, and the following is an excerpt from an article on the web, which reads:
int nnettimeout=1000;//1 seconds,
Set Send timeout
SetSockOpt (Socket,sol_socket,so_sndtimeo, (char *) &nnettimeout,sizeof (int));
Set the Receive timeout
SetSockOpt (Socket,sol_socket,so_rcvtimeo, (char *) &nnettimeout,sizeof (int)); This does not produce any effect in a Linux environment , as defined below : struct Timeval timeout = {3,0};
//Set Send timeout
setsockopt (Socket,sol_socket,so_sndtimeo, (char *) &timeout,sizeof (struct timeval)); //Set receive timeout
setsockopt (Socket,sol_socket,so_rcvtimeo, (char *) &timeout,sizeof (struct timeval)); There are two points to note :1) The fourth parameter of recv () must be Msg_waitall, and the specified number of data in blocking mode will not be returned unless the time-out expires. Also note that as long as the receive timeout is set, it is also valid when there is no Msg_waitall . After all, the timeout is to not let your program wait there for a time to return. 2) Even if the wait timeout value is not reached, but the other party has closed the socket, then recv () will immediately return and receive how much data is returned.

Timeout wait in "go" blocking mode

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.