LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 05-01-2008, 12:14 PM   #1
simasimon
Member
 
Registered: Mar 2006
Posts: 60

Rep: Reputation: 15
Programming timeout with sockets


hi
iīm experiencing some client/server application programming with C. my idea is to be able to share files between client/server, but there is a problem i couldnīt solve for quiet some time.

i am trying to implement a connection timeout while receiving files.. the idea is that if i am receiving a file from the server suppose, and the server goes down, after X amount of seconds the client (which was receiving the file) should timeout.

here is what i tried

Code:
 nfds = sock + 1;
 errno = 0;
 FD_ZERO(&rfds);
        
 while(j < file_size){
       int st;
        
       FD_SET(sock, &rfds);

       printf("llamando a select \n");
       if((st = select(nfds, &rfds, (fd_set *)0, (fd_set *)0, &tv)) <= 0){
                if (errno == EINTR)
                          continue;
                else if (st == 0)
                          error ("Connection timeout\n");
                else
                          printf("Select Error: %d\n", errno);
        }
        
        if(FD_ISSET(sock, &rfds)){
        cc = read (sock, buf, buf_size);
        if (cc < 0)
                error ("Error reading socket\n");
        j += cc;
        if (fwrite (buf, sizeof (char), cc, file) < cc)
                error ("Error writing file\n");
        }
}
what happends is the following. i initiate the transfer of a big file. while transfer is going on, y temrinate the server... but the select function instead of making timeout as i expected, it just kept "rceiving" data, and it never timed out... any ideas?
 
Old 05-01-2008, 02:47 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
First of all, you need to change this:
Code:
       if((st = select(nfds, &rfds, (fd_set *)0, (fd_set *)0, &tv)) <= 0){
                if (errno == EINTR)
                          continue;
                else if (st == 0)
                          error ("Connection timeout\n");
                else
                          printf("Select Error: %d\n", errno);
}
because continue will keep adding the descriptor to rfds:
Code:
while ((st = select(nfds, &rfds, (fd_set *)0, (fd_set *)0, &tv)) < 0 && errno == EINTR);
if (st <= 0)
 { 
if (st <  0) printf("Select Error: %d\n", errno);
else         error ("Connection timeout\n");
break;
 }
}
You should also FD_ZERO after every cycle. Lastly, you need to shutdown sockets when one end exits. Merely closing the descriptor or letting the program exit leaves the socket extant, so the other end doesn't see that the other end is permanently closed until shutdown occurs or the kernel realizes that nothing else can write to the socket.
ta0kira

PS You can replace the while (... && errno == EINTR) with TEMP_FAILURE_RETRY(...).

PPS You also need to re-init the timeout value at the beginning of each cycle since it might be modified as a result of select returning early.

Last edited by ta0kira; 05-01-2008 at 02:50 PM.
 
Old 05-02-2008, 10:26 AM   #3
simasimon
Member
 
Registered: Mar 2006
Posts: 60

Original Poster
Rep: Reputation: 15
select continues returning 1 after i kill the client
 
Old 05-02-2008, 02:31 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Did you try it with the changes I suggested?
ta0kira
 
Old 05-02-2008, 03:22 PM   #5
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Long ago that I programmed something like that, but if I remember correctly:

When the other side hangs up, select will indicate that the socket is readable and you have to read it. When you read it, you will get zero bytes, which indicates that the other side closed the connection and you can close the socket.
You do read, but you don't check for zero bytes (only less than zero).

Last edited by Wim Sturkenboom; 05-02-2008 at 03:24 PM.
 
Old 05-02-2008, 03:36 PM   #6
simasimon
Member
 
Registered: Mar 2006
Posts: 60

Original Poster
Rep: Reputation: 15
Yes i tried thank you taokira but wim saved the day
it works know, thanks a lot!!!
 
Old 05-03-2008, 01:16 AM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by Wim Sturkenboom View Post
When the other side hangs up, select will indicate that the socket is readable and you have to read it. When you read it, you will get zero bytes, which indicates that the other side closed the connection and you can close the socket.
You do read, but you don't check for zero bytes (only less than zero).
Yes, I did forget about that.
ta0kira
 
  


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
is sockets programming my only way? nesta Programming 1 03-23-2007 11:37 AM
Sockets Programming pink_lady Programming 4 05-31-2006 08:56 AM
Question about Sockets Programming sibtay Programming 2 09-24-2004 06:04 PM
Configuring timeout for sockets hairysocks Linux - Networking 0 04-16-2004 08:07 AM
Sockets Programming-Getting Started funkymunky Programming 3 01-09-2004 01:51 PM

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

All times are GMT -5. The time now is 10:57 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration