473,224 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

UDP socket - how to make a timer and how to handle packet loss

Sagittarius
I will first try to describe my problem in words.

I have a simple program, written in C++, that needs to send a single bytearray via a UDP socket to a microprocessor, which returns an answer, also via UDP.

Everything works just fine - exept when packet loss occurs (or nothing is recieved at the recieving socket).

If packet loss occurs, the program locks up, and it needs to be restarted!

Here comes the question:

How do I make the program wait for a given amount of time, to recieve something on the socket? If nothing has arrived, I will need to retransmit. If multiple retransmits wont help, an error should be returned.

What I dont know is:

- How do I 'check that something has arrived in the socket', now that the program 'locks up'?
- How do I, in an easy way, make the program wait, lets say 2 seconds, and then retransmit if nothing has arrived?

My problem is mainly the syntax, and also that I am very unexperienced in socket-programming, so what might be simple to everyone, is not that simple for me.

My code for the socket-is as follows:

Expand|Select|Wrap|Line Numbers
  1. WSADATA ws; //These two lines sets up a Windows Socket
  2. WSAStartup(0x0101,&ws);
  3.  
  4. SOCKET udp_socket; //The socket that sends the bytearray RISP_Frame
  5. struct sockaddr_in peer;
  6. int peerlen;
  7.  
  8.   peer.sin_family = AF_INET;
  9.   peer.sin_port = htons(PORT); //port
  10.   peer.sin_addr.s_addr = inet_addr(IPADRESS); //IP adress
  11.  
  12.   udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
  13.  
  14.   sendto(udp_socket, RISP_Frame, 10, 0, (struct sockaddr *)&peer, sizeof(peer)); //The bytearray is sent, no problems so far
  15.  
  16.  
  17.    SOCKET udp_socket_rec; //Recieving socket
  18.  
  19.    char recvbuffer[10]; //Bytearray has a fixed lenth of 10
  20.    int retval;
  21.  
  22.    peer.sin_family = AF_INET;
  23.    peer.sin_port = htons(1228);
  24.    peer.sin_addr.s_addr = htonl(INADDR_ANY);
  25.  
  26.    udp_socket_rec = socket(AF_INET, SOCK_DGRAM, 0);
  27.  
  28.    bind(udp_socket_rec,(struct sockaddr *)&peer,sizeof(peer));
  29.  
  30.    peerlen=sizeof(peer);
  31.  
  32. //When packet loss occurs, this is where the program ends
  33.  
  34.    retval = recvfrom(udp_socket_rec, recvbuffer, sizeof(recvbuffer), 0, (struct sockaddr *)&peer, &peerlen);
  35.  
  36.    closesocket(udp_socket_rec);
  37.  
I hope that you are able to help me.

As said before - it works as it should - the only problem is to make it stabile.

I look forward to see if you have any solutiuon-proposals.

Best regards to all of you

/Sagi
Feb 6 '07 #1
6 15659
horace1
1,510 Expert 1GB
where you call recvfrom() you have a loop calling either select() or poll() to determine if data has arrived - if so you then call recvfrom() to read the data. If nothing has arrived within your time limit up exit the loop and send a datagram to the micrprocessor again, etc etc.
see the recvfrom() man page, e.g.
http://www.die.net/doc/linux/man/man2/recvfrom.2.html
Feb 6 '07 #2
where you call recvfrom() you have a loop calling either select() or poll() to determine if data has arrived - if so you then call recvfrom() to read the data. If nothing has arrived within your time limit up exit the loop and send a datagram to the micrprocessor again, etc etc.
see the recvfrom() man page, e.g.
http://www.die.net/doc/linux/man/man2/recvfrom.2.html
Thanks a lot buddy - it makes perfect sense :) The functionallity of select and poll was just what I was looking for.

I have chosen to use select().

However I am stuck with a new problem now - I cant figure out how I am supposed to call select()

I have found various examples on the web, and very good descriptions, but I just cant make it compile.

The arguments for Select are(as far as I know):

select(a, b, c, d, e)

a) The first is used for backward-compatibility, and can be set to any integer I want.

b) The second argument I am a bit confused about - according to my sources I have found, it is a File Descriptor which points to a socket that i wish to check for readability (which is what I want) - do I, in my case, just need to insert the socket in question (udp_socket_rec) here, or is that not allowed?

c & d) This is the same as argument b), only difference is that c) if for writeability-check and d) is for error check.

e) As far as I know, this is a timer, which in miliseconds state for how long the select-function os to listen on the socket. Since I am going to put my select()-function inside a loop, and therefore there is no need for a timer, what should i give as value to this argument.


Putting the function into a loop and how to retransmit the package is not a problem - what I really need to know is how to call select.

An example, how to check on the socket in my program above, just once, would be very appreciated :)

Again, thanks for your quick answer

Best regards and have a nice weekend everyone

/Sagi

P.S. my main source for my socket programming can be found here
Feb 9 '07 #3
horace1
1,510 Expert 1GB
have a look at this thread
http://www.ussg.iu.edu/hypermail/linux/kernel/0001.2/0756.html
Feb 9 '07 #4
horace1
1,510 Expert 1GB
have a look at the udp-client.cc on URL
http://sage.mc.yu.edu/kbeen/teaching/networking/resources/sockets.html
which uses select so
Expand|Select|Wrap|Line Numbers
  1.     // wait until answer comes back, for up to 1 second
  2.     FD_ZERO(&readSet);
  3.     FD_SET(socketDescriptor, &readSet);
  4.     timeVal.tv_sec = 1;
  5.     timeVal.tv_usec = 0;
  6.  
  7.     if (select(socketDescriptor+1, &readSet, NULL, NULL, &timeVal)) {
  8.       // Read the modified line back from the server.
  9.  
the UDP server and client work OK on my machine
Feb 9 '07 #5
Thank you so much for your help :)

Everything works now.

It is really appreciated.

All the best regards

/Sagi
Feb 21 '07 #6
horace1
1,510 Expert 1GB
Thank you so much for your help :)

Everything works now.

It is really appreciated.

All the best regards

/Sagi
always pleased to help!
Feb 21 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Stephan Steiner | last post by:
Hi I have a networking application that periodically needs to go into sleep mode (from an application point of view, I'm simply suspending the receiver thread until it's time to start listening...
3
by: Stephan Steiner | last post by:
Hi I have a small program listening to UDP broadcast datagrams that are periodically sent out. It will stop listening for a certain period if either a sufficient number of packets has been...
6
by: Anders Both | last post by:
If you send data using TCP and Socket, can the client then be 100% sure that if it send´s data and no exception uccur, then the data will also arrived in a a correct way on the server. What if...
6
by: roger beniot | last post by:
I have a program that launches multiple threads with a ThreadStart method like the following (using System.Net.Sockets.Socket for UDP packet transfers to a server): ThreadStart pseudo code: ...
2
by: allen | last post by:
I have an async socket callback for a UDP socket. When the callback is fired after receiving a packet, I want to map the packet to a structure I have. note, all unsafe code blocks here. //...
0
by: Macca | last post by:
Hi, I am writing an asychronous socket server to handle 20+ simulataneous connections. I have used the example in MSDN as a base. The code is shown at end of question. Each connection has a...
4
by: gaddamreddy | last post by:
Hai to all frns, Reqirement 1: 1.I need to send a Request to one server through UDP socket.at that time i have to start a timer (setted to 2 sec) if i wont get the Response/ACK from that...
2
by: Nick Toop | last post by:
Hi, I have a Java application which runs a server socket on a PC. Various sensors (using microprocessors) can call it up and each gets its own thread running the socket connection. The...
21
by: puzzlecracker | last post by:
Problem: I send a lot of requests to the application (running on a different box, of course), and I receive back responses from the app . Below: socket corresponds to Socket socket=new...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.