Obtaining a server IP address from hostname

后端 未结 3 1323
广开言路
广开言路 2021-01-01 04:01

When performing an NSURLRequest to a hostname, is it possible to obtain the IP address of the server that the response came from?

The NSURL

3条回答
  •  自闭症患者
    2021-01-01 04:55

    You can use the system call gethostbyname() to resolve a hostname then use the returning structure to get the ip address. Have a look at inet_ntop() for this last part.

    EXAMPLE CODE

    struct hostent *hostentry;
    hostentry = gethostbyname("google.com");
    char * ipbuf;
    ipbuf = inet_ntoa(*((struct in_addr *)hostentry->h_addr_list[0]));
    printf("%s",ipbuf);
    

提交回复
热议问题