C Programming: give command a 5 sec interval to run

Click For Summary
SUMMARY

The discussion focuses on implementing a 5-second interval for packet acknowledgment in C network programming. The user describes two programs, A and B, where A sends packets with sequence numbers (SN) and waits for acknowledgments (RN) from B. The solution involves using the gettimeofday() function to track time and an infinite loop with select() for monitoring packet reception. The recommended approach includes using sleep(5); to avoid excessive CPU usage while waiting for packet acknowledgment.

PREREQUISITES
  • Understanding of C programming and its syntax
  • Familiarity with network programming concepts
  • Knowledge of the select() function for I/O multiplexing
  • Experience with time management functions like gettimeofday()
NEXT STEPS
  • Research the implementation of the select() function in C for monitoring multiple file descriptors
  • Learn about the gettimeofday() function and its usage in timing operations
  • Explore the use of sleep() and its impact on CPU resource management
  • Investigate error handling techniques in network programming for robust application development
USEFUL FOR

Network programmers, C developers, and anyone involved in implementing packet acknowledgment mechanisms in network applications will benefit from this discussion.

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.
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
2K
Replies
14
Views
4K
  • · 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
8K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K
Replies
3
Views
2K