Block time control of socket connect blocking mode under Linux

Source: Internet
Author: User
Tags socket connect

My colleague asked me today how to control the blocking time of Connect in C code under Linux. The background of the application is: Linux C program has two target IP to connect, if blocking, when one of the IP can not connect the case, the program will block on the Connect function.
Originally thought with setsockopt modify what parameters can be done, the results of Baidu for half a day there is no result. It is found on the internet a lot of such solutions: The connect mode is set to non-blocking mode, so that once the program executes will be returned immediately, but the question is whether there is a connection, you need to wait a period of time, and then use the function to determine whether the connection is normal. Tried, seemingly due to the system differences, this method in my Linux environment is not applicable, the main is to determine whether the connection on the function is not, regardless of the connection or not connected to return a value.
It seems that the online method can not be trusted, and finally in the company's previous section of code found a solution, here to share:

Sigset (SIGALRM, U_alarm_handler);
Alarm (2);
Code = Connect (socket_fd, (struct sockaddr*) &socket_st, sizeof (struct sockaddr_in));
Alarm (0);
Sigrelse (SIGALRM);
First, define an interrupt signal processing function U_alarm_handler, for alarm processing after timeout, then define a 2 second timer, execute connect, when the system connect succeeds, the system performs normally, if Connect does not block successfully here, After 2 seconds of definition, the system generates a signal that triggers the execution of the U_alarm_handler function, and when the U_alarm_handler is executed, the program continues to execute from the following line of Connect.
The processing function can be defined as follows, and more error handling can be added.

void U_alarm_handler ()
{
}
This method is relative to the online approach to the code is small, more sophisticated, it seems that the old code is also a lot of places worth learning.

Block time control of socket connect blocking mode under Linux

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.