1

I have a multi-homed Windows 10 and I want my application to open a Server Socket on a specific IP given by its hostname.

My machine hostname is MY-PC and it has a public IP 192.0.0.2/24 and a "direct" IP 192.2.2.2/24 (directly connected to another computer). Both are configured in the Windows Network Adapter page in the Control Panel.

No DNS is configured. My hosts file looks like:

127.0.0.1    localhost
192.2.2.2    MY-PC
192.2.2.1    DIRECT-PC

When I try to ping my hostname (after fully disabling IPv6):

> ping MY-PC

Pinging MY-PC [192.0.0.2] with 32 bytes of data:
Reply from 192.0.0.2: time<1ms
...

A strange thing is that the same setup (hosts file/network adapter configuration) works as expected on another computer (but just one out of six).

How can I force listening on a specific IP from a hostname? (which is a global parameter from a configuration received by the program).

I have already checked a related question but the answer is about fully disabling IPv6.

Matthieu
  • 121
  • 9

1 Answers1

1

Please read this article, there you could find more about name resolution in Windows. Below answer to your question:

Host name resolution generally uses the following sequence:

  1. The client checks to see if the name queried is its own.
  2. The client then searches a local Hosts file, a list of IP address and names stored on the local computer.
  3. Domain Name System (DNS) servers are queried.
  4. If the name is still not resolved, NetBIOS name resolution sequence is used as a backup. This order can be changed by configuring the NetBIOS node type of the client.

The Windows client will try each of these methods until it either successfully resolves the name or exhausts these methods.

Alexander Tolkachev
  • 4,608
  • 3
  • 14
  • 23
  • Thanks. So it's clearly #1. So how can I tell which IP to use for my hostname? (looks like NetBIOS?) – Matthieu Feb 25 '20 at 14:35
  • 1
    @Matthieu How I can understand, it's impossible. But you could add to hosts file entry `192.2.2.2 MY-PC-DIRECT` and bind your application on this hostname. – Alexander Tolkachev Feb 25 '20 at 14:42
  • Thanks again Alexander, that's what I'm doing right now but I wanted to make sure there was no other way than creating a second hostname just for that purpose. Ah, Windows, dear Windows... – Matthieu Feb 25 '20 at 14:47