Reputation: 46
I am sending Data through UDP protocol from one c-program to another
when receiving the data i process the string and use it in my functions, the problem is that if i don't have time to receive the new data due to my functions taking more time to finish, the data will stack and then i will have to process old data instead of dealing with the recent ones which is my actual objective to be in real time processing
can anyone help me with this ?
this is my receiving code:
addr_len = sizeof their_addr;
if ((numbytes = recvfrom(sockfd, ubuf, 6 , 0,(struct sockaddr *)&their_addr, &addr_len)) == -1)
{
perror("recvfrom");
//exit(1);
}
inet_ntop(their_addr.ss_family,get_in_addr((struct sockaddr *)&their_addr),s, sizeof s);
//printf("listener: packet is %d bytes long\n", numbytes);
ubuf[numbytes] = '\0';
printf("listener: packet contains \"%s\"\n", ubuf);
Upvotes: 2
Views: 273
Reputation: 5163
If you change your socket into non-blocking mode, you can perform multiple reads at once and discard all data but the last message.
You will need to use some select call in addition to read though. In Win32 (is your code for Win?) there are many options. In Linux, there are poll
,select
and many invariants.
Upvotes: 0
Reputation: 400029
If you're aiming for real-time, then you can't be spending that long on the processing of each packet.
If you do, then you'll always be running "late", and basically discarding most of the data sent to you, that seems kind of pointless and broken.
Still, one way of solving it would probably be to implement a background thread to service the incoming data, and just timestamp it and put it in a queue of some sort. Your "worker" thread can then poll data from the queue, and discard packets that have been hanging around for too long.
Still, that's not really "real-time", it's ... weird.
Upvotes: 2