Sockets getsockname()在连接()到公共IP后显示127.0.0.1

Sockets getsockname()在连接()到公共IP后显示127.0.0.1,sockets,loopback,winsock2,Sockets,Loopback,Winsock2,我正在努力理解这一点。问题出现在以下机器上: 远程(我没有访问权限) 运行Windows7 没有运行代理或VPN(或者我被告知) 我的应用程序有一段代码,试图快速确定操作系统喜欢哪个接口。它做了以下工作: // Create a socket SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); // Resolve DNS query DWORD dwRemoteIp = GetHostAddr("www.google.com")

我正在努力理解这一点。问题出现在以下机器上:

  • 远程(我没有访问权限)
  • 运行Windows7
  • 没有运行代理或VPN(或者我被告知)
我的应用程序有一段代码,试图快速确定操作系统喜欢哪个接口。它做了以下工作:

// Create a socket
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);

// Resolve DNS query
DWORD dwRemoteIp = GetHostAddr("www.google.com")
// I've simplified the call here, but "www.google.com" resolves to 172.217.3.100 in host byte order, so the resolution is correct

// Create the remote address to connect to
sockaddr_in remoteaddr = {0};
remoteaddr.sin_family = AF_INET;
remoteaddr.sin_addr.s_addr = htonl(dwRemoteIp);
remoteaddr.sin_port = htons(80);

// Connect the socket
if (0 == connect(sock, (struct sockaddr*)&remoteaddr, sizeof(remoteaddr))) {

    // The connection succeeded -- see which local address was bound to
    sockaddr_in localaddr = {0};
    int len = sizeof(localaddr);
    if (0 == getsockname(sock, (struct sockaddr*)&localaddr, (socklen_t *)&len)) {

        // Here is where I see dwLocalIp == 0x7F000001, or 127.0.0.1
        DWORD dwLocalIp = ntohl(localaddr.sin_addr.s_addr);
    }
}
这到底是怎么回事