Change connect () timeout

I searched a bit and cannot find the answer I'm looking for, the only answers I could find were to use select to check if the socket was expiring, which I already do.

What do I want to know, one way or another, to change the time before connect() will time out? I am currently using select() , which returns with errno set to EINPROGRESS until it returns with ETIMEDOUT . Anyway, can I change the amount of time it takes before ETIMEDOUT happens? Currently, this happens after about 60 seconds. I tried to set the timeout value that I pass to the select() call, however this only affects how long it takes before the select() time select() .

+6
source share
2 answers
  • Create a socket.
  • Put it in non-blocking mode.
  • The connect() problem.
  • Call select() or poll() or epoll() , specifying the desired timeout and specifying the socket as writefd , i.e. blocking until the timeout expires or the socket becomes writable.
  • If the timeout expires, close the socket, etc.
  • Otherwise, get the last error in the socket through iotcl() and see if it is zero. If so, the connection succeeded, otherwise it did not work.

Please note that you cannot increase the connection timeout outside the default platform (about a minute) using this tool, but you can reduce it.

+2
source

setsockopt (3) allows you to install this: http://linux.die.net/man/3/setsockopt

A bit confusing, timeout values ​​are actually socket properties.

Parameters you are looking for: SO_SNDTIMEO and SO_RCVTIMEO

EDIT As indicated in the comments, this does not work for the connection. Here's why: http://www.sekuda.com/overriding_the_default_linux_kernel_20_second_tcp_socket_connect_timeout As a solution, it is proposed to set limits on the number of repeated SYN packets sent by the kernel to establish a connection after an initial initial failure. The time doubles since the last retry, which means: a) you can only set the number of repetitions, which indirectly adds the value of the connection timeout and b) it depends on the OS and the system. Not a solution for you, probably ...

+1
source

Source: https://habr.com/ru/post/947451/


All Articles