ProgrammingThis 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.
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.
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
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.
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.
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();
}
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.