1

How to get the list of active interface in a system using C? what API or library i need to use? and Can i choose a particular interface to send data in socket programming? if yes then please give an example.

jww
  • 97,681
  • 90
  • 411
  • 885
prashant
  • 59
  • 7

3 Answers3

1

If on Linux you might like to take look at:

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.

alk
  • 69,737
  • 10
  • 105
  • 255
  • but how to choose a particular interface to send data.(i.e. eth0/wlan0 etc.) – prashant Aug 12 '13 at 02:54
  • @prashant: Send to the interface's address or to an address routed via the interface. – alk Aug 12 '13 at 06:34
  • can this "getifaddrs()" function able to detect interfaces for 3G/4G_LTE modem? it will be great if you can provide an example with output for the same. thanks! – prashant Aug 12 '13 at 10:41
  • @prashant: My answer links to a page containing an example on how to use `getifaddrs()`. – alk Aug 12 '13 at 10:49
0

You can show all the active network interfaces with ioctl() and its different flags. You can find an exemple here.

You will be able to bind your socket to a specific interface, with setsockopt(). An example can be found here.

Community
  • 1
  • 1
nouney
  • 4,363
  • 19
  • 31
0

On Windows, use GetAdaptersInfo() and/or GetAdaptersAddresses().

On most POSIX-compatible systems, you can use getifaddrs().

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770