Project Home
Project Home
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Timeout on socket connect(): (14 Items)
   
Timeout on socket connect()  
Hi team,

If I connect to a non-existing IP, TCP connect function will timeout only after 74 seconds. In my case, the connect 
function should timeout in milliseconds range. I tried using timer_timeout() with timeout values as 1ms,10ms,100ms etc. 
But it timeout doesn’t work, whereas timeout value if specified in seconds range works correctly. How can I shorten the
 TCP connect timeout?

Regards,
princy
Re: Timeout on socket connect()  
On Fri, Apr 23, 2010 at 09:28:31AM -0400, princy f wrote:
> Hi team,
> 
> If I connect to a non-existing IP, TCP connect function will timeout only after 74 seconds. In my case, the connect 
function should timeout in milliseconds range. I tried using timer_timeout() with timeout values as 1ms,10ms,100ms etc. 
But it timeout doesn???t work, whereas timeout value if specified in seconds range works correctly. How can I shorten 
the TCP connect timeout?

1ms will be pushing it since the timer interrupt generally runs at
that frequency.  Can you post an example?

-seanb
RE: Timeout on socket connect()  

> -----Original Message-----
> From: princy f [mailto:community-noreply@qnx.com]
> Sent: Friday, April 23, 2010 9:29 AM
> To: general-networking
> Subject: Timeout on socket connect()
> 
> Hi team,
> 
As the documentation specifies timer_timeout() applies on the NEXT kernel calls.  Connect() may consist of many  kernel 
calls.

Setting the receive timeout with setsockopt is the way to go.

> If I connect to a non-existing IP, TCP connect function will timeout
> only after 74 seconds. In my case, the connect function should timeout
> in milliseconds range. I tried using timer_timeout() with timeout
> values as 1ms,10ms,100ms etc. But it timeout doesn’t work, whereas
> timeout value if specified in seconds range works correctly. How can I
> shorten the TCP connect timeout?
> 
> Regards,
> princy
> 
> 
> 
> _______________________________________________
> 
> General
> http://community.qnx.com/sf/go/post52509
Re: Timeout on socket connect()  
Are you talking about connect()?

I think what you see is expected behavior; if there is no listener on 
the other side a blocking call to connect() should block for the 
duration that TCP tries the handshake and then return with ETIMEDOUT 
errno. A non-blocking call will return right away with ETIMEDOUT, and 
later you'll get ETIMEDOUT from SO_ERROR after the same amount of time.

The TCP three-way handshake sends SYNs to the other end, and times out 
and retries a number of times if it doesn't get a response. It aborts 
after about 75 seconds, which is what you're observing.

If I was you I'd do a non-blocking connect(), then [p]select() with your 
desired time out...

One problem with setting a very short timer_timeout() is that you may 
get pre-empted after setting the timeout but before you go into 
connect(), and then you may get the sigevent before you enter connect()...

/P

On 23/04/10 09:28 AM, princy f wrote:
> Hi team,
>
> If I connect to a non-existing IP, TCP connect function will timeout only after 74 seconds. In my case, the connect 
function should timeout in milliseconds range. I tried using timer_timeout() with timeout values as 1ms,10ms,100ms etc. 
But it timeout doesn’t work, whereas timeout value if specified in seconds range works correctly. How can I shorten the
 TCP connect timeout?
>
> Regards,
> princy
>
>
>
> _______________________________________________
>
> General
> http://community.qnx.com/sf/go/post52509
>    
Re: Timeout on socket connect()  
Hi,

I tried non blocking connect()  with timeout values set in milliseconds using select(). In some cases, if connect is 
attempted to an invalid IP, select() call returns only after ETIMEDOUT i.e.(after 75s). Sometimes if connect is 
attempted to 
valid IP (server code :-accept() running on other side) , then connect timeouts with EINTR. My requirement is to make
 512 connections in less than 1s. Is there any method to implement timeout for connect() , so as to accept all valid 
connections ?

Regards,
princy
Re: Timeout on socket connect()  

 > valid IP (server code :-accept() running on other side) , then 
connect timeouts with EINTR.
Are you sure it's timing out with EINTR or did it actually get 
"interrupted" as the documentation states?

 >  Is there any method to implement timeout for connect() , so as to 
accept all valid connections ?
                                                 ^^^^^^^^             ^^^^^^
This doesn't make sense. Are you talking about connect            or accept?

/P

Re: Timeout on socket connect()  
HI,

I am talking about connect(). I tried non blocking connect() with timeout values set in milliseconds using select(). In 
some cases, if connect is attempted to an invalid IP, select() call returns only after ETIMEDOUT(with 75 sec). Sometimes
 if connect is attempted to valid IP , then connect timeouts with EINTR. Both are unexpected behaviors. If connect is 
attempted to an in-valid IP with timeout set for connect(), it should always be interrupted with EINTR, also if connect 
attempted to valid IP, it should not return with EINTR or ETIMEDOUT. Is there any method to achieve this?
 
Regards,
princy
Re: Timeout on socket connect()  
> HI,
>
> I am talking about connect(). I tried non blocking connect() with timeout values set in milliseconds using select(). 
In some cases, if connect is attempted to an invalid IP, select() call returns only after ETIMEDOUT(with 75 sec). 
Sometimes if connect is attempted to valid IP , then connect timeouts with EINTR. Both are unexpected behaviors. If 
connect is attempted to an in-valid IP with timeout set for connect(), it should always be interrupted with EINTR, also 
if connect attempted to valid IP, it should not return with EINTR or ETIMEDOUT. Is there any method to achieve this
This doesn't make sense, are you selecting on read/write/exception 
socket sets? Do you have source code you can share?
/P
Re: Timeout on socket connect()  
Hi patrik

I am sorry if I have misled you.

I would like to explain my exact requirement. I have a scenario in which I need to take some specified number of TCP 
connections from a single TCP client program running on a QNX self host machine to 'n' (maximum of 512) number of TCP 
servers running on 'n' different  QNX self host machines(one server per machine). While doing a feasibility on this 
requirement, I could found out that in some cases this operation take around 75 seconds.  We cannot tolerate this time 
delay as it affect our system behaviors in some other aspect. We need to do this operation(operation in the sense, 
connect, send and then close) within a maximum of 2 seconds. If we could only do say, 300 connections within this 
stipulated time, its okay. But the thing is if somebody in our 512 server ip list is not reachable/existing at the time 
of connect, the connect() API blocks for some seconds. We need to change this behavior like if we could not connect to a
 particular server, just skip that after some user defined timeout and take the next server in the 512 list for connect 
operation. I have approached two methods to implement this timeout. 1. timer_timeout() & 2. using select(). The major code snippet in both cases are attached.

The observations in both the scenarios match in some aspect. Still there are differences.

Case 1: If we give an invalid server ip (not existing/unreachable/in which server program is not running),

	In Approach1: In some cases, user specified timeout doesn't work. Connect() call return with ETIMEDOUT after >75seconds  
	In Approach2: User specified timeout doesn't work. select() call return with a value >0 after 75 seconds and get 
ETIMEDOUT from SO_ERROR 

Case 2: If we give a valid server ip (in which server program is running),

	In Approach1: In some cases, connect() fails with EINTR after user specified timeout !!!!!
	In Approach2: select call returns EINTR indicating timedout. User specified timeout works here. 

In case2, we are expected to connect as the server is listening for the client. But it fails!!! May be the client fails 
to connect within the specified timeout. Right?
We need to address 2 scenarios. If there is an invalid server ip, then connect() API should exit after user specified 
timeout and for valid IPs , the connect should proceed.

Can you suggest any better approach to implement time outs in connect and the time out value that we can safely specify?

As you are not getting my real intention through earlier mails, I wrote this long story. I am sorry if I took your 
valuable time behind this.

thanks
princy
Attachment: Text code.txt 1.17 KB
Re: Timeout on socket connect()  
Did you ever look at the packets on the wire? I.e. do you see a TCP SYN 
go out and does the handshake complete when you're using a valid server 
address?

/P
Re: Timeout on socket connect()  
Hi,

 I will check this and get back you with the results.
Regards,
princy
Re: Timeout on socket connect()  
> 	timer_timeout(CLOCK_REALTIME, _NTO_TIMEOUT_REPLY, NULL,&t_timeout, NULL);
> 	ret = connect(sock_fd, (struct sockaddr *)&address, sizeof(address));
>    
I thought it was concluded earlier in this thread that the above isn't 
going to work in general...


> 2. select()
> ==============
>
> 	sock_fd = socket(AF_INET,SOCK_STREAM,0)
>    
This code has bugs. There is code which creates sock_fd but 
create_socket is never created... Later some calls are on sock_fd and 
others are on create_socket. E.g. non blocking is set on create_socket, 
but connect is attempted on sock_fd so connect will be blocking. So the 
behavior you observe is expected with this code.

> 	arg = fcntl(create_socket, F_GETFL, NULL);
> 	arg |= O_NONBLOCK;
> 	fcntl(sock_fd, F_SETFL, arg);
>
> 	ret = connect(sock_fd, (struct sockaddr *)&address, sizeof(address));
>
> 	if (ret<  0)
> 	{
> 		if (errno == EINPROGRESS)
> 		{
> 			tv.tv_sec = 1;
> 			tv.tv_usec = 0; //1000
>
> 			FD_ZERO(&myset);
> 			FD_SET(create_socket,&myset);
>
> 			if (select(create_socket+1, NULL,&myset, NULL,&tv)>  0)
> 			{
> 				lon = sizeof(int);
> 				getsockopt(create_socket, SOL_SOCKET, SO_ERROR, (void*)(&valopt),&lon);
> 				if (valopt)
> 				{
> 					fprintf(stderr, " SO_ERROR in connection %d - %s\n", valopt, strerror(valopt));
> 					exit(0);
> 				}
> 			}	
> 			else
> 			{
> 			   fprintf(stderr, "Select Timedout or error - %s\n", strerror(errno));
> 			   exit(0);
> 			}
> 		}
> 	}
>    
Re: Timeout on socket connect()  
Hi,

'That was a typo error occurred when I inserted code into a text file to attach with the earlier reply. Actually I have 
used the same socket descriptor, sock_fd for fcntl operation'

Regards,
princy
Attachment: Text code.txt 846 bytes
Re: Timeout on socket connect()  
This code works perfectly for me. What version of QNX are you using?

Again, did you have a look at the packets sent on the wire while you 
perform your tests?

I've attached the compilable test code I used (please excuse, I've just 
hacked it up to make it work, don't consider it production ready :-) but 
it was enough to show that things work as expected for me). It is 
basically exactly your code but I printed all return codes and errnos.
/P

Attachment: Text connectselect.c 2.19 KB