After accepting data from a socket, can I view the header for the data? I want to know what IP address the packet was sent to as I am listening on multiple interfaces.
Asked
Active
Viewed 207 times
0
-
2Are you talking about TCP, UDP or something else? – Marcelo Cantos Sep 21 '11 at 12:28
-
What opperating system are you using? – DipSwitch Sep 21 '11 at 12:29
-
For UDP, asked a few times before: http://stackoverflow.com/questions/5281409/get-destination-address-of-a-received-udp-packet – Steve-o Sep 21 '11 at 12:40
1 Answers
3
You can use getsockname
to fetch the local IP address of the socket.
int getsockname(int socket, struct sockaddr *restrict address,
socklen_t *restrict address_len);
Here is an example:
struct sockaddr_in addr;
socklen_t len = sizeof(addr);
memset(&addr, 0, sizeof(addr));
getsockname(s, &addr, &len);

cnicutar
- 178,505
- 25
- 365
- 392