使用unix套接字获取本地IP地址

时间:2014-11-11 12:20:36

标签: c++ sockets unix network-programming

我正在编写一个简单的UDP客户端服务器聊天,以便更熟悉网络编程。现在,让用户更容易看到服务器IP地址是什么(因此用户可以将其键入客户端进行连接)。这就是为什么我希望服务器显示自己的本地IP地址。

这是我的代码:(编辑处理多个IP的代码)

// get the hostname of the server
char hostname[128];
if (gethostname(hostname, sizeof(hostname)) == -1) {
    std::cout << "Could not get the hostname of the server. Error: " << std::strerror(errno) << std::endl;
    return 1;
}

struct addrinfo hints, *serverInfo, *p;

std::memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC; // use AF_INET (IPv4) or AF_INET6 (IPv6) to force version
hints.ai_socktype = SOCK_DGRAM;

// try to get information about our hostname
int rv;
if ((rv = getaddrinfo(hostname, NULL, &hints, &serverInfo)) != 0) {
    std::cout << "Failed to get information about the host \"" << hostname << "\". Error: " << gai_strerror(rv) << std::endl;
    return 1;
}

std::cout << "IP addresses for " << hostname << ":" << std::endl;

// iterate over all the infos we got and try to extract the IP address
for(p = serverInfo; p != NULL; p = p->ai_next) {
    char serverIPAddress[INET6_ADDRSTRLEN];
    void *addr;
    std::string ipVersion;

    // get the pointer to the address itself,
    // different fields in IPv4 and IPv6:
    if (p->ai_family == AF_INET) { // IPv4
        struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
        addr = &(ipv4->sin_addr);
        ipVersion = "IPv4";
    } else { // IPv6
        struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
        addr = &(ipv6->sin6_addr);
        ipVersion = "IPv6";
    }

    // take the IP address of our hostname and convert it into a readable format
    inet_ntop(p->ai_family, addr, serverIPAddress, sizeof(serverIPAddress));
    std::cout << "  " << ipVersion << ": " << serverIPAddress << std::endl;
}

freeaddrinfo(serverInfo); // free the linked list

它可以工作,但它不会显示正确的IP地址。每次我运行它时都会显示一个不同的IP,这不是正确的IP。正如我所说,我仍在学习网络编程,所以我不知道为什么会这样。有人能指出我正确的方向吗?

5 个答案:

答案 0 :(得分:0)

您将无法获得此类“全局”IP地址,只能获取本地IP地址。为了让客户端连接到您,它需要全局IP地址,这可以从Internet上看到,即您的网关(路由器)IP。这是因为您的路由器将计算机的IP地址转换为全局可见的IP地址并将其存储在表中,以便可以将传入连接正确路由到正确的子网。这称为 NAT ing(了解有关 NAT here的更多信息)。

如果您希望客户端连接显式,您需要为其提供路由器的IP地址(根据您的操作系统使用命令行工具)并配置端口转发,路由器将自动转发到达的数据包(IP_router,port_router)到(IP_host,port_host)。这是在路由器的配置面板中完成的。根据您的路由器,这将在菜单中的某处找到。您可以通过键入192.068.0.1192.168.0.0等内容来连接到路由器。

答案 1 :(得分:0)

在Windows上,您可以使用gethostbyname并枚举hostent h_addr_list ...在Linux上,您可以创建一个临时套接字并在ioctl上使用SIOCGIFCONF并通过接口枚举。

答案 2 :(得分:0)

// Get a Vector of the IP addresses of this computer
std::vector<std::string> getIPAddresses() {

    std::vector<std::string> IPAddresses;
    int TempAddress = 0;
    char* TempIPAddress = (char*) malloc(16);

    ifaddrs* ifap = NULL;   //Head of the interface address linked list

    if (getifaddrs(&ifap) == 0 && ifap != NULL) {

        //Get another pointer to move down the linked list
        ifaddrs* current = ifap;

        //Move down the linked list until we get to a NULL pointer
        while (current != NULL) {

            //Create a pointer of the correct type to work with
            const sockaddr_in* interfaceAddress = reinterpret_cast<const sockaddr_in*>(current->ifa_addr);

            if (current->ifa_addr != NULL) {
                if (current->ifa_addr->sa_family == AF_INET) {
                    //printf("%s:", current->ifa_name);
                    if (interfaceAddress != NULL) {
                        TempAddress = ntohl(interfaceAddress->sin_addr.s_addr);
                        sprintf(TempIPAddress, "%d.%d.%d.%d", (TempAddress >> 24) & 0xFF, (TempAddress >> 16) & 0xFF,
                               (TempAddress >> 8) & 0xFF, TempAddress & 0xFF);
                        //Don't include the lookback address
                        if (strcmp(TempIPAddress, "127.0.0.1") != 0) {
                            IPAddresses.push_back(std::string(TempIPAddress));
                        }
                        //printf("%s\n", TempIPAddress);
                    }
                }
            }

            //Move to the next node in the linked-list
            current = current->ifa_next;
        }

        //Release the interface memory
        freeifaddrs(ifap);
        ifap = NULL;
    }

    return IPAddresses;

}

答案 3 :(得分:0)

我认为在代码结束时,您滥用了数据结构sockaddr_in。 这一行:

inet_ntop(AF_INET, &(serverInfo->ai_addr), serverIPAddress, INET_ADDRSTRLEN);

应替换为:

struct sockaddr_in *ipv4 = (struct sockaddr_in *)serverInfo->ai_addr;
void *addr = &(ipv4->sin_addr);
inet_ntop(AF_INET, addr, serverIPAddress, INET_ADDRSTRLEN);

答案 4 :(得分:0)

Soo,感谢我在这里得到的建议,我改变了我的方法,而不是尝试将我的机器的主机名解析为IP地址,我现在简单地列出所有网络接口及其IP地址。

这是我的代码:

// get the hostname of the server
char hostname[128];
if (gethostname(hostname, sizeof(hostname)) == -1) {
    std::cout << "Could not get the hostname of the server. Error: " << std::strerror(errno) << std::endl;
    return 1;
}

std::cout << "IP addresses for " << hostname << ":" << std::endl << std::endl;

struct ifaddrs *networkInterfaceList, *p;

getifaddrs (&networkInterfaceList); // get information about the network interfaces

// iterate over all the network interfaces we got and try to extract their IP address
for (p = networkInterfaceList; p != NULL; p = p->ifa_next) {
    char serverIPAddress[INET6_ADDRSTRLEN];
    void *addr;
    std::string ipVersion;

    // get the pointer to the address itself,
    // different fields in IPv4 and IPv6:
    if (p->ifa_addr->sa_family == AF_INET) { // IPv4
        struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ifa_addr;
        addr = &(ipv4->sin_addr);
        ipVersion = "IPv4";
    } else { // IPv6
        struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ifa_addr;
        addr = &(ipv6->sin6_addr);
        ipVersion = "IPv6";
    }

    // take the IP address of our hostname and convert it into a readable format
    inet_ntop(p->ifa_addr->sa_family, addr, serverIPAddress, sizeof(serverIPAddress));

    // sometime the IP address is empty so only print if there is one
    if (std::strlen(serverIPAddress) > 0)
        std::cout << "Interface: " << std::setw(6) << std::left << p->ifa_name << " " << ipVersion << ": " << serverIPAddress << std::endl;
}

freeifaddrs(networkInterfaceList); // free the linked list

以防万一其他人正在尝试类似的东西。

如果有人对此感兴趣,请访问聊天:https://github.com/Foaly/Chat