470,641 Members | 1,549 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,641 developers. It's quick & easy.

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 14816
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

Post your reply

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

Similar topics

4 posts views Thread by Stephan Steiner | last post: by
3 posts views Thread by Stephan Steiner | last post: by
6 posts views Thread by Anders Both | last post: by
2 posts views Thread by allen | last post: by
reply views Thread by Macca | last post: by
21 posts views Thread by puzzlecracker | last post: by
1 post views Thread by Korara | last post: by
???
1 post views Thread by Stoney L | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.