LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-12-2004, 06:02 PM   #1
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Rep: Reputation: 30
Talking connect() timeout change


Can the connect() function have its timeout changed? on redhat its about 3 minutes and I want to change that to about 10 seconds.
 
Old 02-12-2004, 07:34 PM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
the solution is using nonblocking i/o and select. here is the function i use:
Code:
//do a nonblocking connect 
//  return -1 on a system call error, 0 on success
//  sa - host to connect to, filled by caller
//  sock - the socket to connect
//  timeout - how long to wait to connect
inline int 
conn_nonb(struct sockaddr_in sa, int sock, int timeout)
{   
    int flags = 0, error = 0, ret = 0;
    fd_set  rset, wset;
    socklen_t   len = sizeof(error);
    struct timeval  ts;
    
    ts.tv_sec = timeout;
    
    //clear out descriptor sets for select
    //add socket to the descriptor sets
    FD_ZERO(&rset);
    FD_SET(sock, &rset);
    wset = rset;    //structure assignment ok
    
    //set socket nonblocking flag
    if( (flags = fcntl(sock, F_GETFL, 0)) < 0)
        return -1;
    
    if(fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0)
        return -1;
    
    //initiate non-blocking connect
    if( (ret = connect(sock, (struct sockaddr *)&sa, 16)) < 0 )
        if (errno != EINPROGRESS)
            return -1;

    if(ret == 0)    //then connect succeeded right away
        goto done;
    
    //we are waiting for connect to complete now
    if( (ret = select(sock + 1, &rset, &wset, NULL, (timeout) ? &ts : NULL)) < 0)
        return -1;
    if(ret == 0){   //we had a timeout
        errno = ETIMEDOUT;
        return -1;
    }

    //we had a positivite return so a descriptor is ready
    if (FD_ISSET(sock, &rset) || FD_ISSET(sock, &wset)){
        if(getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
            return -1;
    }else
        return -1;

    if(error){  //check if we had a socket error
        errno = error;
        return -1;
    }

done:
    //put socket back in blocking mode
    if(fcntl(sock, F_SETFL, flags) < 0)
        return -1;

    return 0;
}
HTH

Last edited by infamous41md; 02-12-2004 at 07:36 PM.
 
Old 02-12-2004, 07:49 PM   #3
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Original Poster
Rep: Reputation: 30
ill give that a try, Thanks.
 
Old 02-12-2004, 08:19 PM   #4
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Original Poster
Rep: Reputation: 30
im afraid that didnt work ,infamous41md. it compiled alright, but given a 10 second timeout it still waited < 1 second and didnt connect to a valid host im not sure whats wrong
 
Old 02-12-2004, 08:55 PM   #5
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
well i use it everyday, so im pretty sure it works could i see how u used it? u say it returned right away? you should be checking the errror return for -1 and calling perror() if it is.
 
Old 02-12-2004, 11:09 PM   #6
schmack
LQ Newbie
 
Registered: Feb 2004
Posts: 12

Rep: Reputation: 0
connect() will return an immediate failure if it is able to determine that it can't establish the connection. This is often the case when you are trying to connect to a socket on the same machine.

The function given above will wait _up to_ timeout seconds. In theory, you could change it to retry every now and then until your timeout value is exhausted.
 
Old 09-04-2004, 08:20 PM   #7
JurajPsycho
Member
 
Registered: Sep 2004
Distribution: Debian, kernel 2.6.10
Posts: 50

Rep: Reputation: 15
:(

well, I did try your code infamous41md , and it compiles OK, but when I use it, the timeout comes after 333 sec, while when I used usuall connect, timeout came after 189 sec....
Code:
 
    s_in.sin_family = PF_INET;
    s_in.sin_port = htons(21);
    sprintf(ip,"147.232.153.5");   //just for testing purposes, it's NOT my IP
    s_in.sin_addr.s_addr=inet_addr(ip);
    con = conn_nonb(s_in,s,10);
    if(con==-1)
    {
            printf("%d - IP = %s - Connection failed\n",getpid(),ip);
            perror();
    }
What am I doing wrong? Thx for help.
J.
 
Old 09-04-2004, 08:22 PM   #8
JurajPsycho
Member
 
Registered: Sep 2004
Distribution: Debian, kernel 2.6.10
Posts: 50

Rep: Reputation: 15
oh, I forgot:
Code:
    int s;
    s = socket(PF_INET, SOCK_STREAM, 0);
    if(s<0)
    {
        perror("socket");
    }
 
Old 09-04-2004, 08:45 PM   #9
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
I believe you can use setsockopt() with the SO_RCVTIMEO or SO_SNDTIMEO options. I'm not sure if those settings have any effect on connect(), but it should give you a place to look
 
Old 09-11-2004, 06:35 PM   #10
JurajPsycho
Member
 
Registered: Sep 2004
Distribution: Debian, kernel 2.6.10
Posts: 50

Rep: Reputation: 15
well, I want to do that under Linux...
 
Old 09-11-2004, 07:26 PM   #11
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
add this:
ts.tv_usec = 0;

right below
ts.tv_sec = timeout
 
Old 09-13-2004, 07:01 PM   #12
JurajPsycho
Member
 
Registered: Sep 2004
Distribution: Debian, kernel 2.6.10
Posts: 50

Rep: Reputation: 15
thx

thx, now it works fine
J.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
change the timeout on boot billb Linux - General 7 12-25-2004 09:40 PM
change GRUB timeout Gijet Red Hat 3 02-12-2004 10:53 AM
Connect timeout error for adsl vanloke Linux - Hardware 0 02-04-2004 04:31 AM
how to change telnet timeout? qnguyendang Linux - Networking 0 02-17-2003 04:13 AM
help!!! how can i timeout a socket connect call... ? bzImage Programming 1 03-06-2002 02:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration