Resolve host name to an ip address
I developed a client/server simulation application. I deployed client and server on two different Windows XP machines. Somehow, the client is not able to send requests to the server.
I tried below options:
Pinged server machine successfully from client using ip-address.
Pinged client machine successfully from server using ip-address.
Checked netstat command line tool from both machines. Server is in LISTENING mode and client is in SYS_SENT mode. But the foreign address it is using to send is host name not the ip address.
Pinged server machine unsuccessfully using host name from client.
Pinged client machine successfully using host name from server.
I feel the problem is when the client is trying to connect to the server using the host name.
Could you please let me know how to force an application to use an ip address instead of a host name? Is there any other way to map the host name to an ip address?
5 Answers 5
Go to your client machine and type in:
substituting the real host name of your server for server.company.com , of course.
That should tell you which DNS server your client is using (if any) and what it thinks the problem is with the name.
To force an application to use an IP address, generally you just configure it to use the IP address instead of a host name. If the host name is hard-coded, or the application insists on using a host name in preference to an IP address (as one of your other comments seems to indicate), then you’re probably out of luck there.
However, you can change the way that most machine resolve the host names, such as with /etc/resolv.conf and /etc/hosts on UNIXy systems and a local hosts file on Windows-y systems.
Try tracert to resolve the hostname. IE you have Ip address 8.8.8.8 so you would use; tracert 8.8.8.8
You could use a C function getaddrinfo() to get the numerical address — both ipv4 and ipv6. See the example code here
This is hard to answer without more detail about the network architecture. Some things to investigate are:
- Is it possible that client and/or server is behind a NAT device, a firewall, or similar?
- Is any of the IP addresses involved a «local» address, like 192.168.x.y or 10.x.y.z?
- What are the host names, are they «real» DNS:able names or something more local and/or Windows-specific?
- How does the client look up the server? There must be a place in code or config data that holds the host name, simply try using the IP there instead if you want to avoid the lookup.
Windows XP has the Windows Firewall which can interfere with network traffic if not configured properly. You can turn off the Windows Firewall, if you have administrator privileges, by accessing the Windows Firewall applet through the Control Panel. If your application works with the Windows Firewall turned off then the problem is probably due to the settings of the firewall.
We have an application which runs on multiple PCs communicating using UDP/IP and we have been doing experiments so that the application can run on a PC with a user who does not have administrator privileges. In order for our application to communicate between multiple PCs we have had to use an administrator account to modify the Windows Firewall settings.
In our application, one PC is designated as the server and the others are clients in a server/client group and there may be several groups on the same subnet.
The first change was to use the functionality of the Exceptions tab of the Windows Firewall applet to create an exception for the port that we use for communication.
We are using host name lookup so that the clients can locate their assigned server by using the computer name which is composed of a mnemonic prefix with a dash followed by an assigned terminal number (for instance SERVER100-1). This allows several servers with their assigned clients to coexist on the same subnet. The client uses its prefix to generate the computer name for the assigned server and to then use host name lookup to discover the IP address of the assigned server.
What we found is that the host name lookup using the computer name (assigned through the Computer Name tab of the System Properties dialog) would not work unless the server PC’s Windows Firewall had the File and Printer Sharing Service port enabled.
So we had to make two changes: (1) setup an exception for the port we used for communication and (2) enable File and Printer Service in the Exceptions tab to allow for the host name lookup.
Resolve host name from IP address
I’m looking for a command line tool which gets an IP address and returns the host name, for Windows.
8 Answers 8
The command you are looking for is called nslookup , works fine for reverse lookups IFF someone has configured a reverse zone file, which they don’t always do.
if all the above fails, and you are specifically looking for a Windows machine, you can use
The data returned will be all the NetBIOS records the machine has. The one with a record type will usually be the machine’s name.
For many IP addresses you could just use ping -a, for example
If you use nslookup command with the IP address as its first argument will return the PTR record (the reverse entry) if it exists. For example:
Use dig. A Windows port is available from the ISC here (look in the immediate download box for the link to the zip file). Here’s their man page reference for dig.
Ward’s point about the reverse lookup records often not getting created is very much true. Reverse lookups often do fail because many admins don’t bother creating the ptr records.
(tested under Windows 10 x64)
From command line:
FOR /F «tokens=2 delims= » %A in (‘2^>NUL NSLOOKUP «%IP_ADDRESS%» ^| FINDSTR /C:»: «‘) do ECHO %A
Within a script:
FOR /F «tokens=2 delims= » %%A in (‘2^>NUL NSLOOKUP «%IP_ADDRESS%» ^| FINDSTR /C:»: «‘) do ECHO %%A
Two (side)notes:
- To supress NSLOOKUP errors you have to use 2^>NUL instead of 1^>NUL
- I’ve used FINDSTR /C to extract the value after the four whitespace characters. As the four spaces only seem to exist for the Name: entry, this appears to be only way to make it work on other localized systems.
How to get Networked Computer Name from IP Address on a LAN
If you have the local IP address of a computer on your network, and need to get that computer’s name, there is an easy method using the ping command in a Windows command prompt.
Command Prompt
Open up a command prompt by typing «cmd» into the start menu search (Windows Vista, 7, or newer) or by opening a Run window and then running «cmd» (Windows XP).
Use the following command to ping the local IP address (change xxx.xxx.xxx.xxx to the IP address you want to ping):
ping -a xxx.xxx.xxx.xxx
The -a option of the ping command tells it to resolve the hostname of the IP address, so it will give you the name of the networked computer.
Here is an example of the output for a hypothetical computer named «Office1» that is at local IP address 192.168.1.123:
C:\Users\Username>ping -a 192.168.1.123
Pinging Office1 [192.168.1.123] with 32 bytes of data:
Reply from 192.168.1.123: bytes=32 time<1ms ttl=128
Reply from 192.168.1.123: bytes=32 time<1ms ttl=128
Reply from 192.168.1.123: bytes=32 time<1ms ttl=128
Reply from 192.168.1.123: bytes=32 time<1ms ttl=128
Ping statistics for 192.168.1.123:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms