LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 09-24-2009, 06:24 PM   #1
phsythax
Member
 
Registered: Oct 2005
Location: Denmark
Distribution: Gentoo & XP pro for gaming
Posts: 152

Rep: Reputation: 30
Question nonblocking select() + socket()


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.
 
Old 09-24-2009, 11:37 PM   #2
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
You can't do it the way you're thinking.

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.
 
Old 09-24-2009, 11:55 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
This may be worth a read http://www.beej.us/guide/bgnet/
 
Old 09-25-2009, 01:55 AM   #4
phsythax
Member
 
Registered: Oct 2005
Location: Denmark
Distribution: Gentoo & XP pro for gaming
Posts: 152

Original Poster
Rep: Reputation: 30
Question

Quote:
Originally Posted by orgcandman View Post
You can't do it the way you're thinking.

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?
 
Old 09-25-2009, 08:44 AM   #5
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
man setsockopt

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).
 
Old 09-25-2009, 07:04 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by orgcandman View Post
man setsockopt

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
 
Old 06-25-2010, 12:33 AM   #7
jork
LQ Newbie
 
Registered: Jun 2010
Location: beweth
Distribution: Ubuntu, RHEL, Monta Vista, QNX
Posts: 16

Rep: Reputation: 0
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.

-jork
 
  


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
how do i know if the connection is closed using a nonblocking socket? Thinking Programming 2 10-06-2005 08:53 AM
Select() did not select my socket thvo Programming 1 05-08-2005 12:20 AM
how to find out if a nonblocking socket is closed jwstric2 Programming 3 04-03-2005 10:14 AM
Socket and Select() problems strikernzl Programming 2 09-01-2003 08:34 AM
setting a socket to nonblocking mode raven Programming 1 06-02-2002 10:56 PM

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

All times are GMT -5. The time now is 03:52 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