c++ - in_addr 结构(IPv4 地址)的 Mac/Windows 不同格式

标签 c++ windows macos ip-address

我正在将一些 Windows 代码移植到 Mac。我遇到过一些使用 in_addr 类型的代码,并在此处找到了 Windows 实现的文档:http://msdn.microsoft.com/en-us/library/windows/desktop/ms738571%28v=vs.85%29.aspx

具体来说,我遇到的问题是代码试图访问 in_addr 的 S_un 属性,编译器 (CLang) 提示该属性不存在。查看 Mac 实现的定义,我得到了这个(在 usr/include/netinet/in.h 中):

struct in_addr {
    in_addr_t s_addr;
};

...和in_addr_t定义如下(在usr/include/sys/types.h中):

typedef __uint32_t      in_addr_t;

有没有人知道一个好的解决方法,或者 Mac 上是否有与 Windows 实现更紧密相关的 IPv4 地址表示?

最佳答案

请看我的评论。而且...您可以在代码中实现 windows 声明(只是不要将其称为 in_addr 以避免冲突)。你可以这样做:

 typedef struct Win_in_addr {
   union {
     struct {
       __uint8_t s_b1,s_b2,s_b3,s_b4;
     } S_un_b;
     struct {
       __uint16_t s_w1,s_w2;
     } S_un_w;
     __uint32_t S_addr;
   } S_un;
 } Win_IN_ADDR, *Win_PIN_ADDR, *Win_LPIN_ADDR;

但是请注意,这不是可移植的,只能在小端 CPU 上按预期工作。

关于c++ - in_addr 结构(IPv4 地址)的 Mac/Windows 不同格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12423244/

相关文章:

c++ - 位集超过 32 位?

Windows 批处理 : for and findstr to include and exclude from results

c++ - 我如何检测我的应用程序何时最小化?

macos - 运行桌面并行 mac 的 Visual Studio 中的花括号

python - pyobjc-core 安装期间出现 xcode-select 错误

c++ - 在 c 中键入不存在的结构

C++ 返回空指针

linux - GTK 文件打开对话框样式

macos - 如何在苹果脚本中制作对话框等待用户输入

C++ : User Input of Names and Scores, 未知数量的名字,比较分数