Socket programming question
All,
I am trying to receive a UDP packet from a socket and determine the IP address it came from. Is this possible? I am currently able to receive the packet using a bind to any IP address with a specific port. However, the recv call only returns the number of bytes read with no information about the incoming packet (other than the actual data). Thanks in advance. Brian |
For UDP packets, you should be using recvfrom instead of recv. That will get you an address that you are receiving from.
|
Yes, it's possible to determine the IP address it came from. The IP address is stored in a sockaddr_in struct type whose address you have supplied to recvfrom as a pointer. The IP is in its sin_addr field.
The following code will print the foreign IP address: Code:
printf("%s", inet_ntoa(client.sin_addr)); /* which client is your sockaddr_in struct */ |
All times are GMT -5. The time now is 04:08 AM. |