{"id":149,"date":"2026-03-05T10:58:11","date_gmt":"2026-03-05T02:58:11","guid":{"rendered":"http:\/\/47.104.26.10\/?p=149"},"modified":"2026-03-05T10:58:16","modified_gmt":"2026-03-05T02:58:16","slug":"%e3%80%90%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e3%80%91visual-studio-2022-%e4%b8%ad-inet_addr-%e5%bc%83%e7%94%a8%e8%ad%a6%e5%91%8a%e7%9a%84%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88%ef%bc%88%e4%bd%bf","status":"publish","type":"post","link":"http:\/\/47.104.26.10\/index.php\/2026\/03\/05\/%e3%80%90%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e3%80%91visual-studio-2022-%e4%b8%ad-inet_addr-%e5%bc%83%e7%94%a8%e8%ad%a6%e5%91%8a%e7%9a%84%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88%ef%bc%88%e4%bd%bf\/","title":{"rendered":"\u3010\u7f51\u7edc\u7f16\u7a0b\u3011Visual Studio 2022 \u4e2d inet_addr \u5f03\u7528\u8b66\u544a\u7684\u89e3\u51b3\u65b9\u6848\uff08\u4f7f\u7528 inet_pton\uff09"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u95ee\u9898\u63cf\u8ff0<\/h2>\n\n\n\n<p>\u5728 VS2022 \u4e2d\u4f7f\u7528 Winsock \u7f16\u7a0b\u65f6\uff0c\u5982\u679c\u4ee3\u7801\u4e2d\u5305\u542b <code>inet_addr(\"127.0.0.1\")<\/code> \u8fd9\u6837\u7684\u8bed\u53e5\uff0c\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6848\uff08\u65b9\u6848\u4e00\uff1a\u4f7f\u7528 inet_pton\uff09<\/h2>\n\n\n\n<p><code>inet_pton<\/code> \u80fd\u591f\u540c\u65f6\u5904\u7406 IPv4 \u548c IPv6 \u5730\u5740\uff0c\u5e76\u80fd\u68c0\u6d4b\u65e0\u6548 IP \u683c\u5f0f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6b65\u9aa4<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5305\u542b\u5fc5\u8981\u7684\u5934\u6587\u4ef6<\/strong><br>\u5728\u6e90\u6587\u4ef6\u4e2d\u6dfb\u52a0 <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><code>#include &lt;ws2tcpip.h><\/code>\uff08\u653e\u5728 <code>#include &lt;winsock2.h><\/code> \u4e4b\u540e\uff09\u3002<\/mark><\/li>\n\n\n\n<li><strong>\u66ff\u6362\u51fd\u6570\u8c03\u7528<\/strong><br>\u5c06\u539f\u6765\u7684 <code>addrSer.sin_addr.S_un.S_addr = inet_addr(\"127.0.0.1\");<\/code> \u66ff\u6362\u4e3a\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code> <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-green-cyan-color\">  inet_pton(AF_INET, \"127.0.0.1\", &amp;(addrSer.sin_addr));<\/mark><\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>\uff08\u53ef\u9009\uff09\u6dfb\u52a0\u9519\u8bef\u5904\u7406<\/strong><br><code>inet_pton<\/code> \u6210\u529f\u65f6\u8fd4\u56de 1\uff0c\u5931\u8d25\u8fd4\u56de 0 \u6216 -1\u3002\u53ef\u4ee5\u52a0\u4e0a\u5224\u65ad\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   if (inet_pton(AF_INET, \"127.0.0.1\", &amp;(addrSer.sin_addr)) != 1) {\n       \/\/ \u5904\u7406\u9519\u8bef\uff08\u4f8b\u5982 IP \u683c\u5f0f\u65e0\u6548\uff09\n       printf(\"IP \u5730\u5740\u8f6c\u6362\u5931\u8d25\\n\");\n   }<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b8c\u6574\u4ee3\u7801\u793a\u4f8b\uff08TCP \u5ba2\u6237\u7aef\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ tcpclient.cpp\n#include &lt;iostream&gt;\n#include &lt;winsock2.h&gt;\n#include &lt;ws2tcpip.h&gt;      \/\/ \u6dfb\u52a0\u6b64\u5934\u6587\u4ef6\n#pragma comment(lib, \"ws2_32.lib\")\n\nint main() {\n    WSADATA wsaData;\n    if (WSAStartup(MAKEWORD(2, 2), &amp;wsaData) != 0) {\n        std::cout &lt;&lt; \"Winsock \u521d\u59cb\u5316\u5931\u8d25\";\n        return 0;\n    }\n\n    SOCKET sockCli = socket(AF_INET, SOCK_STREAM, 0);\n    if (sockCli == INVALID_SOCKET) {\n        std::cout &lt;&lt; \"\u521b\u5efa\u5957\u63a5\u5b57\u5931\u8d25\";\n        WSACleanup();\n        return 0;\n    }\n\n    SOCKADDR_IN addrSer;\n    addrSer.sin_family = AF_INET;\n    addrSer.sin_port = htons(6000);\n    \/\/ \u66ff\u6362 inet_addr \u4e3a inet_pton\n    if (inet_pton(AF_INET, \"127.0.0.1\", &amp;(addrSer.sin_addr)) != 1) {\n        std::cout &lt;&lt; \"IP \u5730\u5740\u683c\u5f0f\u9519\u8bef\";\n        closesocket(sockCli);\n        WSACleanup();\n        return 0;\n    }\n\n    if (connect(sockCli, (SOCKADDR*)&amp;addrSer, sizeof(addrSer)) == SOCKET_ERROR) {\n        std::cout &lt;&lt; \"\u8fde\u63a5\u670d\u52a1\u5668\u5931\u8d25\";\n        closesocket(sockCli);\n        WSACleanup();\n        return 0;\n    }\n    std::cout &lt;&lt; \"\u8fde\u63a5\u670d\u52a1\u5668\u6210\u529f\\n\";\n\n    char sendbuf&#91;256], recvbuf&#91;256];\n    while (true) {\n        std::cout &lt;&lt; \"\u5ba2\u6237\u7aef\u8bf4\uff1a\";\n        std::cin &gt;&gt; sendbuf;\n        if (strcmp(sendbuf, \"exit\") == 0) break;\n        send(sockCli, sendbuf, strlen(sendbuf) + 1, 0);\n        int bytes = recv(sockCli, recvbuf, sizeof(recvbuf), 0);\n        if (bytes == SOCKET_ERROR) {\n            std::cout &lt;&lt; \"\u63a5\u6536\u6570\u636e\u5931\u8d25\";\n            break;\n        }\n        recvbuf&#91;bytes] = '\\0';   \/\/ \u786e\u4fdd\u5b57\u7b26\u4e32\u7ed3\u5c3e\n        std::cout &lt;&lt; \"\u670d\u52a1\u5668\u8bf4\uff1a\" &lt;&lt; recvbuf &lt;&lt; std::endl;\n    }\n\n    closesocket(sockCli);\n    WSACleanup();\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">TCP \u670d\u52a1\u5668\u7aef\u5bf9\u5e94\u7684\u4fee\u6539<\/h3>\n\n\n\n<p>\u9700\u8981\u4fee\u6539 <code>bind<\/code> \u4e4b\u524d\u7684\u5730\u5740\u8f6c\u6362\u90e8\u5206\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u539f\u4ee3\u7801\uff08\u5f03\u7528\uff09\naddrSer.sin_addr.S_un.S_addr = htonl(INADDR_ANY);   \/\/ \u7ed1\u5b9a\u6240\u6709\u672c\u5730\u5730\u5740\n\/\/ \u4f7f\u7528 inet_pton \u7ed1\u5b9a\u6307\u5b9a IP\uff08\u5982 \"127.0.0.1\"\uff09\uff1a\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-pale-pink-color\">inet_pton(AF_INET, \"127.0.0.1\", &amp;(addrSer.sin_addr));<\/mark>\n\/\/ \u7ed1\u5b9a\u4efb\u610f\u5730\u5740\u53ef\u4f7f\u7528\u5b8f INADDR_ANY\uff0c\u4f46\u6ce8\u610f\u8be5\u5b8f\u4e0d\u662f\u5b57\u7b26\u4e32\uff0c\u4e0d\u80fd\u76f4\u63a5\u7528 inet_pton\uff0c\u53ef\u4ee5\u4fdd\u7559\u539f\u8d4b\u503c\u65b9\u5f0f\uff08\u4e0d\u8b66\u544a\uff09\naddrSer.sin_addr.s_addr = htonl(INADDR_ANY);   \/\/ \u4e0d\u4f1a\u89e6\u53d1\u8b66\u544a<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5176\u4ed6\u6ce8\u610f\u4e8b\u9879<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>IPv6 \u652f\u6301<\/strong>\uff1a\u82e5\u9700\u8981\u652f\u6301 IPv6\uff0c\u53ea\u9700\u5c06 <code>AF_INET<\/code> \u6539\u4e3a <code>AF_INET6<\/code>\uff0c\u5e76\u786e\u4fdd\u5730\u5740\u5b57\u7b26\u4e32\u4e3a IPv6 \u683c\u5f0f\u3002<\/li>\n\n\n\n<li><strong>\u9519\u8bef\u68c0\u6d4b<\/strong>\uff1a\u59cb\u7ec8\u68c0\u67e5 <code>inet_pton<\/code> \u7684\u8fd4\u56de\u503c\uff0c\u907f\u514d\u65e0\u6548 IP \u5bfc\u81f4\u540e\u7eed\u9519\u8bef\u3002<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u95ee\u9898\u63cf\u8ff0 \u5728 VS2022 \u4e2d\u4f7f\u7528 Winsock \u7f16\u7a0b\u65f6\uff0c\u5982\u679c\u4ee3\u7801\u4e2d\u5305\u542b inet_addr(&#8220;127.0. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-149","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/47.104.26.10\/index.php\/wp-json\/wp\/v2\/posts\/149","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/47.104.26.10\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/47.104.26.10\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/47.104.26.10\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/47.104.26.10\/index.php\/wp-json\/wp\/v2\/comments?post=149"}],"version-history":[{"count":1,"href":"http:\/\/47.104.26.10\/index.php\/wp-json\/wp\/v2\/posts\/149\/revisions"}],"predecessor-version":[{"id":150,"href":"http:\/\/47.104.26.10\/index.php\/wp-json\/wp\/v2\/posts\/149\/revisions\/150"}],"wp:attachment":[{"href":"http:\/\/47.104.26.10\/index.php\/wp-json\/wp\/v2\/media?parent=149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/47.104.26.10\/index.php\/wp-json\/wp\/v2\/categories?post=149"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/47.104.26.10\/index.php\/wp-json\/wp\/v2\/tags?post=149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}