开发者

View socket data header?

开发者 https://www.devze.com 2023-04-06 04:53 出处:网络
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.You can use getsoc

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.


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

精彩评论

暂无评论...
验证码 换一张
取 消