LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 09-15-2004, 04:47 AM   #1
P_Shep
LQ Newbie
 
Registered: Jun 2003
Location: Reading, UK
Distribution: RH 7.2
Posts: 26

Rep: Reputation: 15
Reducing TCP/IP connection timeouts


Hi there.

I'm looking for a way of reducing the time taken to move on after attempting to connect to a non-existant server.

I've had a couple of thoughts:
Test for server first
Reduce the timeout time
reduced the number of re-tries
multi-thread the app

The latter of which i'd prefer not to do since it'll probably require the most re-programming.

I'm using RH 7.0 and programming in C (whatever GCC version it on there!)

Can anyone point me in the direction of info on how to do the former three?

TIA.

Last edited by P_Shep; 09-15-2004 at 04:49 AM.
 
Old 09-15-2004, 10:31 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Your best bet is to make your connection socket non-blocking. Then you can use select() to create whatever timeout you'd like.

Check out the man page for fcntl().
 
Old 09-20-2004, 09:52 AM   #3
P_Shep
LQ Newbie
 
Registered: Jun 2003
Location: Reading, UK
Distribution: RH 7.2
Posts: 26

Original Poster
Rep: Reputation: 15
Cheers
 
Old 09-27-2004, 09:13 AM   #4
P_Shep
LQ Newbie
 
Registered: Jun 2003
Location: Reading, UK
Distribution: RH 7.2
Posts: 26

Original Poster
Rep: Reputation: 15
OK done that
create socket
turn socket into non-blocking
add socket to FDSET
connect
Select
if FDSET complete connect

Question is now, how do I test to see if the connection is still being attempted?
If i wait say 10s and no connection is completed in that time, I'll try and re-connect, but i'm pretty sure taht would cause problems, no?

TIA
 
Old 09-27-2004, 09:40 AM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
This is from 'man connect':
Quote:
EINPROGRESS
The socket is non-blocking and the connection can-
not be completed immediately. It is possible to
select(2) or poll(2) for completion by selecting
the socket for writing. After select indicates
writability, use getsockopt(2) to read the SO_ERROR
option at level SOL_SOCKET to determine whether
connect completed successfully (SO_ERROR is zero)
or unsuccessfully (SO_ERROR is one of the usual
error codes listed here, explaining the reason for
the failure).
Is that helpful?
 
Old 09-27-2004, 09:46 AM   #6
P_Shep
LQ Newbie
 
Registered: Jun 2003
Location: Reading, UK
Distribution: RH 7.2
Posts: 26

Original Poster
Rep: Reputation: 15
Can you make it a bit more obvious?

Doh! Brain not working properly.

Cheers again.
 
Old 09-27-2004, 09:47 AM   #7
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Here's a tiny section of code that I wrote once for an email plugin that does what you're trying to do:
Code:
  for(e = pending_email_list;e;e = enext)
  {
    enext = e->next;

    if(!FD_ISSET(e->socket, &writeset))
      continue;

    getsockopt(e->socket, SOL_SOCKET, SO_ERROR, (void *)&err, &len);
    if(err)    /* connect() failed */
    {
      while((rv = try_next_server(e)) == 0);   /* Whole loop */
      if(rv == -1)
      {
        notifyf(e->player, "I couldn't send email to %s.",
          (e->victim)?name(e->victim):e->send_to);
        del_email_send(e);
      }
      continue;
    }

    blocking_flags = fcntl(e->socket, F_GETFL);
    blocking_flags &= ~O_NONBLOCK;
    fcntl(e->socket, F_SETFL, blocking_flags);
    send_email(e);
    del_email_send(e);
  }
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Connection Timeouts lobo78 Linux - Networking 1 11-01-2005 06:40 PM
hijacking tcp connection atul_mehrotra Linux - Networking 4 04-30-2005 12:50 AM
To establish TCP/IP connection over UART vinaybms Linux - Networking 1 03-07-2005 10:41 AM
enabling tcp/ip connection via pg_ctl vickr1z Linux - Newbie 3 10-19-2004 06:21 AM
Unable to get TCP/IP connection kgunnar Linux - Newbie 9 05-01-2002 04:24 AM

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

All times are GMT -5. The time now is 10:06 AM.

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