简体   繁体   中英

what is interface in socket programming?

"INADDR_ANY binds the socket to all available interfaces."

This is the statement i Encountered. I found it here

What is interface here? Is it a port number or something else?

And another question is

Is interface and channel or one and same?

Usually your host (your computer) has more than one interfaces. For example, (older) computer without network would have only IPv4 loopback interface.

If you add and configure IPv4 network to that PC, you'll get another interface: eth0 , or net0 or something similar.

When you install VPN, it will create you yet another interface, as instead of sending packets into unsecured network, you send it into logical VPN interface, and that one forwards data to eth0 after some processing.

Every time, when you add a hardware link (with driver) to a network, or create logical network, it creates you a new interface. For example, if you use VMVare, and create virtual machine, the system would provider you some set of interfaces needed to route data between your host, network, and virtual machine.

When routing IPv4, every interface is assigned IPv4 address. Even loopback (127.0.0.1). The address can be static, or obtained from server when your system boots.

So you can listen only on one interface. For example, if you bind to loopback, you will not be able to access any network, and network hosts will not be able to access your socket (assuming routing is not broken). But you connect multiple processes on your host to each other.

If you bind to particular network interface, it means you want to work with systems, that are connected to that network (directly or indirectly).

If you bind to any , for server sockets it means you let system to accept connections from anywhere , considering that anywhere can ping you.

As per my understanding the socket interface is something like this

  1. Gives a file system like abstraction to the capabilities of the network.
  2. Each transport protocol offers a set of services. The socket API provides the abstraction to access these services
  3. The API defines function calls to create, close, read and write to/from a socket.

Also something like this also

A network interface is the point of interconnection between a computer and a private or public network. A network interface is generally a network interface card (NIC), but does not have to have a physical form. Instead, the network interface can be implemented in software.

For example, the loopback interface (127.0.0.1 for IPv4 and ::1 for IPv6) is not a physical device but a piece of software simulating a network interface. The loopback interface is commonly used in test environments

Examples for interfaces:

  • your LAN card where you can plug a network cable,
  • a wifi adapter,
  • a (software-only) thing which provides an imaginary network between your main system and a virtual machine
  • the (software-only) loopback adapter which sends everything you send to it "back" to your own computer

etc. If you´re writing a socket server, you can choose
where the client connections may come from.
Only from a virtual machine, but no real computer outside?
Only wifi, but no cable-bound LAN? Or just all together?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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