【问题标题】:Equivalent inet_addr for interface name (eth0, etc.) C/C++, Linux [duplicate]接口名称(eth0等)的等效inet_addr C/C++,Linux [重复]
【发布时间】:2012-02-28 01:22:18
【问题描述】:

可能重复:
Get IP address of an interface on linux

我有如下代码:

const char* interface = "10.123.123.123";
struct ip_mreq mc_req;    
mc_req.imr_interface.s_addr = inet_addr(interface);

其中interface是我要使用的接口的inet地址。

我怎样才能通过name来引用接口?

例如,我想写这样的东西:

const char* interface = "eth0";
struct ip_mreq mc_req;    
mc_req.imr_interface.s_addr = inet_addr(interface);  // ???

但这不起作用。是否有类似 inet_addr 的函数,我可以在其中查找引用“eth0”的 inet 地址?

【问题讨论】:

  • 注意:SO 上的搜索框是你的朋友.. ;)

标签: c++ c linux networking


【解决方案1】:

getifaddrs() 将完成这项工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多