Quintin
Quintin

Reputation: 109

View socket data header?

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.

Upvotes: 0

Views: 221

Answers (1)

cnicutar
cnicutar

Reputation: 182619

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);

Upvotes: 3

Related Questions