LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 12-21-2001, 12:53 PM   #1
krivi
LQ Newbie
 
Registered: Nov 2001
Posts: 25

Rep: Reputation: 15
Socket programming problem


Please help me...
i have problem with connect().In some situations my program don't respond for long time period,because server dont respond.How to set function connect() to conecting just 5 seconds and after that exit...which function to use for timing?
 
Old 12-21-2001, 01:41 PM   #2
ferrous
LQ Newbie
 
Registered: Dec 2001
Location: Atlanta
Distribution: Slackware
Posts: 16

Rep: Reputation: 0
Re: Socket programming problem

Quote:
Originally posted by krivi
Please help me...
i have problem with connect().In some situations my program don't respond for long time period,because server dont respond.How to set function connect() to conecting just 5 seconds and after that exit...which function to use for timing?
This isn't so much a sockets problem as an IPC problem. You need to use the alarm(2) system call to send your program a SIGALRM signal in a given number of seconds. Set up a signal handler with sigaction() to catch the signal and inform the user that the server is crapped-out.

Use alarm once before the connect call specifying the number of seconds and once after the connect call sending it 0 to reset the previous alarm.

I know this was very terse, but it's all in the man pages or online somewhere. Have fun.
 
Old 12-24-2001, 02:28 PM   #3
pinoy
LQ Newbie
 
Registered: Sep 2001
Posts: 21

Rep: Reputation: 15
this is not really a portable solution. In some cases a signal will cause the connect to return -1 with errno set to EINTR, but some implementations will the connect call will be restarted. It has something to do with TCP's handshake mechanism and reliability. It must wait for the otherside to issue either an ACK or an RST, if it doesn't get this right away it will block for a while. What you can do is set connect to non blocking mode and do issue a select on the socket. See fcntl for setting the socket to non blocking mode and select for your time out and connection polling.
 
Old 01-14-2002, 02:04 AM   #4
kervin
Member
 
Registered: Jan 2002
Location: Melbourne, FL.
Distribution: redhat
Posts: 168

Rep: Reputation: 31
You need to connect non-blocking or use the select() function to poll the fd of the socket.

May I recommend the Stevens "UNIX network programming" ISBN 013490012X . This is a classic and a must read for any UNIX network programmer, IMHO.

Also check out the socket FAQ at http://www.lcg.org/sock-faq/ . This document will give you a lot of the information you need.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
socket programming problem...??? arunka Programming 4 10-11-2005 08:08 AM
socket programming iiit Programming 2 07-05-2005 04:02 PM
pthread socket programming problem hk_michael Programming 1 04-03-2005 08:54 PM
Socket Programming cxel91a Programming 4 03-19-2003 10:05 AM
socket programming problem bgraur Programming 2 03-09-2003 09:05 AM

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

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