3

The IP address is 192.168.23.4. I am able to get the hostname from the ipaddress using the following code snippet:

struct sockaddr_in sa;
char str[INET_ADDRSTRLEN];
inet_pton(AF_INET, "192.168.23.4", &(sa.sin_addr));

struct sockaddr_in saGNI;
char hostname[NI_MAXHOST];
char servInfo[NI_MAXSERV];
u_short port = 27015;
saGNI.sin_family = AF_INET;
saGNI.sin_addr.s_addr = sa.sin_addr.s_addr;
saGNI.sin_port = htons(port);

DWORD dwRetval = getnameinfo((struct sockaddr *) &saGNI,
    sizeof(struct sockaddr),
    hostname,
    NI_MAXHOST, servInfo, NI_MAXSERV, NI_NUMERICSERV);
printf("HostName: %s", hostname);

I am getting an output of the form

ComputerName.domain.com

How do I get the Computername from the hostname?

Eg Input

ComputerName.domain.com

Eg Output

ComputerName

Is there any way to directly get the ComputerName of a system whose IP address is known?

I am looking for the same result as displayed using the Hostname command on the remote system.

jww
  • 97,681
  • 90
  • 411
  • 885
  • Please place answers in Answer blocks. Later, you can accept your own Answer. Also see [How does accepting an answer work?](https://meta.stackexchange.com/q/5234/173448) – jww Nov 15 '19 at 09:08

4 Answers4

4

Check the manual pages for getnameinfo

http://man7.org/linux/man-pages/man3/getnameinfo.3.html

According to the manual pages, you should set the NI_NOFQDN flags.

 NI_NOFQDN
          If set, return only the hostname part of the fully qualified
          domain name for local hosts.
emirc
  • 1,948
  • 1
  • 23
  • 38
  • 1
    Also valid for winsock, just in case: https://learn.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-getnameinfo – parktomatomi Nov 15 '19 at 08:43
2

As suggested by emirc, the following code is printing Computername:

struct sockaddr_in sa;
char str[INET_ADDRSTRLEN];
inet_pton(AF_INET, "192.168.23.4", &(sa.sin_addr));

struct sockaddr_in saGNI;
char hostname[NI_MAXHOST];
char servInfo[NI_MAXSERV];
u_short port = 27015;
saGNI.sin_family = AF_INET;
saGNI.sin_addr.s_addr = sa.sin_addr.s_addr;
saGNI.sin_port = htons(port);

DWORD dwRetval = getnameinfo((struct sockaddr *) &saGNI,
    sizeof(struct sockaddr),
    hostname,
    NI_MAXHOST, servInfo, NI_MAXSERV, NI_NOFQDN);
printf("HostName: %s", hostname);

Note: I have changed the flag from NI_NUMERICSERV

to

NI_NOFQDN

0

You mean, you want to truncate the string before the first period?

std::string host(hostname);
size_t pos = host.find('.');
if (pos != std::string::npos)
{
   host = host.substr(0,pos);
}
strcpy(hostname, host.c_str());
selbie
  • 100,020
  • 15
  • 103
  • 173
-1

If you are interested in finding the host name of the computer on which the code is running, Boost.Asio could simplify the task:

#include <iostream>
#include <string>
#include <boost/asio.hpp>

int main() {
  std::string hostname = boost::asio::ip::host_name();
  std::cout << "hostname = " << hostname << std::endl;
}

Note that this needs to be compiled with the -lpthread option.

RHertel
  • 23,412
  • 5
  • 38
  • 64