Time-out settings in Linux network programming

Source: Internet
Author: User

1 The following is the information found on the Internet, first very grateful.

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:
In Linux, it is important to note that the control structure of time is a struct timeval and not an integral number.
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 the 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.

2 Project Problem description

The client needs to go to the server every minute to pull the log, the file size is uncertain, in order to serve the database backup, when the service side database crashes need to repair the path, so the service side need to restart, then the problem, if it is the client sends the thread when the service side is broken, what to do?

Symptom-----> Client is not continuing to send thread, just download thread. Conjecture may be because there is no confirmation message from the server, after debugging found not. It is then positioned above the recv function. Try to set timeout 3 seconds, there is a new problem, that is, the file sent to unlimited send, and finally know the cause of the problem is: Set the time is too short, so that the file is too large to send, the backup database does not clear the file number, causing the loop to send. Set it to a minute just fine.

Come on, sexy years ....

Time-out settings in Linux network programming

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.