LinuxQuestions.org
Help answer threads with 0 replies.
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 10-21-2006, 02:19 PM   #1
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Rep: Reputation: 30
connect() with timeout


Im trying to put a timeout on a connect call, and it works fine if I try to contact www.google.com on say port 81, but for some reason if I try to connect to an unopen port on 127.0.0.1 or my router's IP, it succesds all the time.

The method I used is : first I set the socket to non-blocking, tried connecting with connect(), not bothering to check the return value because its always -1, then using select() to check if the socket could be written to.

heres my code:
Code:
bool TcpClient::Connect(	const char* host,
				const unsigned short port,
				const unsigned short timeOut)
{
	// get the IP address
	struct hostent* h=gethostbyname(host);
	if (!h) return false;

	// create socket and set it to non-blocking
	sockfd=socket(PF_INET,SOCK_STREAM,0);
	fcntl(sockfd,F_SETFL,O_NONBLOCK);

	// set up address structure
	remoteAddr.sin_port=htons(port);
	memcpy(&(remoteAddr.sin_addr.s_addr),h->h_addr,sizeof(remoteAddr.sin_addr.s_addr));

	// attempt to connect
	connect(sockfd,(struct sockaddr*)&remoteAddr,sizeof(struct sockaddr));

	// set up file descriptor set
	FD_ZERO(&fds);
	FD_SET(sockfd,&fds);

	// set up timeval struct
	tv.tv_sec=timeOut;
	tv.tv_usec=0;

	// monitor the socket
	if (select(sockfd+1,NULL,&fds,NULL,&tv)<=0) 
	{
		close(sockfd);
		return false;
	}

	return true;
}
Thanks for any help.

Last edited by nodger; 10-21-2006 at 02:23 PM.
 
Old 10-21-2006, 02:34 PM   #2
nodger
Member
 
Registered: Oct 2003
Location: Ireland
Distribution: Slackware 9.1, Ubuntu
Posts: 192

Original Poster
Rep: Reputation: 30
hmm.. i think Ive found a solution. if I just call send(sockfd,0,0,0) and I get a return value of 0, then its connected.
 
Old 10-21-2006, 07:58 PM   #3
_john_i_
Member
 
Registered: Aug 2003
Location: Austin, TX
Distribution: Linux from Scratch
Posts: 52

Rep: Reputation: 15
A method I have used before is to create a signal handler function for SIGALRM, and before calling connect call alarm(timeout); and after the connect call alarm(0);

When the alarm goes off and your signal handler returns, connect will exit, and you just turn off the alarm with the alarm(0).
 
Old 10-21-2006, 10:14 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

There are several ways for a sockets client to "connect() with timeout":

Quote:
1. Timeout with "select()"
I really wouldn't recommend this one for your situation. At best, if I was using UDP "sendto()" and "recvfrom ()", I might use "select()" to verify that the socket became writeable or readable within a specific time before I used it.
Quote:
2. Sigalarm
_john_i_'s suggestion is a good one. For example:
Code:
  /* Adapted from W. Richard Stevens, "Unix Network Programming" */
  oldsigfunc = signam (SIGALRM, mynewfunc);
  if (alarm (nsec) != 0) {
    printf ("WARNING: Alarm already set!\n");
  }

  /* Try to connect */
  iretval = connect (sockfd, &sa, slen);
  if (iretval  < 0) {
    close (sockfd);
    if (errno == EINTR)
      printf ("WARNING: timeout occurred...\n");
  }

  /* Clear Alarm/reset original SIGALRM handler */
  alarm (0);
  signal (SIGALRM, oldsigfunc);

  /* ... continue or not, based on "iretval" ... */
Quote:
3. "setsockopt (SO_RCVTIMEO)"
This is the best option of all ... if your stack supports it.

My version of Stevens (the 2nd Edition, 1998) states that you CANNOT use "setsockopt(SO_RCVTIMEO)" to set a connect timeout. This is no longer true on most OS's, and it has been available on Linux since kernel 2.3.41.
<= If at all possible, I'd encourage you to consider SO_RECVTIMEO
'Hope that helps .. PSM

PS:
Here are some links:
http://publib.boulder.ibm.com/infoce...2/gtpc2m2u.htm
http://linuxreviews.org/man/socket/
 
  


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
ProFTPd ... FTP client fails to connect: timeout after client sends 'LIST' nutnut Linux - Software 2 01-01-2006 07:09 PM
connect() timeout change nodger Programming 11 09-13-2004 07:01 PM
Connect timeout error for adsl vanloke Linux - Hardware 0 02-04-2004 04:31 AM
KDE desktop sharing and connect timeout akidd Linux - Software 2 05-18-2003 12:45 AM
help!!! how can i timeout a socket connect call... ? bzImage Programming 1 03-06-2002 02:54 PM

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

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