Linux 在 C/C++ 中查找可用的网络接口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8434918/
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
Find available network interfaces in C/C++?
提问by Filipe
Does anyone know a C/C++ code for finding the network interfaces available?
有谁知道用于查找可用网络接口的 C/C++ 代码?
I've been looking for some code, but most times they are quite complex. Is there a simple way to do this?
我一直在寻找一些代码,但大多数时候它们非常复杂。有没有一种简单的方法可以做到这一点?
采纳答案by Duck
See the getifaddrsman page. There is an example program towards the end.
请参阅getifaddrs手册页。最后有一个示例程序。
回答by BRPocock
If you're looking for this in context of a desktop application, and you want to be notified of changes (e.g. interfaces connecting/disconnecting), consider using DBus to monitor NetworkManager.
如果您正在桌面应用程序的上下文中查找此内容,并且希望收到更改通知(例如接口连接/断开连接),请考虑使用 DBus 来监视 NetworkManager。
http://projects.gnome.org/NetworkManager/developers/api/09/spec.html
http://projects.gnome.org/NetworkManager/developers/api/09/spec.html
You can enumerate interfaces, as well as interface-specific things (like available and connected WiFi access points, configured-but-not-dialed PPP links, and so forth), and if anything changes, you'll receive a notification over the DBus.
您可以枚举接口以及特定于接口的内容(例如可用和已连接的 WiFi 接入点、已配置但未拨号的 PPP 链接等),如果有任何更改,您将通过 DBus 收到通知.
(If this is for something more like a server program, where you expect the network configuration to remain more stable, then things like getifaddrs
are possibly more appropriate.)
(如果这更像是服务器程序,您希望网络配置保持更稳定,那么类似getifaddrs
的事情可能更合适。)