Linux sockaddr - 打印 sa_data 保存的所有信息 - C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4463676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sockaddr - print all information which sa_data holds - c++
提问by gln
in my c++ application I' using sockaddr. I want to see all the infarmation which sockaddr.sa_data[14] holds. for now I just print the ip from sa_data[2].sa_data[3].sa_data[4].sa_data[5].
在我的 C++ 应用程序中,我使用 sockaddr。我想查看 sockaddr.sa_data[14] 保存的所有信息。现在我只是从 sa_data[2].sa_data[3].sa_data[4].sa_data[5] 打印 IP。
I want to print in a way that I can understand (and please explain) all the information in the sa_data 14 bytes.
我想以一种我可以理解(并请解释)sa_data 14 字节中的所有信息的方式打印。
any help?
有什么帮助吗?
thanks!
谢谢!
采纳答案by Mark Wilkins
回答by CashCow
std::copy( &sa_data[0], &sa_data[0]+sizeof(sa_data)/sizeof(sa_data[0]),
std::ostream_iterator<int>(std::cout, " "));
will print each element as an int with a space separating. You can also use unsigned int if you don't want negative values and you can iomanip your stream to print hex if you prefer that.
将每个元素打印为 int 并用空格分隔。如果您不想要负值,您也可以使用 unsigned int,如果您愿意,您可以 iomanip 流打印十六进制。
回答by Jason Orendorff
The information in sockaddr
depends on what socket family and protocol you're using.
中的信息sockaddr
取决于您使用的套接字系列和协议。
If you're using IPv4, the right thing is to cast the sockaddr
pointer to sockaddr_in *
. Only the first 6 bytes of the address are meaningful when you're using IPv4. The rest should just be ignored.
如果您使用的是 IPv4,正确的做法是将sockaddr
指针转换为sockaddr_in *
. 当您使用 IPv4 时,只有地址的前 6 个字节有意义。其余的应该被忽略。
回答by Steve Hibbert
In the sa_data member, for IPv4 on Windows, I have found that the first two bytes hold the port number, and the next four hold the IP address.
在 sa_data 成员中,对于 Windows 上的 IPv4,我发现前两个字节保存端口号,接下来的四个字节保存 IP 地址。
For example, if I resolve the address 228.0.0.1:9995
, the sa_data member is...
例如,如果我解析 address 228.0.0.1:9995
,则 sa_data 成员是...
27 0b e4 00 00 01 00 00 00 00 00 00 00 00
Here, 270b
is the Hex value representation of 9995 in the first two bytes. The next four bytes are the IP address, where 0xe4
is 228, then two zeros, then 0x01
, or 228 0 0 1.
The last eight bytes are unused, which tallies with the comment above about only the first six bytes being used.
这里,270b
是前两个字节中 9995 的十六进制值表示。接下来的四个字节是 IP 地址,其中0xe4
是 228,然后是两个零,然后是0x01
228 0 0 1。
最后八个字节未使用,这与上面关于仅使用前六个字节的注释相符。
Note that sa_data will vary in format with the protocol being used.
请注意, sa_data 的格式将随所使用的协议而异。
回答by hailelil
the value of the sa_data which is 14 bytes is changed(are different) based on the address family : sa_family.
14 字节的 sa_data 的值根据地址族:sa_family 改变(不同)。
if the the address family is AF_INET the first two bytes are port number and the next 4 bytes will be the source ip address.
if the the address family is PF_PACKET the first two bytes tells the Ethernet type(wither 0800--> IP, 0806 --> ARP etc..) and the next 4 bytes (actually the first one is enough) it tells the source interface. if the value is:
- 02 00 00 00 = eth0,
- 03 00 00 00 = eth1,
- 04 00 00 00 = eth2 etc ... and the last 8 bytes are unused in AF_INET family but in the PF_PACKET i didn't find out.
如果地址族是 AF_INET,则前两个字节是端口号,接下来的 4 个字节将是源 IP 地址。
如果地址族是 PF_PACKET,则前两个字节告诉以太网类型(wither 0800--> IP、0806 --> ARP 等),接下来的 4 个字节(实际上第一个就足够了)它告诉源接口. 如果值为:
- 02 00 00 00 = eth0,
- 03 00 00 00 = eth1,
- 04 00 00 00 = eth2 等......最后8个字节在AF_INET系列中未使用,但在PF_PACKET中我没有发现。
回答by user3731622
To get and print the sa_data member
of the struct sockaddr
please refer to John's answer at Getting IPV4 address from a sockaddr structure
要获取和打印的sa_data member
,struct sockaddr
请参阅 John 在从 sockaddr 结构中获取 IPV4 地址的回答