When i use the INADDR_ANY to specify the IP address to bind to socket, which later listens on a port, like so:

sockaddr_in sockAddr;

sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(80);
sockAddr.sin_addr.S_un.S_addr = INADDR_ANY; // use default

Will this socket structure allow external, internal, and loopback IP addresses to connect to to me?

I don't want to limit IP addresses that can communicate with me by putting my computer external address(ie: 122.215.214.3) in place of INADDR_ANY because then internal IP's can't connect.

I'm wondering if INADDR_ANY will bind with all 3 of my computer's IP addresses(external,internal,loopback).

有帮助吗?

解决方案

When binding a listening socket, INADDR_ANY allows inbound connections on any local IPv4 address that directly belongs to the machine that the listening socket is running on, which includes loopback addresses. However, you cannot bind to an external IP address that is outside of the machine, such as the public IP of a network router. The router would have to be configured to forward incoming connections from the public IP to a private LAN IP that is assigned to the listening machine that it can bind on.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top