Archived

This topic is now archived and is closed to further replies.

How to program Sender/Receiver in a Lan..

This topic is 5082 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi ppl, (linux sockets) The program I''m trying (TRYING) to implement is: -> a file (sender.c) is ment to send packages over the Lan, and receive ACKs from the receiver.c ->If there is congestion, the sender must stop at least one second for the receiver to start receiving packages again. The receiver.c simulates a network with 3% packet loss. sender receiver | packet(1) | |-------------> | | ACK(1) | | <--------------| | packet(2) | |-------------> | | ... | | ... | (and so off) Can anyone help me with coding this? NOTE: If you don''t have the time to help me with the above question, please try to answer this offe: -> I need to use the alarm(2) in order to know if there was congestion in the receiver (no ACK received by the sender in 2 seconds). This signal works ok then I use it alone, I mean, after 2 seconds, the expression "Alarm Clock" apears on the screen. However, it doesn''t work if I use is with the function signal(SIGALRM, function): - I have a funtion: void function() { printf("Network is congested! need to wait and resend.."); } - Am I calling the signal() the right way? Wasn''t it supposed to appear "Network is congested! need to wait and resend.."?? Nothing appears on the screen.... Thanks a lot! VisualFX

Share on other sites
First, it''s never safe to do I/O from a signal handler. You''re supposed to set some volatile boolean flag, and then return; nothing more. If you do something more (like call library functions) then your program will contain subtle bugs that only your users will find, and you won''t be able to reproduce to debug. Don''t go there.

Second, your printf() does not contain a newline, and stdout is typically buffered, so you probably want to add a newline and call fflush() to make sure data gets sent. That is, if you call your I/O functions from a routine where it''s safe.

Third, when a signal is handled, blocking system calls typically return with the error EINTR; this is your cue to test the boolean flag to see whether the timeout happened.

Alas, there''s a race condition here. Suppose your code looks like:

flag = false;alarm(2);if( !flag && recv(...) < 0 && errno == EINTR ) {  if( flag ) {    timed out;  }}

The problem with this is that you may (because of scheduling, or whatever), receive the signal after setting the flag to false and testing whether it''s true, but before calling recv(). If that happened, you''d be waiting forever.

Thus, it''s better to code your logic using select() (with a time-out) and a state machine.