ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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");
====================================================================
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.
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.
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.
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.