#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib"
int main() {
WSADATA wsaData;
// Инициализация библиотеки winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
std::cerr << "Failed to initialize winsock" << std::endl;
return 1;
}
char hostname[255];
if (gethostname(hostname, sizeof(hostname)) == 0) {
std::cout << "Hostname: " << hostname << std::endl;
struct addrinfo hints, *res;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
// Получение информации об адресе
if (getaddrinfo(hostname, NULL, &hints, &res) == 0) {
struct sockaddr_in *addr = (struct sockaddr_in *)res->ai_addr;
std::cout << "IP Address: " << inet_ntoa(addr->sin_addr) << std::endl;
freeaddrinfo(res);
} else {
std::cerr << "Failed to get address info" << std::endl;
}
} else {
std::cerr << "Failed to get hostname" << std::endl;
}
// Освобождение ресурсов winsock
WSACleanup();
return 0;
}