C Programming: give command a 5 sec interval to run

Click For Summary
The discussion centers around a user seeking help with network programming in C, specifically regarding two programs, A and B, that communicate through packets. Program A sends packets with a sequence number (SN) and waits for an acknowledgment (RN) from Program B to proceed. If the acknowledgment is not received within a set timer, it resends the packet. Program B operates similarly, sending its own packets and waiting for acknowledgments from A.The main issue raised is the implementation of a timer that allows for a 5-second wait for packet reception, akin to the select() function, but monitoring an integer (SN and RN) instead of just a socket. The solution proposed involves using gettimeofday() to record the current time and entering an infinite loop that checks for either the arrival of the correct packet or the expiration of the 5-second interval. The suggestion includes using sleep(5) to avoid unnecessary CPU usage during the wait.
kcirtap
Messages
5
Reaction score
0
Hi all,

I am new to C and I need some help solving an issue with network programming.
So in general, i have two programs running: Program A and Program B.

Description of A:
Send out packet with SN = i
(SN is the label for the packet. i.e first pkt SN=1, 2nd pkt SN=2 etc)
Set timer
If RN=x is received from B, and x=i+1, set SN=i+1.
(RN is label for packet coming back from B)
Otherwise, ignore.

If timer expires, resend pkt SN=i
Reset timer
and repeat.

Description of B:
Send out packet with RN = j
Set timer
If SN=y is received from A, and y=i, set RN=i+1.
Otherwise, ignore.
repeat

In short, these two programs are just loops. A needs to receive an acknowledgment from B to proceed to the next state, otherwise resend the current packet. And for B, if B received a packet from A, it will send an acknowledgment to A telling it to move on to next state.

My problem is with the "Set timer" part. So this part is supposed to allow about 5 seconds for each program to receive pkt. For those who know how to use the select() function, I want something similar select() except it will watch over an integer (like SN and RN) instead of just a socket.

Thanks in advance.
 
Technology news on Phys.org
If I understand correctly, the integer would only be modified by the code that parses packets received over the socket?

In that case, you need to call gettimeofday(), record the value, then start an infinite loop of select(), packet parsing, and more gettimeofday(), that terminates when either of two conditions is satisfied:

- (1) a packet with the correct number has arrived
- or (2) the predetermined time interval has elapsed since you entered into the loop.
 
For a 5 seconds delay use "sleep(5);" , which doesn't waste CPU processing time.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 22 ·
Replies
22
Views
2K
Replies
14
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
19
Views
4K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • Sticky
  • · Replies 13 ·
Replies
13
Views
7K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
3
Views
2K