How to Set a timeout mechanism for socket Io operations

Source: Internet
Author: User
There are three main solutions: (1) Calling Alarm, which generates a sigalrm signal when the specified timeout period expires. This method involves signal processing, and there are differences in signal processing in different implementations, and may interfere with existing alarm calls in the process. (2) block wait IO in select, that is, multiplexing, because select has built-in time restrictions, in place of directly blocking calls on read or write. (3) Use the newer so_rcvtimeo and so_sndtimeo socket options. The problem with this method is that not all implementations support these two socket options. I. First, let's look at how to call alarm. It is mainly done by generating an interruption signal for slow system calls.
 
Int connect_timeo (INT sockfd, const Sa * saptr, socklen_t Salen, int nsec) {sigfunc * sigfunc; int N; sigfunc = signal (sigalrm, connect_alarm ); if (alarm (nsec )! = 0) err_msg ("alarm has already been set! "); If (n = connect (sockfd, saptr, salen) <0) {// If nsec seconds are exceeintr close (sockfd ); // close this socket to prevent the three-way handshake from continuing if (errno = eintr) // the interruption signal generated by the slow System Call errno = etimedout;} alarm (0); signal (sigalrm, sigfunc); Return N;} static void connect_alarm (INT signo) {return ;}

 
 
Ii. Let's take a look at how select multiplexing achieves this. It mainly sets the blocked descriptor timeout through the fifth parameter.
 
Int readable_timeo (int fd, int Sec) {fd_set rset; struct timeval TV; // This struct is mainly set, the struct is passed to the select function fd_zero (& rset); fd_set (FD, & rset); TV. TV _sec = sec; TV. TV _usec = 0; Return (select (FD + 1, & rset, null, null, & TV ));}

In this way, you can call this function to set the time-out seconds before each read call. As follows:
 
Int readable_timeo (int fd, int Sec) {fd_set rset; struct timeval TV; // This struct is mainly set, the struct is passed to the select function fd_zero (& rset); fd_set (FD, & rset); TV. TV _sec = sec; TV. TV _usec = 0; Return (select (FD + 1, & rset, null, null, & TV ));}

3. Use the so_rcvtimeo socket option to set timeout for recvfrom. Once this option is set to a descriptor, the timeout setting is applied to all read operations on the descriptor. The advantage of this method is one setting, the preceding two methods must be set before each operation. In addition, this socket option is only used for read operations. Similar to so_sndtimeo, it is only used for write operations, but neither can be used to set timeout for connect. From the name, we can see that one is to receive, one is sending.

 
Struct timeval TV; TV. TV _sec = 5; TV. TV _usec = 0 setsockopt (sockfd, sol_socket, so_rcvtimeo, & TV, sizeof (TV); // you can set the timeout value by setting the socket option while (fgets (sendline, maxline, stdin )! = NULL) {sendto (sockfd, sendline, strlen (sendline), 0, pservaddr, servlen); n = recvfrom (sockfd, recvline, maxline, 0, null, null ); if (n <0) {If (errno = ewouldblock) {// This is a bit like a non-blocking socket, that is, the returned value of fprintf (stderr, "socket read timeout! "); Continue;} else err_sys (" recvfrom error! ") ;}Recvline [N] = 0; fputs (recvline, stdout );}

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.