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 07-12-2004, 04:08 PM   #1
dravya
LQ Newbie
 
Registered: May 2004
Posts: 23

Rep: Reputation: 15
Question How do I add a timeout for the recv() cmd?


Hi all,

I am a newbie to the socket programming world. I wanted to know how can I add a timeout to the recv() function. There are times when my server hangs when the recv() or write() functions are invoked. Following is an except of my code. The last line the server prints out before it hangs is: "Created received logs file or opened it in write only mode: OK". After this statement the server does not display anything. All i see is a blinking server. On the client side, the program says that it has sent the file and the sending was ok ... but on the server side I don't receive anything. The client can keep sending data but the server never receives it. It stays hung in the original position: for example:

===============================================
Server started listening on port ****
Accepted a connection
[12/Jul/2004 16:44:40 EDT] Client IPAddress is: 1.2.3.4
Created received logs file or opened it in write only mode: OK
(blinking cursor)
===============================================

The times when it works... the output is as follows:

===============================================
Server listening on port ****
Accepted a connection
[12/Jul/2004 09:33:13 EDT] Client IP Address is: 1.2.3.4
Created received logs file or opened it in write only mode: OK
Done copying data from file received from client
Server received 951 bytes
Server has received so far 2783 bytes
Unzipped and changed mod on received log file
Appended new data to all received logs
ALL DONE OK
===============================================

I am not able to replicate this problem. I don't know exactly when it will occur. Thus I am asking for any assistance whatsoever.... the following it the code snippet:


===================================================================
rcv_file = open("received_logs.gz", O_WRONLY | O_CREAT);
if (rcv_file < 0 ) {
printf("Couldn't open received logs file\n");
}
-----> printf("Created received logs file or opened it in write only mode: OK\n"); <--------

int i = 1;
memset(buffer, 0x0, sizeof(buffer));
while ((rcv_data = recv(client, buffer, sizeof(buffer), 0)) > 0) {
total_received += rcv_data;
if ( write(rcv_file, buffer, rcv_data) < 0 ) {
perror("An error occurred while writing to the file\n");
fprintf(server_outputFile, "An error occurred while writing to the file\n");
fflush(server_outputFile);
break;
}
memset(buffer, 0x0, sizeof(buffer));
}
printf("Done copying data from file received from client\n");
====================================================================

thanx a ton

dravya
 
Old 07-13-2004, 12:59 AM   #2
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
you might want to set this socket to be select'ed so that you can add a timeout..
this way if some stuff is not received within some time limit. you can check the errno returned and it is a timeout , close the socket or do whatever you want to do.
 
Old 07-13-2004, 08:52 AM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
There's a socket option called SO_RCVTIMEO that I've never played with. You might be able to change the value. Check out 'man 7 socket' for that option.
 
Old 07-13-2004, 09:48 AM   #4
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
yup...you're right....this can be used to achieve the same effect as select. This and another option SO_SNDTIMEO are used to specify timeouts for recv's and sends.
they use the same structure as select(), timeval.

Last edited by shishir; 07-13-2004 at 09:49 AM.
 
Old 07-13-2004, 10:39 AM   #5
dravya
LQ Newbie
 
Registered: May 2004
Posts: 23

Original Poster
Rep: Reputation: 15
Question

thanx for your replies...

I had already looked in the man pages for SO_RCVTIMEO and SO_SNDTIMEO but couldn't find out a way to change the edit the time on that.
 
Old 07-14-2004, 12:26 AM   #6
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
setsockopt(sockfd,SOL_SOCKET,SO_RCVTIMEO,&tv,sizeof(tv));

this should do...
 
Old 07-14-2004, 09:28 AM   #7
dravya
LQ Newbie
 
Registered: May 2004
Posts: 23

Original Poster
Rep: Reputation: 15
> setsockopt(sockfd,SOL_SOCKET,SO_RCVTIMEO,&tv,sizeof(tv));

doen't this timeout value represent the time the socket will wait for before receiving data? What happens if data is being received and then it hangs in the middle of the data transfer, which is the case in my situation. How do i recover from such a scenario?

Any help is appreciated.

thank you very much

dravya
 
  


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
Can't recv() Ephracis Programming 2 01-04-2005 02:49 PM
How do I add a timeout feature to this code? Dr Twox Programming 3 07-12-2004 04:19 AM
recv() buffer linuxanswer Programming 1 03-22-2004 12:11 PM
xcdroast - cdrecord.mmap: Input/output error. write_g1: scsi sendcmd: cmd timeout masgari Linux - Software 11 03-17-2004 10:55 PM
How to make a thread sleep or std::recv timeout? BrianK Programming 3 10-10-2003 02:35 AM

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

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