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.
I would like to use the following example to timeout a connection attempt, made by a TCP socket:
Code:
/*
** select.c -- a select() demo
*/
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#define STDIN 0 // file descriptor for standard input
int main(void){
struct timeval tv;
fd_set readfds;
tv.tv_sec = 2;
tv.tv_usec = 500000;
FD_ZERO(&readfds);
FD_SET(STDIN, &readfds);
// don't care about writefds and exceptfds:
select(STDIN+1, &readfds, NULL, NULL, &tv);
if (FD_ISSET(STDIN, &readfds))
printf("A key was pressed!\n");
else
printf("Timed out.\n");
return 0;
}
my socket function looks like this:
Code:
int checkprt(int port, char *ip){
int test = 0;
int sockfd;
struct sockaddr_in servaddr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0) {
printf("error. cannot creat socket\n");
return ERROR;
}
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(ip);
servaddr.sin_port = htons(port);
test = connect(sockfd, (struct sockaddr_in *)&servaddr, sizeof(servaddr));
if(test == -1){
close(sockfd);
return CLOSED;
}
close(sockfd);
return OPEN;
}
As you probably have guessed I am in the middle of learning socket programming. Any help here on how to use the select() example to timeout a connect attempt would be appreciated.
There's a non-portable (read LINUX) way of doing something similar:
The socket option TCP_SYNCNT. It's defaulted to 5, which corresponds to 180 seconds. However, you could easily make this number.. say.. 2. And your connect will time out much more quickly.
There's a non-portable (read LINUX) way of doing something similar:
The socket option TCP_SYNCNT. It's defaulted to 5, which corresponds to 180 seconds. However, you could easily make this number.. say.. 2. And your connect will time out much more quickly.
I've found out that on my system (gentoo updated), its defaulted to 7.
What would the best method of altering this value be without doing it permanently?
Just set the TCP_SYNCNT socket option. This has the advantage of working the way you expect on any linux machine (but other systems probably will fail).
Just set the TCP_SYNCNT socket option. This has the advantage of working the way you expect on any linux machine (but other systems probably will fail).
This can probably be averted with #ifdef TCP_SYNCNT /*...*/ #endif around the corresponding code. Those without Linux will just be out of luck!
Kevin Barry
Much more easy and standard way for this. Put the socket in non-blocking mode and call the connect after that. Connect will return immediately with EINPROGRESS. Now you call the select again for this socket, with the desired timeout. Now the select will wake up once the socket gets connected or it will timeout.
This is the standard async socket programming way and I hope this is the way you wanted to follow.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.