简体   繁体   English

如何获取C中活动网络接口的列表?

[英]How to get a list of active network interfaces in C?

How to get the list of active interface in a system using C? 如何使用C获取系统中活动接口的列表? what API or library i need to use? 我需要使用什么API或库? and Can i choose a particular interface to send data in socket programming? 并且可以在套接字编程中选择特定的接口来发送数据吗? if yes then please give an example. 如果是,那么请举一个例子。

If on Linux you might like to take look at: 如果在Linux上,您可能要看一下:

int getifaddrs(struct ifaddrs **ifap);

The getifaddrs() function creates a linked list of structures describing the network interfaces of the local system, and stores the address of the first item of the list in *ifap. getifaddrs()函数创建描述本地系统网络接口的结构的链接列表,并将列表第一项的地址存储在* ifap中。

You can show all the active network interfaces with ioctl() and its different flags. 您可以使用ioctl()及其不同的标志显示所有活动的网络接口。 You can find an exemple here . 你可以在这里找到一个例子。

You will be able to bind your socket to a specific interface, with setsockopt() . 您将可以使用setsockopt()将套接字绑定到特定接口。 An example can be found here . 一个例子可以在这里找到。

On Windows, use GetAdaptersInfo() and/or GetAdaptersAddresses() . 在Windows上,使用GetAdaptersInfo()和/或GetAdaptersAddresses()

On most POSIX-compatible systems, you can use getifaddrs() . 在大多数POSIX兼容系统上,可以使用getifaddrs()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM