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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
09-15-2004, 04:47 AM
|
#1
|
LQ Newbie
Registered: Jun 2003
Location: Reading, UK
Distribution: RH 7.2
Posts: 26
Rep:
|
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.
|
|
|
09-15-2004, 10:31 AM
|
#2
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
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().
|
|
|
09-20-2004, 09:52 AM
|
#3
|
LQ Newbie
Registered: Jun 2003
Location: Reading, UK
Distribution: RH 7.2
Posts: 26
Original Poster
Rep:
|
Cheers 
|
|
|
09-27-2004, 09:13 AM
|
#4
|
LQ Newbie
Registered: Jun 2003
Location: Reading, UK
Distribution: RH 7.2
Posts: 26
Original Poster
Rep:
|
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
|
|
|
09-27-2004, 09:40 AM
|
#5
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
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?
|
|
|
09-27-2004, 09:46 AM
|
#6
|
LQ Newbie
Registered: Jun 2003
Location: Reading, UK
Distribution: RH 7.2
Posts: 26
Original Poster
Rep:
|
Can you make it a bit more obvious?
 Doh! Brain not working properly.
Cheers again.
|
|
|
09-27-2004, 09:47 AM
|
#7
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
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);
}
|
|
|
All times are GMT -5. The time now is 10:06 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|