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
- WSADATA ws; //These two lines sets up a Windows Socket
- WSAStartup(0x0101,&ws);
- SOCKET udp_socket; //The socket that sends the bytearray RISP_Frame
- struct sockaddr_in peer;
- int peerlen;
- peer.sin_family = AF_INET;
- peer.sin_port = htons(PORT); //port
- peer.sin_addr.s_addr = inet_addr(IPADRESS); //IP adress
- udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
- sendto(udp_socket, RISP_Frame, 10, 0, (struct sockaddr *)&peer, sizeof(peer)); //The bytearray is sent, no problems so far
- SOCKET udp_socket_rec; //Recieving socket
- char recvbuffer[10]; //Bytearray has a fixed lenth of 10
- int retval;
- peer.sin_family = AF_INET;
- peer.sin_port = htons(1228);
- peer.sin_addr.s_addr = htonl(INADDR_ANY);
- udp_socket_rec = socket(AF_INET, SOCK_DGRAM, 0);
- bind(udp_socket_rec,(struct sockaddr *)&peer,sizeof(peer));
- peerlen=sizeof(peer);
- //When packet loss occurs, this is where the program ends
- retval = recvfrom(udp_socket_rec, recvbuffer, sizeof(recvbuffer), 0, (struct sockaddr *)&peer, &peerlen);
- closesocket(udp_socket_rec);
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