LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Socket programming question (https://www.linuxquestions.org/questions/programming-9/socket-programming-question-271973/)

brianvdc 12-30-2004 12:53 PM

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

deiussum 12-30-2004 01:06 PM

For UDP packets, you should be using recvfrom instead of recv. That will get you an address that you are receiving from.

Nerox 12-30-2004 01:36 PM

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 */
I hope it helps


All times are GMT -5. The time now is 04:08 AM.