I have a problem concerning an UDP socket in C++ (Winsock).
The next paragraphs is merely to explain the system I am working on. If U want to skip it, I have marked the question in bold below it.
I have a pc, connected to a network of boards, all connected in a ring, all having their own unique IP adresses.
As it is now, I send messages to the network of boards via broadcast, so all the boards gets the message.
All the boards looks at the message, and if they are'nt the reciever of the message, they just passes it to the next board in the ring.
One of the boards is connected directly to the PC, and is therefore acting as the Gateway-board - and that is exactly the core of my question:
I want only the first message to be sent over broadcast, since the response wil come from the gateway-board. All the following messages only needs to be sent to the gateway-board. If the message is not for the gateway board, it will just pass it along to the next board, which will do the same, until the message reaches the desired board. Here a response will be generated, and a message will be send back trough the network, ending at the gateway-board, which will send it to the PC.
All this was just background - my question is fairly simple (but for the , the answer is'nt):
The fist message I send over broadcast, will recieve response from the gateway-board. How do I in C++ extract the IP adress from that response - the IP adress of the Gateway-board?
If I can get the IP adress, I can store it, and make it so that all other messages are sent to that adress, instead of broadcast.
I am using UDP (Winsock) socket in C++. I dont know is there is a predefines funtion to extract the IP adress from a UDP socket response
I hope that someone is able to help.
Best regards and thanks in advance.
/Sagi
P.S. I dont know if it helps, but il post my code here, so U can see it:
Expand|Select|Wrap|Line Numbers
- WSADATA ws;
- WSAStartup(0x0101,&ws);
- struct sockaddr_in peer;
- int peerlen = 0;
- peer.sin_family = AF_INET;
- peer.sin_port = htons(8998); //port
- peer.sin_addr.s_addr = inet_addr("192.168.34.255"); //Broadcast IP adress
- udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
- sendto(udp_socket, RISP_Frame, 10, 0, (struct sockaddr *)&peer, sizeof(peer));
- //The buffer array needs to be empty
- char recvbuffer[10] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
- int retval = 0;
- 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));
- FD_ZERO(&readSet);
- FD_SET(udp_socket_rec, &readSet);
- timeVal.tv_sec = 1;
- timeVal.tv_usec = 0;
- for (int tries =0; tries <= 2; tries ++) //3 tries before transmitting is aborted
- {
- if (select(udp_socket_rec, &readSet, NULL, NULL, &timeVal)) //Checks if there is something in the socket
- {
- peerlen=sizeof(peer);
- retval = recvfrom(udp_socket_rec, recvbuffer, sizeof(recvbuffer), 0, (struct sockaddr *)&peer, &peerlen);
- closesocket(udp_socket_rec);
- /* bla bla bla */
- break;
- }
- else
- {
- cout << "Retransmitting..." << endl;
- closesocket(udp_socket);
- /* A new socketconnection is established and so an and so forth */