In a C project (POSIX), how do I get the fully qualified name for the current system?
For example, I can get just the hostname of my machine by doing
gethostname()
from unistd.h. This might give me machine3
in return, but I'm actually looking for machine3.somedomain.com
for example.
How do I go about getting this information? I do not want to use a call to system() to do this, if possible.
This question is related to
c
sockets
posix
environment
My solution:
#ifdef WIN32
#include <Windows.h>
#include <tchar.h>
#else
#include <unistd.h>
#endif
void GetMachineName(char machineName[150])
{
char Name[150];
int i=0;
#ifdef WIN32
TCHAR infoBuf[150];
DWORD bufCharCount = 150;
memset(Name, 0, 150);
if( GetComputerName( infoBuf, &bufCharCount ) )
{
for(i=0; i<150; i++)
{
Name[i] = infoBuf[i];
}
}
else
{
strcpy(Name, "Unknown_Host_Name");
}
#else
memset(Name, 0, 150);
gethostname(Name, 150);
#endif
strncpy(machineName,Name, 150);
}
The easy way, try uname()
If that does not work, use gethostname() then gethostbyname() and finally gethostbyaddr()
The h_name of hostent{} should be your FQDN
I believe you are looking for:
Just pass it the localhost IP.
There is also a gethostbyname function, that is also usefull.
gethostname()
is POSIX way to get local host name. Check out man
.
BSD function getdomainname()
can give you domain name so you can build fully qualified hostname. There is no POSIX way to get a domain I'm afraid.
I believe you are looking for:
Just pass it the localhost IP.
There is also a gethostbyname function, that is also usefull.
My solution:
#ifdef WIN32
#include <Windows.h>
#include <tchar.h>
#else
#include <unistd.h>
#endif
void GetMachineName(char machineName[150])
{
char Name[150];
int i=0;
#ifdef WIN32
TCHAR infoBuf[150];
DWORD bufCharCount = 150;
memset(Name, 0, 150);
if( GetComputerName( infoBuf, &bufCharCount ) )
{
for(i=0; i<150; i++)
{
Name[i] = infoBuf[i];
}
}
else
{
strcpy(Name, "Unknown_Host_Name");
}
#else
memset(Name, 0, 150);
gethostname(Name, 150);
#endif
strncpy(machineName,Name, 150);
}
gethostname()
is POSIX way to get local host name. Check out man
.
BSD function getdomainname()
can give you domain name so you can build fully qualified hostname. There is no POSIX way to get a domain I'm afraid.
I believe you are looking for:
Just pass it the localhost IP.
There is also a gethostbyname function, that is also usefull.
gethostname()
is POSIX way to get local host name. Check out man
.
BSD function getdomainname()
can give you domain name so you can build fully qualified hostname. There is no POSIX way to get a domain I'm afraid.
I believe you are looking for:
Just pass it the localhost IP.
There is also a gethostbyname function, that is also usefull.
The easy way, try uname()
If that does not work, use gethostname() then gethostbyname() and finally gethostbyaddr()
The h_name of hostent{} should be your FQDN
gethostname()
is POSIX way to get local host name. Check out man
.
BSD function getdomainname()
can give you domain name so you can build fully qualified hostname. There is no POSIX way to get a domain I'm afraid.
Source: Stackoverflow.com