{"id":2150,"date":"2025-06-25T15:23:11","date_gmt":"2025-06-25T07:23:11","guid":{"rendered":"https:\/\/www.madbull.site\/?p=2150"},"modified":"2025-06-25T15:27:43","modified_gmt":"2025-06-25T07:27:43","slug":"c%e8%af%ad%e8%a8%80%e4%b8%ad%e8%8e%b7%e5%8f%96%e4%b8%bb%e6%9c%ba%e6%88%96%e8%80%85%e5%9f%9f%e5%90%8d%e7%9a%84ip%e5%9c%b0%e5%9d%80","status":"publish","type":"post","link":"https:\/\/www.madbull.site\/?p=2150","title":{"rendered":"C\u8bed\u8a00\u4e2d\u83b7\u53d6\u4e3b\u673a\u6216\u8005\u57df\u540d\u7684IP\u5730\u5740"},"content":{"rendered":"\n<p class=\"has-x-large-font-size\"><strong>\u95ee\u9898<\/strong><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u6700\u8fd1\u6709\u4e2a\u4efb\u52a1\uff0c\u9700\u8981\u628a\u516c\u53f8\u7684\u5404\u4e2a\u68c0\u6d4b\u5f15\u64ce\u505a\u6210docker\u865a\u62df\u673a\uff0c\u4f7f\u7528docker-compose\u7ba1\u7406\uff0c\u5728\u5404\u4e2a\u865a\u62df\u673a\u4e4b\u95f4\u4f7f\u7528\u6865\u6a21\u5f0f\u642d\u5efa\u865a\u62df\u7f51\u7edc\uff0c\u5404\u4e2a\u68c0\u6d4b\u5f15\u64ce\u901a\u8fc7\u865a\u62df\u7f51\u7edc\u901a\u8baf\u3002\u5177\u4f53\u642d\u5efa\u8fc7\u7a0b\u4ee5\u540e\u7ed9\u5927\u5bb6\u5206\u4eab\uff0c\u4e0d\u662f\u8fd9\u7bc7\u6587\u7ae0\u7684\u91cd\u70b9\u3002<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><strong>\u540e\u6765\u53d1\u73b0\u4e00\u4e2a\u95ee\u9898\uff1a<\/strong><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u4e4b\u524d\u5404\u670d\u52a1\u7684\u7aef\u53e3\u90fd\u662f\u76d1\u542c\u5728\u672c\u5730\uff08127.0.0.1\uff09\u7684\uff0c\u800c\u5728\u670d\u52a1\u914d\u7f6e\u4e4b\u95f4\u901a\u8baf\u6709\u4e9b\u76f4\u63a5\u4f7f\u7528\u7684TCP\u5957\u63a5\u5b57\uff08socket\u901a\u8baf\uff09\uff0c\u6240\u4ee5\u5728\u5ba2\u6237\u7aef\u76f4\u63a5\u4f7f\u7528 connect 127.0.0.1 \u8fd9\u4e2a\u5730\u5740\u5c31\u53ef\u4ee5\u4e86\u3002\u4f46\u662f\u73b0\u5728\u9700\u8981\u5c06\u5404\u4e2a\u68c0\u6d4b\u5f15\u64ce <strong>\u901a\u8fc7\u865a\u62df\u7f51\u7edc\u901a\u8baf<\/strong>\uff0c\u800c\u5404\u4e2a\u68c0\u6d4b\u5f15\u64ce\u865a\u62df\u673a\u7684 <strong>IP\u5730\u5740\u662f\u672a\u77e5\u7684\uff0c\u800c\u5404\u4e2a\u68c0\u6d4b\u5f15\u64ce\u865a\u62df\u673a\u7684\u4e3b\u673a\u540d\u662f\u5df2\u77e5\u7684<\/strong>\uff0c\u56e0\u4e3a\u4e3b\u673a\u540d\u53ef\u4ee5\u901a\u8fc7 docker-compose \u914d\u7f6e\u6765\u8bbe\u7f6e\u56fa\u5b9a\u4e3b\u673a\u540d\u3002<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><strong>\u6240\u4ee5\uff1a<\/strong><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u6211\u5c31\u53bb\u4fee\u6539C\u4ee3\u7801\uff0c\u589e\u52a0\u4e3b\u673a\u540d\u6216\u8005\u57df\u540d\u89e3\u6790\u6210IP\u5730\u5740\u7684\u65b9\u6cd5\uff0c\u987a\u4fbf\u6574\u7406\u4e86\u7ed9\u5927\u5bb6\u5206\u4eab\u4e00\u4e0b\u3002\u4ee5\u540e\u7528\u7740\u4e86\uff0c\u76f4\u63a5copy\u5c31\u80fd\u7528\u4e86\u3002<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-x-large-font-size\"><strong>talk is cheap, show me the code<\/strong>!!<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ getaddrinfo.c\n\/\/ gcc -Wall getaddrinfo.c -o test \n\n#include &lt;stdio.h>\n#include &lt;stdlib.h>\n#include &lt;string.h>\n#include &lt;sys\/types.h>\n#include &lt;sys\/socket.h>\n#include &lt;netdb.h>\n#include &lt;arpa\/inet.h>\n\n\/**\n * @brief \u7ed9\u5b9a\u4e00\u4e2a\u4e3b\u673a\u540d\u6216\u8005\u57df\u540d\uff0c\u628a\u4e3b\u673a\u540d\u80fd\u89e3\u6790\u5230\u7684IPv4\u548cIPv6\u7ed9\u51fa\u6765\n *\n * @param hostname \u4e3b\u673a\u540d\/\u57df\u540d\n * @param (* ip_list \u89e3\u6790\u7ed3\u679c\uff0c\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u5185\u90e8\u6709 \u7533\u8bf7\u7a7a\u95f4\uff0c\u5982\u679c\u4e0d\u4e3a\u7a7a\uff0c\u9700\u8981free\n *\n * @return \u8fd4\u56de\u89e3\u6790\u5230\u7684IP\u5730\u5740\u7684\u6570\u91cf\n *\/\nint get_ip_by_hostname( char *hostname, char (* (* ip_list) )&#91;INET6_ADDRSTRLEN]) {\n    \n    struct addrinfo hints; \/\/ \u8bbe\u7f6e\u67e5\u8be2\u6761\u4ef6\n    struct addrinfo *res;  \/\/ \u6307\u5411\u7ed3\u679c\u94fe\u8868\u7684\u6307\u9488\n    int status;\n    int count = 0 ;        \/\/ \u89e3\u6790\u5230\u7684 ip \u6570\u91cf\n    char ipstr&#91;INET6_ADDRSTRLEN] ;\n    char (*ip_rslt)&#91;INET6_ADDRSTRLEN] = NULL ; \n\n    \/\/ \u8bbe\u7f6e\u67e5\u8be2\u6761\u4ef6\n    memset(&amp;hints, 0, sizeof(hints));\n    hints.ai_family = AF_UNSPEC;     \/\/ \u652f\u6301 IPv4 \u548c IPv6\n    hints.ai_socktype = SOCK_STREAM; \/\/ TCP \u6d41\u5f0f\u5957\u63a5\u5b57\n\n    \/\/ \u67e5\u8be2 host \u3001 DNS \u7b49\n    if ((status = getaddrinfo(hostname, NULL, &amp;hints, &amp;res)) != 0) {\n        fprintf(stderr, \"getaddrinfo error: %s\\n\", gai_strerror(status));\n        return -1 ; \n    }\n\n    struct addrinfo *p;\n    for (p = res; p != NULL; p = p->ai_next) {\n        void *addr;\n\n        \/\/ \u83b7\u53d6\u5730\u5740\u7ed3\u6784\n        if (p->ai_family == AF_INET) { \/\/ IPv4\n            struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;\n            addr = &amp;(ipv4->sin_addr);\n        } else { \/\/ IPv6\n            struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;\n            addr = &amp;(ipv6->sin6_addr);\n        }\n        ++count ;\n\n        \/\/ \u5c06\u7f51\u7edc\u5730\u5740\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\n        inet_ntop(p->ai_family, addr, ipstr, sizeof(ipstr));\n\n        \/\/ \u7533\u8bf7\u7a7a\u95f4\uff0c\u89e3\u6790\u6570\u636e\n        ip_rslt = (char (*) &#91;INET6_ADDRSTRLEN]) realloc (ip_rslt, count* INET6_ADDRSTRLEN) ;\n        snprintf(ip_rslt&#91;count-1], INET6_ADDRSTRLEN, \"%s\", ipstr ) ;\n    }\n    *ip_list = ip_rslt ;\n\n    \/\/ \u91ca\u653e\u8d44\u6e90\n    freeaddrinfo(res);\n\n    return count;\n\n}\n\n\n\/**\n * @brief \u6d4b\u8bd5\n *\n * @return \n *\/\nint main() {\n    int count = 0 ;\n    char (*ip_rslt)&#91;INET6_ADDRSTRLEN] = NULL ; \n\n    \/\/ \u6d4b\u8bd5\u4e00\n    char * hostname = \"node9\" ;\n    count = get_ip_by_hostname(hostname, &amp;ip_rslt) ;\n    printf(\"IP addresses for %s:\\n\", hostname);\n    for( int i = 0  ; i &lt; count ; ++i ) {\n        printf(\"  %d: %s\\n\", i+1, ip_rslt&#91;i]);\n\n    }\n    if(ip_rslt) free(ip_rslt) ;\n    ip_rslt = NULL ;\n\n    \/\/ \u6d4b\u8bd5\u4e8c\n    hostname = \"localhost\" ;\n    count = get_ip_by_hostname(hostname, &amp;ip_rslt) ;\n    printf(\"IP addresses for %s:\\n\", hostname);\n    for( int i = 0  ; i &lt; count ; ++i ) {\n        printf(\"  %d: %s\\n\", i+1, ip_rslt&#91;i]);\n\n    }\n    if(ip_rslt) free(ip_rslt) ;\n    ip_rslt = NULL ;\n\n    \/\/ \u6d4b\u8bd5\u4e09\n    hostname = \"www.163.com\" ;\n    count = get_ip_by_hostname(hostname, &amp;ip_rslt) ;\n    printf(\"IP addresses for %s:\\n\", hostname);\n    for( int i = 0  ; i &lt; count ; ++i ) {\n        printf(\"  %d: %s\\n\", i+1, ip_rslt&#91;i]);\n\n    }\n    if(ip_rslt) free(ip_rslt) ;\n    ip_rslt = NULL ;\n}\n<\/code><\/pre>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-x-large-font-size\"><strong>\u6d4b\u8bd5<\/strong><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u5176\u4e2d \u4e3b\u673a node9 \u662f\u5728 \/etc\/hosts \u4e2d\u914d\u7f6e\u7684\u3002<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"446\" height=\"358\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2025\/06\/22.png\" alt=\"\" class=\"wp-image-2152\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u6709\u4e2a\u4efb\u52a1\uff0c\u9700\u8981\u628a\u516c\u53f8\u7684\u5404\u4e2a\u68c0\u6d4b\u5f15\u64ce\u505a\u6210docker\u865a\u62df\u673a\uff0c\u4f7f\u7528docker-compose\u7ba1\u7406\uff0c\u5728\u5404\u4e2a\u865a\u62df\u673a\u4e4b\u95f4\u4f7f\u7528\u6865\u6a21\u5f0f\u642d\u5efa\u865a\u62df\u7f51\u7edc\uff0c\u5404\u4e2a\u68c0\u6d4b\u5f15\u64ce\u901a\u8fc7\u865a\u62df\u7f51\u7edc\u901a\u8baf\u3002\u5177\u4f53\u642d\u5efa\u8fc7\u7a0b\u4ee5\u540e\u7ed9\u5927\u5bb6\u5206\u4eab\uff0c\u4e0d\u662f\u8fd9\u7bc7\u6587\u7ae0\u7684\u91cd\u70b9\u3002<br \/>\n\u540e\u6765\u53d1\u73b0\u4e00\u4e2a\u95ee\u9898\uff1a<br \/>\n\u4e4b\u524d\u5404\u670d\u52a1\u7684\u7aef\u53e3\u90fd\u662f\u76d1\u542c\u5728\u672c\u5730\uff08127.0.0.1\uff09\u7684\uff0c\u800c\u5728\u670d\u52a1\u914d\u7f6e\u4e4b\u95f4\u901a\u8baf\u6709\u4e9b\u76f4\u63a5\u4f7f\u7528\u7684TCP\u5957\u63a5\u5b57\uff08socket\u901a\u8baf\uff09\uff0c\u6240\u4ee5\u5728\u5ba2\u6237\u7aef\u76f4\u63a5\u4f7f\u7528 connect 127.0.0.1 \u8fd9\u4e2a\u5730\u5740\u5c31\u53ef\u4ee5\u4e86\u3002\u4f46\u662f\u73b0\u5728\u9700\u8981\u5c06\u5404\u4e2a\u68c0\u6d4b\u5f15\u64ce \u901a\u8fc7\u865a\u62df\u7f51\u7edc\u901a\u8baf\uff0c\u800c\u5404\u4e2a\u68c0\u6d4b\u5f15\u64ce\u865a\u62df\u673a\u7684 IP\u5730\u5740\u662f\u672a\u77e5\u7684\uff0c\u800c\u5404\u4e2a\u68c0\u6d4b\u5f15\u64ce\u865a\u62df\u673a\u7684\u4e3b\u673a\u540d\u662f\u5df2\u77e5\u7684\uff0c\u56e0\u4e3a\u4e3b\u673a\u540d\u53ef\u4ee5\u901a\u8fc7 docker-compose \u914d\u7f6e\u6765\u8bbe\u7f6e\u56fa\u5b9a\u4e3b\u673a\u540d\u3002<\/p>\n<p>\u6240\u4ee5\uff1a<br \/>\n\u6211\u5c31\u53bb\u4fee\u6539C\u4ee3\u7801\uff0c\u589e\u52a0\u4e3b\u673a\u540d\u6216\u8005\u57df\u540d\u89e3\u6790\u6210IP\u5730\u5740\u7684\u65b9\u6cd5\uff0c\u987a\u4fbf\u6574\u7406\u4e86\u7ed9\u5927\u5bb6\u5206\u4eab\u4e00\u4e0b\u3002\u4ee5\u540e\u7528\u7740\u4e86\uff0c\u76f4\u63a5copy\u5c31\u80fd\u7528\u4e86\u3002<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","sticky":false,"template":"single-with-sidebar","format":"standard","meta":{"footnotes":""},"categories":[156,154],"tags":[665,175,306,237,275,228],"class_list":["post-2150","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-c","category-154","tag-addr","tag-c","tag-ip","tag-linux","tag-socket","tag-tcp"],"_links":{"self":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/2150","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2150"}],"version-history":[{"count":3,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/2150\/revisions"}],"predecessor-version":[{"id":2154,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/2150\/revisions\/2154"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}