#include#include using namespace std; int main() { char hostName[255]; gethostname(hostName, 255); cout << "Hostname: " << hostName << endl; return 0; }
#includeThis code uses the gethostbyname() function from the arpa/inet.h and netdb.h libraries to retrieve the host information, including the name of the computer, and then prints it to the console. Package library: These examples use the C++ standard library and platform-specific libraries such as the Windows API and arpa/inet.h and netdb.h libraries.#include #include using namespace std; int main() { char hostName[256]; gethostname(hostName, 256); struct hostent* host = gethostbyname(hostName); if (host == NULL) { cout << "Failed to get host info" << endl; return 1; } cout << "Hostname: " << host->h_name << endl; return 0; }