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.

1
  • Please place answers in Answer blocks. Later, you can accept your own Answer. Also see How does accepting an answer work? Commented Nov 15, 2019 at 9:08

4 Answers 4

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.
Sign up to request clarification or add additional context in comments.

1 Comment

Also valid for winsock, just in case: learn.microsoft.com/en-us/windows/win32/api/ws2tcpip/…
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

Comments

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());

1 Comment

one-liner: std::getline(std::stringstream(hostname), host, '.');
-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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.