【问题标题】:Gethostbyaddr fails in linux but works with windows machineGethostbyaddr 在 linux 中失败,但适用于 windows 机器
【发布时间】:2013-10-12 07:33:09
【问题描述】:

我正在尝试获取远程系统的主机名,而 gethostbyaddr 失败,错误号为 22,根据 errno.h 是 EINVAL。我正在尝试获取 Windows 系统的主机名及其失败,同样是对于linux系统。但是相同的功能在windows上运行良好。我已经通过thread按照他们的说法,必须存在反向dns记录才能使该功能正常工作。还有其他替代功能来获取远程系统名称吗?我已经发布了下面的代码,请告诉我获得相同的方法。

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>



int main(void)
{

    struct hostent *hp;
    in_addr_t ipaddr;
    char Ipaddr[20];

    printf("\n Enter the ip address : ");
    scanf("%s",Ipaddr);

    ipaddr=inet_addr(Ipaddr);

    printf("Converted ip address : %zu",ipaddr);

    printf("\n Hostname is : %s",hname);   
    hp=gethostbyaddr((void *)&ipaddr,4,AF_INET);
    if(hp==NULL)
    {
        printf("\n The hostname could not be found ");
        perror("gethostbyaddr"); // error printed as "gethostbyaddr: Success"
        printf("Error Number : %d",errno); //error number is : 22 which is EINVAL as per the header file

        return 0;
    }
    printf("\n The hostname by gethostbyaddr : %s",hp->h_name);

}

编辑

#include <stdio.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>

int main(int argc, char **argv)
{
    int dwRetval;

    struct sockaddr_in saGNI;
    char hostname[NI_MAXHOST];
    char servInfo[NI_MAXSERV];
    u_short port = 27015;

    char Ip_Address[18];

    // Validate the parameters

    printf("enter the ip address : ");
    scanf("%s",Ip_Address);


    //-----------------------------------------
    // Set up sockaddr_in structure which is passed
    // to the getnameinfo function
    saGNI.sin_family = AF_INET;
    saGNI.sin_addr.s_addr = inet_addr(Ip_Address);
    saGNI.sin_port = htons(port);

    //-----------------------------------------
    // Call getnameinfo
    dwRetval = getnameinfo((struct sockaddr *) &saGNI,
                           sizeof (struct sockaddr),
                           hostname,
                           NI_MAXHOST, servInfo, NI_MAXSERV,NI_NOFQDN);

    if (dwRetval != 0) {
        perror("getnameinfo");
        printf("getnameinfo returned herror = %d\n", errno);

        return 1;
    } else {
        printf("getnameinfo returned hostname = %s\n", hostname);
        return 0;
    }
}

【问题讨论】:

    标签: c sockets


    【解决方案1】:

    解析代码好像没问题,但是报错部分错了。 至少在 Unix 上,gethostbyaddr()not 设置了errno。在出错的情况下, h_errno 已设置,herror() 可用于打印文本错误消息:

    if (hp == NULL) {
        herror("Could not resolve address");
        printf("Error Number : %d", h_errno);
    
        return 0;
    }
    

    备注: 将 IP 地址转换为主机名的更现代的接口是 getnameinfo()。它适用于 IPv4 和 IPv6,并且应该在 Unix 和 窗户。

    【讨论】:

    • 您好,我将主机名作为机器的 IP 地址。
    • @blitz:我现在很困惑。您的问题似乎暗示hp == NULL,因此我专注于那部分。 gethostbyaddr() 是否成功?
    • 我用 getnameinfo 编写了代码,其标志为 NI_NOFQDN 和 NI_NUMERICSERV 但仍然相同,它返回了 IP 地址
    • @blitz:也许我误解了你的问题。您说 Linux 上的(原始)代码 failsif(hp==NULL) ... 部分已执行。现在你说函数成功了,但返回主机名作为数字 IP 地址。
    • 根据您的建议,我尝试使用 getnameinfo ,该函数返回一个 ipaddress 但不是主机名。抱歉英语不好。
    猜你喜欢
    • 2020-07-25
    • 2012-07-17
    • 2012-08-02
    • 2017-12-14
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多