LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How do I add a timeout for the recv() cmd? (https://www.linuxquestions.org/questions/programming-9/how-do-i-add-a-timeout-for-the-recv-cmd-204216/)

dravya 07-12-2004 04:08 PM

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

shishir 07-13-2004 12:59 AM

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.

itsme86 07-13-2004 08:52 AM

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.

shishir 07-13-2004 09:48 AM

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.

dravya 07-13-2004 10:39 AM

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.

shishir 07-14-2004 12:26 AM

setsockopt(sockfd,SOL_SOCKET,SO_RCVTIMEO,&tv,sizeof(tv));

this should do...

dravya 07-14-2004 09:28 AM

> 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


All times are GMT -5. The time now is 02:22 AM.