{"id":4919,"date":"2024-10-06T12:01:01","date_gmt":"2024-10-06T04:01:01","guid":{"rendered":""},"modified":"2024-10-06T12:01:01","modified_gmt":"2024-10-06T04:01:01","slug":"\u4e3a\u4ec0\u4e48linux\u9002\u5408\u7f16\u7a0b_linux\u57fa\u7840\u77e5\u8bc6\u70b9","status":"publish","type":"post","link":"https:\/\/mushiming.com\/4919.html","title":{"rendered":"\u4e3a\u4ec0\u4e48linux\u9002\u5408\u7f16\u7a0b_linux\u57fa\u7840\u77e5\u8bc6\u70b9"},"content":{"rendered":"

Linux\u7684SOCKET\u7f16\u7a0b\u8be6\u89e3<\/h3>\n<\/p>\n

\u4e00. \u7f51\u7edc\u4e2d\u8fdb\u7a0b\u4e4b\u95f4\u5982\u4f55\u901a\u4fe1<\/span><\/h2>\n
\n

     \u8fdb\u7a0b\u901a\u4fe1\u7684\u6982\u5ff5\u6700\u521d\u6765\u6e90\u4e8e\u5355\u673a\u7cfb\u7edf\u3002\u7531\u4e8e\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u5728\u81ea\u5df1\u7684\u5730\u5740\u8303\u56f4\u5185\u8fd0\u884c\uff0c\u4e3a\u4fdd\u8bc1\u4e24\u4e2a\u76f8\u4e92\u901a\u4fe1\u7684\u8fdb<\/p>\n

\u7a0b\u4e4b\u95f4\u65e2\u4e92\u4e0d\u5e72\u6270\u53c8\u534f\u8c03\u4e00\u81f4\u5de5\u4f5c\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4e3a\u8fdb\u7a0b\u901a\u4fe1\u63d0\u4f9b\u4e86\u76f8\u5e94\u8bbe\u65bd\uff0c\u5982\uff1a<\/p>\n

UNIX BSD\u6709\uff1a<\/strong><\/span>\u7ba1\u9053\uff08pipe\uff09\u3001\u547d\u540d\u7ba1\u9053\uff08named pipe\uff09\u8f6f\u4e2d\u65ad\u4fe1\u53f7\uff08signal\uff09<\/p>\n

UNIX system V\u6709\uff1a<\/strong><\/span>\u6d88\u606f\uff08message\uff09\u3001\u5171\u4eab\u5b58\u50a8\u533a\uff08shared memory\uff09\u548c\u4fe1\u53f7\u91cf\uff08semaphore)\u7b49.<\/p>\n

       \u4ed6\u4eec\u90fd\u4ec5\u9650\u4e8e\u7528\u5728\u672c\u673a\u8fdb\u7a0b\u4e4b\u95f4\u901a\u4fe1\u3002\u7f51\u95f4\u8fdb\u7a0b\u901a\u4fe1\u8981\u89e3\u51b3\u7684\u662f\u4e0d\u540c\u4e3b\u673a\u8fdb\u7a0b\u95f4\u7684\u76f8\u4e92\u901a\u4fe1\u95ee\u9898\uff08\u53ef\u628a\u540c\u673a\u8fdb\u7a0b\u901a\u4fe1\u770b\u4f5c\u662f\u5176\u4e2d\u7684\u7279\u4f8b\uff09\u3002\u4e3a\u6b64\uff0c\u9996\u5148\u8981\u89e3\u51b3\u7684\u662f\u7f51\u95f4\u8fdb\u7a0b\u6807\u8bc6\u95ee\u9898\u3002\u540c\u4e00\u4e3b\u673a\u4e0a\uff0c\u4e0d\u540c\u8fdb\u7a0b\u53ef\u7528\u8fdb\u7a0b\u53f7\uff08process ID\uff09\u552f\u4e00\u6807\u8bc6\u3002\u4f46\u5728\u7f51\u7edc\u73af\u5883\u4e0b\uff0c\u5404\u4e3b\u673a\u72ec\u7acb\u5206\u914d\u7684\u8fdb\u7a0b\u53f7\u4e0d\u80fd\u552f\u4e00\u6807\u8bc6\u8be5\u8fdb\u7a0b\u3002\u4f8b\u5982\uff0c\u4e3b\u673aA\u8d4b\u4e8e\u67d0\u8fdb\u7a0b\u53f75\uff0c\u5728B\u673a\u4e2d\u4e5f\u53ef\u4ee5\u5b58\u57285\u53f7\u8fdb\u7a0b\uff0c\u56e0\u6b64\uff0c\u201c5\u53f7\u8fdb\u7a0b\u201d\u8fd9\u53e5\u8bdd\u5c31\u6ca1\u6709\u610f\u4e49\u4e86\u3002 \u5176\u6b21\uff0c\u64cd\u4f5c\u7cfb\u7edf\u652f\u6301\u7684\u7f51\u7edc\u534f\u8bae\u4f17\u591a\uff0c\u4e0d\u540c\u534f\u8bae\u7684\u5de5\u4f5c\u65b9\u5f0f\u4e0d\u540c\uff0c\u5730\u5740\u683c\u5f0f\u4e5f\u4e0d\u540c\u3002\u56e0\u6b64\uff0c\u7f51\u95f4\u8fdb\u7a0b\u901a\u4fe1\u8fd8\u8981\u89e3\u51b3\u591a\u91cd\u534f\u8bae\u7684\u8bc6\u522b\u95ee\u9898\u3002 <\/p>\n

        \u5176\u5b9eTCP\/IP\u534f\u8bae\u65cf\u5df2\u7ecf\u5e2e\u6211\u4eec\u89e3\u51b3\u4e86\u8fd9\u4e2a\u95ee\u9898\uff0c\u7f51\u7edc\u5c42\u7684\u201cip\u5730\u5740<\/strong>\u201d<\/span>\u53ef\u4ee5\u552f\u4e00\u6807\u8bc6\u7f51\u7edc\u4e2d\u7684\u4e3b\u673a\uff0c\u800c\u4f20\u8f93\u5c42\u7684\u201c\u534f\u8bae+\u7aef\u53e3<\/strong>\u201d<\/span>\u53ef\u4ee5\u552f\u4e00\u6807\u8bc6\u4e3b\u673a\u4e2d\u7684\u5e94\u7528\u7a0b\u5e8f\uff08\u8fdb\u7a0b\uff09\u3002\u8fd9\u6837\u5229\u7528\u4e09\u5143\u7ec4\uff08ip\u5730\u5740\uff0c\u534f\u8bae\uff0c\u7aef\u53e3<\/span><\/strong>\uff09\u5c31\u53ef\u4ee5\u6807\u8bc6\u7f51\u7edc\u7684\u8fdb\u7a0b\u4e86\uff0c\u7f51\u7edc\u4e2d\u7684\u8fdb\u7a0b\u901a\u4fe1\u5c31\u53ef\u4ee5\u5229\u7528\u8fd9\u4e2a\u6807\u5fd7\u4e0e\u5176\u5b83\u8fdb\u7a0b\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n

        \u4f7f\u7528TCP\/IP\u534f\u8bae\u7684\u5e94\u7528\u7a0b\u5e8f\u901a\u5e38\u91c7\u7528\u5e94\u7528\u7f16\u7a0b\u63a5\u53e3\uff1aUNIX  BSD\u7684\u5957\u63a5\u5b57\uff08socket\uff09\u548cUNIX System V\u7684TLI\uff08\u5df2\u7ecf\u88ab\u6dd8\u6c70\uff09\uff0c\u6765\u5b9e\u73b0\u7f51\u7edc\u8fdb\u7a0b\u4e4b\u95f4\u7684\u901a\u4fe1\u3002\u5c31\u76ee\u524d\u800c\u8a00\uff0c\u51e0\u4e4e\u6240\u6709\u7684\u5e94\u7528\u7a0b\u5e8f\u90fd\u662f\u91c7\u7528socket\uff0c\u800c\u73b0\u5728\u53c8\u662f\u7f51\u7edc\u65f6\u4ee3\uff0c\u7f51\u7edc\u4e2d\u8fdb\u7a0b\u901a\u4fe1\u662f\u65e0\u5904\u4e0d\u5728\uff0c\u8fd9\u5c31\u662f\u6211\u4e3a\u4ec0\u4e48\u8bf4\u201c\u4e00\u5207\u7686socket\u201d\u3002<\/p>\n

\n

\u4e8c. \u4ec0\u4e48\u662fTCP\/IP\u3001UDP<\/span><\/h2>\n
\n

     TCP\/IP\uff08Transmission Control Protocol\/Internet Protocol\uff09\u5373\u4f20\u8f93\u63a7\u5236\u534f\u8bae\/\u7f51\u95f4\u534f\u8bae\uff0c\u662f\u4e00\u4e2a\u5de5\u4e1a\u6807\u51c6\u7684\u534f\u8bae\u96c6\uff0c\u5b83\u662f\u4e3a\u5e7f\u57df\u7f51\uff08WANs\uff09\u8bbe\u8ba1\u7684\u3002    <\/span><\/p>\n

     <\/span>TCP\/IP\u534f\u8bae\u5b58\u5728\u4e8eOS\u4e2d\uff0c\u7f51\u7edc\u670d\u52a1\u901a\u8fc7OS\u63d0\u4f9b\uff0c<\/span>\u5728OS\u4e2d\u589e\u52a0\u652f\u6301TCP\/IP\u7684\u7cfb\u7edf\u8c03\u7528\u2014\u2014Berkeley\u5957\u63a5\u5b57\uff0c\u5982Socket\uff0cConnect\uff0cSend\uff0cRecv\u7b49<\/span><\/p>\n

    UDP\uff08User Data Protocol\uff0c\u7528\u6237\u6570\u636e\u62a5\u534f\u8bae\uff09\u662f\u4e0eTCP\u76f8\u5bf9\u5e94\u7684\u534f\u8bae\u3002\u5b83\u662f\u5c5e\u4e8eTCP\/IP\u534f\u8bae\u65cf\u4e2d\u7684\u4e00\u79cd\u3002\u5982\u56fe\uff1a<\/p>\n

    \"\u4e3a\u4ec0\u4e48linux\u9002\u5408\u7f16\u7a0b_linux\u57fa\u7840\u77e5\u8bc6\u70b9<\/p>\n

 <\/p>\n

      TCP\/IP\u534f\u8bae\u65cf\u5305\u62ec\u8fd0\u8f93\u5c42\u3001\u7f51\u7edc\u5c42\u3001\u94fe\u8def\u5c42\uff0c<\/span>\u800csocket\u6240\u5728\u4f4d\u7f6e\u5982\u56fe\uff0cSocket\u662f\u5e94\u7528\u5c42\u4e0eTCP\/IP\u534f\u8bae\u65cf\u901a\u4fe1\u7684\u4e2d\u95f4\u8f6f\u4ef6\u62bd\u8c61\u5c42\u3002<\/span><\/p>\n

     \"\u4e3a\u4ec0\u4e48linux\u9002\u5408\u7f16\u7a0b_linux\u57fa\u7840\u77e5\u8bc6\u70b9<\/p>\n

 <\/p>\n

\u4e09. Socket\u662f\u4ec0\u4e48<\/span><\/h2>\n
\n

1\u3001 socket\u5957\u63a5\u5b57\uff1a<\/strong><\/span><\/h3>\n

     socket\u8d77\u6e90\u4e8eUnix\uff0c\u800cUnix\/Linux\u57fa\u672c\u54f2\u5b66\u4e4b\u4e00\u5c31\u662f\u201c\u4e00\u5207\u7686\u6587\u4ef6\u201d\uff0c\u90fd\u53ef\u4ee5\u7528\u201c\u6253\u5f00open \u2013> \u8bfb\u5199write\/read \u2013> \u5173\u95edclose\u201d\u6a21\u5f0f\u6765\u64cd\u4f5c\u3002Socket\u5c31\u662f\u8be5\u6a21\u5f0f\u7684\u4e00\u4e2a\u5b9e\u73b0\uff0c        socket\u5373\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u6587\u4ef6\uff0c\u4e00\u4e9bsocket\u51fd\u6570\u5c31\u662f\u5bf9\u5176\u8fdb\u884c\u7684\u64cd\u4f5c\uff08\u8bfb\/\u5199IO\u3001\u6253\u5f00\u3001\u5173\u95ed\uff09.<\/span>
     \u8bf4\u767d\u4e86Socket\u662f\u5e94\u7528\u5c42\u4e0eTCP\/IP\u534f\u8bae\u65cf\u901a\u4fe1\u7684\u4e2d\u95f4\u8f6f\u4ef6\u62bd\u8c61\u5c42\uff0c<\/span>\u5b83\u662f\u4e00\u7ec4\u63a5\u53e3<\/span><\/strong>\u3002\u5728\u8bbe\u8ba1\u6a21\u5f0f\u4e2d\uff0cSocket\u5176\u5b9e\u5c31\u662f\u4e00\u4e2a\u95e8\u9762\u6a21\u5f0f\uff0c\u5b83\u628a\u590d\u6742\u7684TCP\/IP\u534f\u8bae\u65cf\u9690\u85cf\u5728Socket\u63a5\u53e3\u540e\u9762\uff0c\u5bf9\u7528\u6237\u6765\u8bf4\uff0c\u4e00\u7ec4\u7b80\u5355\u7684\u63a5\u53e3\u5c31\u662f\u5168\u90e8\uff0c\u8ba9Socket\u53bb\u7ec4\u7ec7\u6570\u636e\uff0c\u4ee5\u7b26\u5408\u6307\u5b9a\u7684\u534f\u8bae\u3002<\/span><\/p>\n

      <\/span> \u6ce8\u610f\uff1a\u5176\u5b9esocket\u4e5f\u6ca1\u6709\u5c42\u7684\u6982\u5ff5\uff0c\u5b83\u53ea\u662f\u4e00\u4e2afacade\u8bbe\u8ba1\u6a21\u5f0f\u7684\u5e94\u7528\uff0c\u8ba9\u7f16\u7a0b\u53d8\u7684\u66f4\u7b80\u5355\u3002\u662f\u4e00\u4e2a\u8f6f\u4ef6\u62bd\u8c61\u5c42\u3002\u5728\u7f51\u7edc\u7f16\u7a0b\u4e2d\uff0c\u6211\u4eec\u5927\u91cf\u7528\u7684\u90fd\u662f\u901a\u8fc7socket\u5b9e\u73b0\u7684\u3002<\/span><\/p>\n

2\u3001\u5957\u63a5\u5b57\u63cf\u8ff0\u7b26<\/strong><\/h3>\n

    <\/span>      \u5176\u5b9e\u5c31\u662f\u4e00\u4e2a\u6574\u6570\uff0c\u6211\u4eec\u6700\u719f\u6089\u7684\u53e5\u67c4\u662f0\u30011\u30012\u4e09\u4e2a\uff0c0\u662f\u6807\u51c6\u8f93\u5165\uff0c1\u662f\u6807\u51c6\u8f93\u51fa\uff0c2\u662f\u6807\u51c6\u9519\u8bef\u8f93\u51fa\u30020\u30011\u30012\u662f\u6574\u6570\u8868\u793a\u7684\uff0c\u5bf9\u5e94\u7684FILE *\u7ed3\u6784\u7684\u8868\u793a\u5c31\u662fstdin\u3001stdout\u3001stderr<\/p>\n<\/p>\n

\u5957\u63a5\u5b57API\u6700\u521d\u662f\u4f5c\u4e3aUNIX\u64cd\u4f5c\u7cfb\u7edf\u7684\u4e00\u90e8\u5206\u800c\u5f00\u53d1\u7684\uff0c\u6240\u4ee5\u5957\u63a5\u5b57API\u4e0e\u7cfb\u7edf\u7684\u5176\u4ed6I\/O\u8bbe\u5907\u96c6\u6210\u5728\u4e00\u8d77\u3002\u7279\u522b\u662f\uff0c\u5f53\u5e94\u7528\u7a0b\u5e8f\u8981\u4e3a\u56e0\u7279\u7f51\u901a\u4fe1\u800c\u521b\u5efa\u4e00\u4e2a\u5957\u63a5\u5b57\uff08socket\uff09\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u5c31\u8fd4\u56de\u4e00\u4e2a\u5c0f\u6574\u6570\u4f5c\u4e3a\u63cf\u8ff0\u7b26\uff08descriptor\uff09\u6765\u6807\u8bc6\u8fd9\u4e2a\u5957\u63a5\u5b57\u3002\u7136\u540e\uff0c\u5e94\u7528\u7a0b\u5e8f\u4ee5\u8be5\u63cf\u8ff0\u7b26\u4f5c\u4e3a\u4f20\u9012\u53c2\u6570\uff0c\u901a\u8fc7\u8c03\u7528\u51fd\u6570\u6765\u5b8c\u6210\u67d0\u79cd\u64cd\u4f5c\uff08\u4f8b\u5982\u901a\u8fc7\u7f51\u7edc\u4f20\u9001\u6570\u636e\u6216\u63a5\u6536\u8f93\u5165\u7684\u6570\u636e\uff09\u3002<\/span><\/p>\n

\u5728\u8bb8\u591a\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u5957\u63a5\u5b57\u63cf\u8ff0\u7b26\u548c\u5176\u4ed6I\/O\u63cf\u8ff0\u7b26\u662f\u96c6\u6210\u5728\u4e00\u8d77\u7684\uff0c\u6240\u4ee5\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u5bf9\u6587\u4ef6\u8fdb\u884c\u5957\u63a5\u5b57I\/O\u6216I\/O\u8bfb\/\u5199\u64cd\u4f5c\u3002<\/span><\/p>\n

\u5f53\u5e94\u7528\u7a0b\u5e8f\u8981\u521b\u5efa\u4e00\u4e2a\u5957\u63a5\u5b57\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u5c31\u8fd4\u56de\u4e00\u4e2a\u5c0f\u6574\u6570\u4f5c\u4e3a\u63cf\u8ff0\u7b26\uff0c\u5e94\u7528\u7a0b\u5e8f\u5219\u4f7f\u7528\u8fd9\u4e2a\u63cf\u8ff0\u7b26\u6765\u5f15\u7528\u8be5\u5957\u63a5\u5b57\uff0c\u9700\u8981I\/O\u8bf7\u6c42\u7684\u5e94\u7528\u7a0b\u5e8f\u8bf7\u6c42\u64cd\u4f5c\u7cfb\u7edf\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u3002\u64cd\u4f5c\u7cfb\u7edf\u5c31\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u63d0\u4f9b\u7ed9\u5e94\u7528\u7a0b\u5e8f\u8bbf\u95ee\u6587\u4ef6\u3002\u4ece\u5e94\u7528\u7a0b\u5e8f\u7684\u89d2\u5ea6\u770b\uff0c\u6587\u4ef6\u63cf\u8ff0\u7b26\u662f\u4e00\u4e2a\u6574\u6570\uff0c\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u7528\u5b83\u6765\u8bfb\u5199\u6587\u4ef6\u3002\u4e0b\u56fe\u663e\u793a\uff0c\u64cd\u4f5c\u7cfb\u7edf\u5982\u4f55\u628a\u6587\u4ef6\u63cf\u8ff0\u7b26\u5b9e\u73b0\u4e3a\u4e00\u4e2a\u6307\u9488\u6570\u7ec4\uff0c\u8fd9\u4e9b\u6307\u9488\u6307\u5411\u5185\u90e8\u6570\u636e\u7ed3\u6784\u3002<\/p>\n

\"\u4e3a\u4ec0\u4e48linux\u9002\u5408\u7f16\u7a0b_linux\u57fa\u7840\u77e5\u8bc6\u70b9<\/p>\n

     \u5bf9\u4e8e\u6bcf\u4e2a\u7a0b\u5e8f\u7cfb\u7edf\u90fd\u6709\u4e00\u5f20\u5355\u72ec\u7684\u8868\u3002\u7cbe\u786e\u5730\u8bb2\uff0c\u7cfb\u7edf\u4e3a\u6bcf\u4e2a\u8fd0\u884c\u7684\u8fdb\u7a0b\u7ef4\u62a4\u4e00\u5f20\u5355\u72ec\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u8868\u3002\u5f53\u8fdb\u7a0b\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u65f6\uff0c\u7cfb\u7edf\u628a\u4e00\u4e2a\u6307\u5411\u6b64\u6587\u4ef6\u5185\u90e8\u6570\u636e\u7ed3\u6784\u7684\u6307\u9488\u5199\u5165\u6587\u4ef6\u63cf\u8ff0\u7b26\u8868\uff0c\u5e76\u628a\u8be5\u8868\u7684\u7d22\u5f15\u503c\u8fd4\u56de\u7ed9\u8c03\u7528\u8005 \u3002\u5e94\u7528\u7a0b\u5e8f\u53ea\u9700\u8bb0\u4f4f\u8fd9\u4e2a\u63cf\u8ff0\u7b26\uff0c\u5e76\u5728\u4ee5\u540e\u64cd\u4f5c\u8be5\u6587\u4ef6\u65f6\u4f7f\u7528\u5b83\u3002\u64cd\u4f5c\u7cfb\u7edf\u628a\u8be5\u63cf\u8ff0\u7b26\u4f5c\u4e3a\u7d22\u5f15\u8bbf\u95ee\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u8868\uff0c\u901a\u8fc7\u6307\u9488\u627e\u5230\u4fdd\u5b58\u8be5\u6587\u4ef6\u6240\u6709\u7684\u4fe1\u606f\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n

      \u9488\u5bf9\u5957\u63a5\u5b57\u7684\u7cfb\u7edf\u6570\u636e\u7ed3\u6784\uff1a<\/strong><\/span><\/p>\n

   1\uff09\u3001\u5957\u63a5\u5b57API\u91cc\u6709\u4e2a\u51fd\u6570socket\uff0c\u5b83\u5c31\u662f\u7528\u6765\u521b\u5efa\u4e00\u4e2a\u5957\u63a5\u5b57\u3002\u5957\u63a5\u5b57\u8bbe\u8ba1\u7684\u603b\u4f53\u601d\u8def\u662f\uff0c\u5355\u4e2a\u7cfb\u7edf\u8c03\u7528\u5c31\u53ef\u4ee5\u521b\u5efa\u4efb\u4f55\u5957\u63a5\u5b57\uff0c\u56e0\u4e3a\u5957\u63a5\u5b57\u662f\u76f8\u5f53\u7b3c\u7edf\u7684\u3002\u4e00\u65e6\u5957\u63a5\u5b57\u521b\u5efa\u540e\uff0c\u5e94\u7528\u7a0b\u5e8f\u8fd8\u9700\u8981\u8c03\u7528\u5176\u4ed6\u51fd\u6570\u6765\u6307\u5b9a\u5177\u4f53\u7ec6\u8282\u3002\u4f8b\u5982\u8c03\u7528socket\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u63cf\u8ff0\u7b26\u6761\u76ee\uff1a<\/p>\n

\"\u4e3a\u4ec0\u4e48linux\u9002\u5408\u7f16\u7a0b_linux\u57fa\u7840\u77e5\u8bc6\u70b9<\/p>\n

\n

   2\uff09\u3001\u867d\u7136\u5957\u63a5\u5b57\u7684\u5185\u90e8\u6570\u636e\u7ed3\u6784\u5305\u542b\u5f88\u591a\u5b57\u6bb5\uff0c\u4f46\u662f\u7cfb\u7edf\u521b\u5efa\u5957\u63a5\u5b57\u540e\uff0c\u5927\u591a\u6570\u5b57\u5b57\u6bb5\u6ca1\u6709\u586b\u5199\u3002\u5e94\u7528\u7a0b\u5e8f\u521b\u5efa\u5957\u63a5\u5b57\u540e\u5728\u8be5\u5957\u63a5\u5b57\u53ef\u4ee5\u4f7f\u7528\u4e4b\u524d\uff0c\u5fc5\u987b\u8c03\u7528\u5176\u4ed6\u7684\u8fc7\u7a0b\u6765\u586b\u5145\u8fd9\u4e9b\u5b57\u6bb5\u3002<\/p>\n

3\u3001\u6587\u4ef6\u63cf\u8ff0\u7b26\u548c\u6587\u4ef6\u6307\u9488\u7684\u533a\u522b\uff1a<\/strong><\/h3>\n<\/p>\n

\u6587\u4ef6\u63cf\u8ff0\u7b26\uff1a<\/strong>\u5728linux\u7cfb\u7edf\u4e2d\u6253\u5f00\u6587\u4ef6\u5c31\u4f1a\u83b7\u5f97\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u5b83\u662f\u4e2a\u5f88\u5c0f\u7684\u6b63\u6574\u6570\u3002\u6bcf\u4e2a\u8fdb\u7a0b\u5728PCB\uff08Process Control Block\uff09\u4e2d\u4fdd\u5b58\u7740\u4e00\u4efd\u6587\u4ef6\u63cf\u8ff0\u7b26\u8868\uff0c\u6587\u4ef6\u63cf\u8ff0\u7b26\u5c31\u662f\u8fd9\u4e2a\u8868\u7684\u7d22\u5f15\uff0c\u6bcf\u4e2a\u8868\u9879\u90fd\u6709\u4e00\u4e2a\u6307\u5411\u5df2\u6253\u5f00\u6587\u4ef6\u7684\u6307\u9488\u3002<\/p>\n

\u6587\u4ef6\u6307\u9488\uff1a<\/strong>C\u8bed\u8a00\u4e2d\u4f7f\u7528\u6587\u4ef6\u6307\u9488\u505a\u4e3aI\/O\u7684\u53e5\u67c4\u3002\u6587\u4ef6\u6307\u9488\u6307\u5411\u8fdb\u7a0b\u7528\u6237\u533a\u4e2d\u7684\u4e00\u4e2a\u88ab\u79f0\u4e3aFILE\u7ed3\u6784\u7684\u6570\u636e\u7ed3\u6784\u3002FILE\u7ed3\u6784\u5305\u62ec\u4e00\u4e2a\u7f13\u51b2\u533a\u548c\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002\u800c\u6587\u4ef6\u63cf\u8ff0\u7b26\u662f\u6587\u4ef6\u63cf\u8ff0\u7b26\u8868\u7684\u4e00\u4e2a\u7d22\u5f15\uff0c\u56e0\u6b64\u4ece\u67d0\u79cd\u610f\u4e49\u4e0a\u8bf4\u6587\u4ef6\u6307\u9488\u5c31\u662f\u53e5\u67c4\u7684\u53e5\u67c4\uff08\u5728Windows\u7cfb\u7edf\u4e0a\uff0c\u6587\u4ef6\u63cf\u8ff0\u7b26\u88ab\u79f0\u4f5c\u6587\u4ef6\u53e5\u67c4\uff09\u3002<\/p>\n

\u8be6\u7ec6\u5185\u5bb9\u8bf7\u770blinux\u6587\u4ef6\u7cfb\u7edf\uff1aLinux\u7cfb\u7edf\u7ed3\u6784\u8be6\u89e3_hguisu\u7684\u535a\u5ba2-CSDN\u535a\u5ba2_linux\u7cfb\u7edf<\/p>\n<\/p>\n

\u56db. \u57fa\u672c\u7684SOCKET\u63a5\u53e3\u51fd\u6570<\/span><\/h2>\n
\n

\u5728\u751f\u6d3b\u4e2d\uff0cA\u8981\u7535\u8bdd\u7ed9B\uff0cA<\/span>\u62e8\u53f7\uff0cB<\/span>\u542c\u5230\u7535\u8bdd\u94c3\u58f0\u540e\u63d0\u8d77\u7535\u8bdd\uff0c\u8fd9\u65f6A\u548cB\u5c31\u5efa\u7acb\u8d77\u4e86\u8fde\u63a5\uff0cA\u548cB\u5c31\u53ef\u4ee5\u8bb2\u8bdd\u4e86\u3002\u7b49\u4ea4\u6d41\u7ed3\u675f\uff0c\u6302\u65ad\u7535\u8bdd\u7ed3\u675f\u6b64\u6b21\u4ea4\u8c08\u3002  \u6253\u7535\u8bdd\u5f88\u7b80\u5355\u89e3\u91ca\u4e86\u8fd9\u5de5\u4f5c\u539f\u7406\uff1a\u201copen\u2014write\/read\u2014close\u201d\u6a21\u5f0f\u3002<\/span><\/p>\n

\"\u4e3a\u4ec0\u4e48linux\u9002\u5408\u7f16\u7a0b_linux\u57fa\u7840\u77e5\u8bc6\u70b9<\/p>\n

 <\/p>\n<\/p>\n

\n

    \u670d\u52a1\u5668\u7aef\u5148\u521d\u59cb\u5316Socket\uff0c\u7136\u540e\u4e0e\u7aef\u53e3\u7ed1\u5b9a(bind)\uff0c\u5bf9\u7aef\u53e3\u8fdb\u884c\u76d1\u542c(listen)\uff0c\u8c03\u7528accept\u963b\u585e\uff0c\u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5\u3002\u5728\u8fd9\u65f6\u5982\u679c\u6709\u4e2a\u5ba2\u6237\u7aef\u521d\u59cb\u5316\u4e00\u4e2aSocket\uff0c\u7136\u540e\u8fde\u63a5\u670d\u52a1\u5668(connect)\uff0c\u5982\u679c\u8fde\u63a5\u6210\u529f\uff0c\u8fd9\u65f6\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u7aef\u7684\u8fde\u63a5\u5c31\u5efa\u7acb\u4e86\u3002\u5ba2\u6237\u7aef\u53d1\u9001\u6570\u636e\u8bf7\u6c42\uff0c\u670d\u52a1\u5668\u7aef\u63a5\u6536\u8bf7\u6c42\u5e76\u5904\u7406\u8bf7\u6c42\uff0c\u7136\u540e\u628a\u56de\u5e94\u6570\u636e\u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\uff0c\u5ba2\u6237\u7aef\u8bfb\u53d6\u6570\u636e\uff0c\u6700\u540e\u5173\u95ed\u8fde\u63a5\uff0c\u4e00\u6b21\u4ea4\u4e92\u7ed3\u675f\u3002<\/p>\n

      <\/span>\u8fd9\u4e9b\u63a5\u53e3\u7684\u5b9e\u73b0\u90fd\u662f\u5185\u6838\u6765\u5b8c\u6210\u3002\u5177\u4f53\u5982\u4f55\u5b9e\u73b0\uff0c\u53ef\u4ee5\u770b\u770blinux\u7684\u5185\u6838<\/span><\/strong><\/p>\n

\n

4.1\u3001socket()\u51fd\u6570<\/h3>\n
 int socket(int protofamily, int type, int protocol);\/\/\u8fd4\u56desockfd<\/code><\/pre>\n

     sockfd\u662f\u63cf\u8ff0\u7b26\u3002<\/span><\/span><\/p>\n

  socket\u51fd\u6570\u5bf9\u5e94\u4e8e\u666e\u901a\u6587\u4ef6\u7684\u6253\u5f00\u64cd\u4f5c\u3002\u666e\u901a\u6587\u4ef6\u7684\u6253\u5f00\u64cd\u4f5c\u8fd4\u56de\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u5b57\uff0c\u800c<\/span>socket()<\/strong><\/span>\u7528\u4e8e<\/span>\u521b\u5efa\u4e00\u4e2asocket\u63cf\u8ff0\u7b26<\/span>\uff08socket descriptor\uff09\uff0c\u5b83\u552f\u4e00\u6807\u8bc6\u4e00\u4e2asocket\u3002\u8fd9\u4e2asocket\u63cf\u8ff0\u5b57\u8ddf\u6587\u4ef6\u63cf\u8ff0\u5b57\u4e00\u6837\uff0c\u540e\u7eed\u7684\u64cd\u4f5c\u90fd\u6709\u7528\u5230\u5b83\uff0c\u628a\u5b83\u4f5c\u4e3a\u53c2\u6570\uff0c\u901a\u8fc7\u5b83\u6765\u8fdb\u884c\u4e00\u4e9b\u8bfb\u5199\u64cd\u4f5c\u3002<\/span><\/p>\n

      \u6b63\u5982\u53ef\u4ee5\u7ed9fopen\u7684\u4f20\u5165\u4e0d\u540c\u53c2\u6570\u503c\uff0c\u4ee5\u6253\u5f00\u4e0d\u540c\u7684\u6587\u4ef6\u3002\u521b\u5efasocket\u7684\u65f6\u5019\uff0c\u4e5f\u53ef\u4ee5\u6307\u5b9a\u4e0d\u540c\u7684\u53c2\u6570\u521b\u5efa\u4e0d\u540c\u7684socket\u63cf\u8ff0\u7b26\uff0csocket\u51fd\u6570\u7684\u4e09\u4e2a\u53c2\u6570\u5206\u522b\u4e3a\uff1a<\/p>\n

    \n
  • protofamily\uff1a\u5373\u534f\u8bae\u57df\uff0c\u53c8\u79f0\u4e3a\u534f\u8bae\u65cf\uff08family\uff09\u3002\u5e38\u7528\u7684\u534f\u8bae\u65cf\u6709\uff0c<\/span>AF_INET<\/span>(IPV4)<\/span>\u3001<\/span>AF_INET6<\/span>(IPV6<\/span>)<\/span>\u3001<\/span>AF_LOCAL<\/span>\uff08\u6216\u79f0<\/span>AF_UNIX<\/span>\uff0cUnix\u57dfsocket\uff09\u3001<\/span>AF_ROUTE<\/span>\u7b49\u7b49\u3002\u534f\u8bae\u65cf\u51b3\u5b9a\u4e86socket\u7684\u5730\u5740\u7c7b\u578b\uff0c\u5728\u901a\u4fe1\u4e2d\u5fc5\u987b\u91c7\u7528\u5bf9\u5e94\u7684\u5730\u5740\uff0c\u5982AF_INET\u51b3\u5b9a\u4e86\u8981\u7528ipv4\u5730\u5740\uff0832\u4f4d\u7684\uff09\u4e0e\u7aef\u53e3\u53f7\uff0816\u4f4d\u7684\uff09\u7684\u7ec4\u5408\u3001AF_UNIX\u51b3\u5b9a\u4e86\u8981\u7528\u4e00\u4e2a\u7edd\u5bf9\u8def\u5f84\u540d\u4f5c\u4e3a\u5730\u5740\u3002<\/span><\/li>\n
  • type\uff1a\u6307\u5b9asocket\u7c7b\u578b\u3002\u5e38\u7528\u7684socket\u7c7b\u578b\u6709\uff0cSOCK_STREAM<\/span>\u3001SOCK_DGRAM<\/span>\u3001SOCK_RAW<\/span>\u3001SOCK_PACKET<\/span>\u3001SOCK_SEQPACKET<\/span>\u7b49\u7b49\uff08socket\u7684\u7c7b\u578b\u6709\u54ea\u4e9b\uff1f\uff09\u3002<\/li>\n
  • protocol\uff1a\u6545\u540d\u601d\u610f\uff0c\u5c31\u662f\u6307\u5b9a\u534f\u8bae\u3002\u5e38\u7528\u7684\u534f\u8bae\u6709\uff0cIPPROTO_TCP<\/span>\u3001IPPTOTO_UDP<\/span>\u3001IPPROTO_SCTP<\/span>\u3001IPPROTO_TIPC<\/span>\u7b49\uff0c\u5b83\u4eec\u5206\u522b\u5bf9\u5e94TCP\u4f20\u8f93\u534f\u8bae\u3001UDP\u4f20\u8f93\u534f\u8bae\u3001STCP\u4f20\u8f93\u534f\u8bae\u3001TIPC\u4f20\u8f93\u534f\u8bae\uff08\u8fd9\u4e2a\u534f\u8bae\u6211\u5c06\u4f1a\u5355\u72ec\u5f00\u7bc7\u8ba8\u8bba\uff01\uff09\u3002<\/li>\n<\/ul>\n

    \u6ce8\u610f<\/span><\/strong>\uff1a\u5e76\u4e0d\u662f\u4e0a\u9762\u7684type\u548cprotocol\u53ef\u4ee5\u968f\u610f\u7ec4\u5408\u7684\uff0c\u5982SOCK_STREAM\u4e0d\u53ef\u4ee5\u8ddfIPPROTO_UDP\u7ec4\u5408\u3002\u5f53protocol\u4e3a0\u65f6\uff0c\u4f1a\u81ea\u52a8\u9009\u62e9type\u7c7b\u578b\u5bf9\u5e94\u7684\u9ed8\u8ba4\u534f\u8bae\u3002<\/span><\/p>\n

    \u5f53\u6211\u4eec\u8c03\u7528socket<\/strong><\/span>\u521b\u5efa\u4e00\u4e2asocket\u65f6\uff0c\u8fd4\u56de\u7684socket\u63cf\u8ff0\u5b57\u5b83\u5b58\u5728\u4e8e\u534f\u8bae\u65cf\uff08address family\uff0cAF_XXX\uff09\u7a7a\u95f4\u4e2d\uff0c\u4f46\u6ca1\u6709\u4e00\u4e2a\u5177\u4f53\u7684\u5730\u5740\u3002\u5982\u679c\u60f3\u8981\u7ed9\u5b83\u8d4b\u503c\u4e00\u4e2a\u5730\u5740\uff0c\u5c31\u5fc5\u987b\u8c03\u7528bind()<\/span>\u51fd\u6570\uff0c\u5426\u5219\u5c31\u5f53\u8c03\u7528connect()<\/span>\u3001listen()<\/span>\u65f6\u7cfb\u7edf\u4f1a\u81ea\u52a8\u968f\u673a\u5206\u914d\u4e00\u4e2a\u7aef\u53e3\u3002<\/p>\n

    4.2\u3001bind()\u51fd\u6570<\/h3>\n

    \u6b63\u5982\u4e0a\u9762\u6240\u8bf4bind()\u51fd\u6570\u628a\u4e00\u4e2a\u5730\u5740\u65cf\u4e2d\u7684\u7279\u5b9a\u5730\u5740\u8d4b\u7ed9socket\u3002\u4f8b\u5982\u5bf9\u5e94AF_INET<\/span>\u3001AF_INET6<\/span>\u5c31\u662f\u628a\u4e00\u4e2aipv4\u6216ipv6\u5730\u5740\u548c\u7aef\u53e3\u53f7\u7ec4\u5408\u8d4b\u7ed9socket\u3002<\/p>\n

    int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);<\/code><\/pre>\n

    \u51fd\u6570\u7684\u4e09\u4e2a\u53c2\u6570\u5206\u522b\u4e3a\uff1a<\/p>\n

      \n
    • sockfd\uff1a\u5373socket\u63cf\u8ff0\u5b57\uff0c\u5b83\u662f\u901a\u8fc7socket()\u51fd\u6570\u521b\u5efa\u4e86\uff0c\u552f\u4e00\u6807\u8bc6\u4e00\u4e2asocket\u3002bind<\/span>()\u51fd\u6570\u5c31\u662f\u5c06\u7ed9\u8fd9\u4e2a\u63cf\u8ff0\u5b57\u7ed1\u5b9a\u4e00\u4e2a\u540d\u5b57\u3002<\/li>\n
    • addr\uff1a\u4e00\u4e2aconst struct sockaddr *\u6307\u9488\uff0c\u6307\u5411\u8981\u7ed1\u5b9a\u7ed9sockfd\u7684\u534f\u8bae\u5730\u5740\u3002\u8fd9\u4e2a\u5730\u5740\u7ed3\u6784\u6839\u636e\u5730\u5740\u521b\u5efasocket\u65f6\u7684\u5730\u5740\u534f\u8bae\u65cf\u7684\u4e0d\u540c\u800c\u4e0d\u540c\uff0c\u5982ipv4\u5bf9\u5e94\u7684\u662f\uff1a \n
      struct sockaddr_in { sa_family_t sin_family; \/* address family: AF_INET *\/ in_port_t sin_port; \/* port in network byte order *\/ struct in_addr sin_addr; \/* internet address *\/ }; \/* Internet address. *\/ struct in_addr { uint32_t s_addr; \/* address in network byte order *\/ };<\/code><\/pre>\n

      ipv6\u5bf9\u5e94\u7684\u662f\uff1a  <\/p>\n

      struct sockaddr_in6 { sa_family_t sin6_family; \/* AF_INET6 *\/ in_port_t sin6_port; \/* port number *\/ uint32_t sin6_flowinfo; \/* IPv6 flow information *\/ struct in6_addr sin6_addr; \/* IPv6 address *\/ uint32_t sin6_scope_id; \/* Scope ID (new in 2.4) *\/ }; struct in6_addr { unsigned char s6_addr[16]; \/* IPv6 address *\/ };<\/code><\/pre>\n

      Unix\u57df\u5bf9\u5e94\u7684\u662f\uff1a  <\/p>\n

      #define UNIX_PATH_MAX 108 struct sockaddr_un { sa_family_t sun_family; \/* AF_UNIX *\/ char sun_path[UNIX_PATH_MAX]; \/* pathname *\/ };<\/code><\/pre>\n<\/li>\n
    • addrlen\uff1a\u5bf9\u5e94\u7684\u662f\u5730\u5740\u7684\u957f\u5ea6\u3002<\/li>\n<\/ul>\n

      \u901a\u5e38\u670d\u52a1\u5668\u5728\u542f\u52a8\u7684\u65f6\u5019\u90fd\u4f1a\u7ed1\u5b9a\u4e00\u4e2a\u4f17\u6240\u5468\u77e5\u7684\u5730\u5740\uff08\u5982ip\u5730\u5740+\u7aef\u53e3\u53f7\uff09\uff0c\u7528\u4e8e\u63d0\u4f9b\u670d\u52a1\uff0c\u5ba2\u6237\u5c31\u53ef\u4ee5\u901a\u8fc7\u5b83\u6765\u63a5\u8fde\u670d\u52a1\u5668\uff1b\u800c\u5ba2\u6237\u7aef\u5c31\u4e0d\u7528\u6307\u5b9a\uff0c\u6709\u7cfb\u7edf\u81ea\u52a8\u5206\u914d\u4e00\u4e2a\u7aef\u53e3\u53f7\u548c\u81ea\u8eab\u7684ip\u5730\u5740\u7ec4\u5408\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u901a\u5e38\u670d\u52a1\u5668\u7aef\u5728listen\u4e4b\u524d\u4f1a\u8c03\u7528bind<\/span>()\uff0c\u800c\u5ba2\u6237\u7aef\u5c31\u4e0d\u4f1a\u8c03\u7528\uff0c\u800c\u662f\u5728connect<\/span>()\u65f6\u7531\u7cfb\u7edf\u968f\u673a\u751f\u6210\u4e00\u4e2a\u3002<\/p>\n

      \n

      \u7f51\u7edc\u5b57\u8282\u5e8f\u4e0e\u4e3b\u673a\u5b57\u8282\u5e8f<\/h4>\n

      \u4e3b\u673a\u5b57\u8282\u5e8f<\/span><\/strong>\u5c31\u662f\u6211\u4eec\u5e73\u5e38\u8bf4\u7684\u5927\u7aef\u548c\u5c0f\u7aef\u6a21\u5f0f\uff1a\u4e0d\u540c\u7684CPU\u6709\u4e0d\u540c\u7684\u5b57\u8282\u5e8f\u7c7b\u578b\uff0c\u8fd9\u4e9b\u5b57\u8282\u5e8f\u662f\u6307\u6574\u6570\u5728\u5185\u5b58\u4e2d\u4fdd\u5b58\u7684\u987a\u5e8f\uff0c\u8fd9\u4e2a\u53eb\u505a\u4e3b\u673a\u5e8f\u3002\u5f15\u7528\u6807\u51c6\u7684Big-Endian\u548cLittle-Endian\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n

      \u3000\u3000a) Little-Endian\u5c31\u662f\u4f4e\u4f4d\u5b57\u8282\u6392\u653e\u5728\u5185\u5b58\u7684\u4f4e\u5730\u5740\u7aef\uff0c\u9ad8\u4f4d\u5b57\u8282\u6392\u653e\u5728\u5185\u5b58\u7684\u9ad8\u5730\u5740\u7aef\u3002<\/p>\n

      \u3000\u3000b) Big-Endian\u5c31\u662f\u9ad8\u4f4d\u5b57\u8282\u6392\u653e\u5728\u5185\u5b58\u7684\u4f4e\u5730\u5740\u7aef\uff0c\u4f4e\u4f4d\u5b57\u8282\u6392\u653e\u5728\u5185\u5b58\u7684\u9ad8\u5730\u5740\u7aef\u3002<\/p>\n

      \u7f51\u7edc\u5b57\u8282\u5e8f<\/span><\/strong>\uff1a4\u4e2a\u5b57\u8282\u768432 bit\u503c\u4ee5\u4e0b\u9762\u7684\u6b21\u5e8f\u4f20\u8f93\uff1a\u9996\u5148\u662f0\uff5e7bit\uff0c\u5176\u6b218\uff5e15bit\uff0c\u7136\u540e16\uff5e23bit\uff0c\u6700\u540e\u662f24~31bit\u3002\u8fd9\u79cd\u4f20\u8f93\u6b21\u5e8f\u79f0\u4f5c\u5927\u7aef\u5b57\u8282\u5e8f\u3002\u7531\u4e8eTCP\/IP\u9996\u90e8\u4e2d\u6240\u6709\u7684\u4e8c\u8fdb\u5236\u6574\u6570\u5728\u7f51\u7edc\u4e2d\u4f20\u8f93\u65f6\u90fd\u8981\u6c42\u4ee5\u8fd9\u79cd\u6b21\u5e8f\uff0c\u56e0\u6b64\u5b83\u53c8\u79f0\u4f5c\u7f51\u7edc\u5b57\u8282\u5e8f\u3002<\/strong>\u5b57\u8282\u5e8f\uff0c\u987e\u540d\u601d\u4e49\u5b57\u8282\u7684\u987a\u5e8f\uff0c\u5c31\u662f\u5927\u4e8e\u4e00\u4e2a\u5b57\u8282\u7c7b\u578b\u7684\u6570\u636e\u5728\u5185\u5b58\u4e2d\u7684\u5b58\u653e\u987a\u5e8f\uff0c\u4e00\u4e2a\u5b57\u8282\u7684\u6570\u636e\u6ca1\u6709\u987a\u5e8f\u7684\u95ee\u9898\u4e86\u3002<\/p>\n

      \u6240\u4ee5<\/span>\uff1a\u5728\u5c06\u4e00\u4e2a\u5730\u5740\u7ed1\u5b9a\u5230socket\u7684\u65f6\u5019\uff0c\u8bf7\u5148\u5c06\u4e3b\u673a\u5b57\u8282\u5e8f\u8f6c\u6362\u6210\u4e3a\u7f51\u7edc\u5b57\u8282\u5e8f\uff0c\u800c\u4e0d\u8981\u5047\u5b9a\u4e3b\u673a\u5b57\u8282\u5e8f\u8ddf\u7f51\u7edc\u5b57\u8282\u5e8f\u4e00\u6837\u4f7f\u7528\u7684\u662fBig-Endian\u3002\u7531\u4e8e\u8fd9\u4e2a\u95ee\u9898\u66fe\u5f15\u53d1\u8fc7\u8840\u6848\uff01\u516c\u53f8\u9879\u76ee\u4ee3\u7801\u4e2d\u7531\u4e8e\u5b58\u5728\u8fd9\u4e2a\u95ee\u9898\uff0c\u5bfc\u81f4\u4e86\u5f88\u591a\u83ab\u540d\u5176\u5999\u7684\u95ee\u9898\uff0c\u6240\u4ee5\u8bf7\u8c28\u8bb0\u5bf9\u4e3b\u673a\u5b57\u8282\u5e8f\u4e0d\u8981\u505a\u4efb\u4f55\u5047\u5b9a\uff0c\u52a1\u5fc5\u5c06\u5176\u8f6c\u5316\u4e3a\u7f51\u7edc\u5b57\u8282\u5e8f\u518d\u8d4b\u7ed9socket\u3002<\/p>\n<\/blockquote>\n

      4.3\u3001listen()\u3001connect()\u51fd\u6570<\/h3>\n

      \u5982\u679c\u4f5c\u4e3a\u4e00\u4e2a\u670d\u52a1\u5668\uff0c\u5728\u8c03\u7528socket()<\/span>\u3001bind<\/span>()<\/span>\u4e4b\u540e\u5c31\u4f1a\u8c03\u7528listen()<\/span>\u6765\u76d1\u542c\u8fd9\u4e2asocket\uff0c\u5982\u679c\u5ba2\u6237\u7aef\u8fd9\u65f6\u8c03\u7528connect()<\/span>\u53d1\u51fa\u8fde\u63a5\u8bf7\u6c42\uff0c\u670d\u52a1\u5668\u7aef\u5c31\u4f1a\u63a5\u6536\u5230\u8fd9\u4e2a\u8bf7\u6c42\u3002<\/p>\n

      int listen(int sockfd, int backlog); int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);<\/code><\/pre>\n

      listen\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u5373\u4e3a\u8981\u76d1\u542c\u7684socket\u63cf\u8ff0\u5b57\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u76f8\u5e94socket\u53ef\u4ee5\u6392\u961f\u7684\u6700\u5927\u8fde\u63a5\u4e2a\u6570\u3002socket()\u51fd\u6570\u521b\u5efa\u7684socket\u9ed8\u8ba4\u662f\u4e00\u4e2a\u4e3b\u52a8\u7c7b\u578b\u7684\uff0clisten\u51fd\u6570\u5c06socket\u53d8\u4e3a\u88ab\u52a8\u7c7b\u578b\u7684\uff0c\u7b49\u5f85\u5ba2\u6237\u7684\u8fde\u63a5\u8bf7\u6c42\u3002<\/p>\n

      connect\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u5373\u4e3a\u5ba2\u6237\u7aef\u7684socket\u63cf\u8ff0\u5b57\uff0c\u7b2c\u4e8c\u53c2\u6570\u4e3a\u670d\u52a1\u5668\u7684socket\u5730\u5740\uff0c\u7b2c\u4e09\u4e2a\u53c2\u6570\u4e3asocket\u5730\u5740\u7684\u957f\u5ea6\u3002\u5ba2\u6237\u7aef\u901a\u8fc7\u8c03\u7528connect\u51fd\u6570\u6765\u5efa\u7acb\u4e0eTCP\u670d\u52a1\u5668\u7684\u8fde\u63a5\u3002<\/p>\n

      4.4\u3001accept()\u51fd\u6570<\/h3>\n

      TCP\u670d\u52a1\u5668\u7aef\u4f9d\u6b21\u8c03\u7528socket()<\/span>\u3001bind<\/span>()<\/span>\u3001listen()<\/span>\u4e4b\u540e\uff0c\u5c31\u4f1a\u76d1\u542c\u6307\u5b9a\u7684socket\u5730\u5740\u4e86\u3002TCP\u5ba2\u6237\u7aef\u4f9d\u6b21\u8c03\u7528socket()<\/span>\u3001connect()<\/span>\u4e4b\u540e\u5c31\u5411TCP\u670d\u52a1\u5668\u53d1\u9001\u4e86\u4e00\u4e2a\u8fde\u63a5\u8bf7\u6c42\u3002TCP\u670d\u52a1\u5668\u76d1\u542c\u5230\u8fd9\u4e2a\u8bf7\u6c42\u4e4b\u540e\uff0c\u5c31\u4f1a\u8c03\u7528accept<\/span>()<\/span>\u51fd\u6570\u53d6\u63a5\u6536\u8bf7\u6c42\uff0c\u8fd9\u6837\u8fde\u63a5\u5c31\u5efa\u7acb\u597d\u4e86\u3002\u4e4b\u540e\u5c31\u53ef\u4ee5\u5f00\u59cb\u7f51\u7edcI\/O\u64cd\u4f5c\u4e86\uff0c\u5373\u7c7b\u540c\u4e8e\u666e\u901a\u6587\u4ef6\u7684\u8bfb\u5199I\/O\u64cd\u4f5c\u3002<\/p>\n

      int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); \/\/\u8fd4\u56de\u8fde\u63a5connect_fd<\/code><\/pre>\n

      \n

      \u53c2\u6570sockfd<\/p>\n

      \u53c2\u6570sockfd\u5c31\u662f\u4e0a\u9762\u89e3\u91ca\u4e2d\u7684\u76d1\u542c\u5957\u63a5\u5b57\uff0c\u8fd9\u4e2a\u5957\u63a5\u5b57\u7528\u6765\u76d1\u542c\u4e00\u4e2a\u7aef\u53e3\uff0c\u5f53\u6709\u4e00\u4e2a\u5ba2\u6237\u4e0e\u670d\u52a1\u5668\u8fde\u63a5\u65f6\uff0c\u5b83\u4f7f\u7528\u8fd9\u4e2a\u4e00\u4e2a\u7aef\u53e3\u53f7\uff0c\u800c\u6b64\u65f6\u8fd9\u4e2a\u7aef\u53e3\u53f7\u6b63\u4e0e\u8fd9\u4e2a\u5957\u63a5\u5b57\u5173\u8054\u3002\u5f53\u7136\u5ba2\u6237\u4e0d\u77e5\u9053\u5957\u63a5\u5b57\u8fd9\u4e9b\u7ec6\u8282\uff0c\u5b83\u53ea\u77e5\u9053\u4e00\u4e2a\u5730\u5740\u548c\u4e00\u4e2a\u7aef\u53e3\u53f7\u3002<\/p>\n

      \u53c2\u6570addr<\/p>\n

      \u8fd9\u662f\u4e00\u4e2a\u7ed3\u679c\u53c2\u6570\uff0c\u5b83\u7528\u6765\u63a5\u53d7\u4e00\u4e2a\u8fd4\u56de\u503c\uff0c\u8fd9\u8fd4\u56de\u503c\u6307\u5b9a\u5ba2\u6237\u7aef\u7684\u5730\u5740\uff0c\u5f53\u7136\u8fd9\u4e2a\u5730\u5740\u662f\u901a\u8fc7\u67d0\u4e2a\u5730\u5740\u7ed3\u6784\u6765\u63cf\u8ff0\u7684\uff0c\u7528\u6237\u5e94\u8be5\u77e5\u9053\u8fd9\u4e00\u4e2a\u4ec0\u4e48\u6837\u7684\u5730\u5740\u7ed3\u6784\u3002\u5982\u679c\u5bf9\u5ba2\u6237\u7684\u5730\u5740\u4e0d\u611f\u5174\u8da3\uff0c\u90a3\u4e48\u53ef\u4ee5\u628a\u8fd9\u4e2a\u503c\u8bbe\u7f6e\u4e3aNULL\u3002<\/p>\n

      \u53c2\u6570len<\/p>\n

      \u5982\u540c\u5927\u5bb6\u6240\u8ba4\u4e3a\u7684\uff0c\u5b83\u4e5f\u662f\u7ed3\u679c\u7684\u53c2\u6570\uff0c\u7528\u6765\u63a5\u53d7\u4e0a\u8ff0addr\u7684\u7ed3\u6784\u7684\u5927\u5c0f\u7684\uff0c\u5b83\u6307\u660eaddr\u7ed3\u6784\u6240\u5360\u6709\u7684\u5b57\u8282\u4e2a\u6570\u3002\u540c\u6837\u7684\uff0c\u5b83\u4e5f\u53ef\u4ee5\u88ab\u8bbe\u7f6e\u4e3aNULL\u3002<\/p>\n<\/p>\n

      \u5982\u679caccept\u6210\u529f\u8fd4\u56de\uff0c\u5219\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u5df2\u7ecf\u6b63\u786e\u5efa\u7acb\u8fde\u63a5\u4e86\uff0c\u6b64\u65f6\u670d\u52a1\u5668\u901a\u8fc7accept\u8fd4\u56de\u7684\u5957\u63a5\u5b57\u6765\u5b8c\u6210\u4e0e\u5ba2\u6237\u7684\u901a\u4fe1\u3002<\/p>\n

      \u6ce8\u610f<\/span>\uff1a<\/p>\n

            accept\u9ed8\u8ba4\u4f1a\u963b\u585e\u8fdb\u7a0b\uff0c\u76f4\u5230\u6709\u4e00\u4e2a\u5ba2\u6237\u8fde\u63a5\u5efa\u7acb\u540e\u8fd4\u56de\uff0c\u5b83\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u65b0\u53ef\u7528\u7684\u5957\u63a5\u5b57\uff0c\u8fd9\u4e2a\u5957\u63a5\u5b57\u662f\u8fde\u63a5\u5957\u63a5\u5b57\u3002<\/p>\n

      \u6b64\u65f6\u6211\u4eec\u9700\u8981\u533a\u5206\u4e24\u79cd\u5957\u63a5\u5b57\uff0c<\/p>\n

             \u76d1\u542c\u5957\u63a5\u5b57: \u76d1\u542c<\/span>\u5957\u63a5\u5b57\u6b63\u5982accept\u7684\u53c2\u6570sockfd\uff0c\u5b83\u662f\u76d1\u542c\u5957\u63a5\u5b57\uff0c\u5728\u8c03\u7528listen\u51fd\u6570\u4e4b\u540e\uff0c\u662f\u670d\u52a1\u5668\u5f00\u59cb\u8c03\u7528socket()\u51fd\u6570\u751f\u6210\u7684\uff0c\u79f0\u4e3a<\/span>\u76d1\u542csocket\u63cf\u8ff0\u5b57(\u76d1\u542c\u5957\u63a5\u5b57<\/span>)<\/span><\/p>\n

             \u8fde\u63a5\u5957\u63a5\u5b57\uff1a<\/span>\u4e00\u4e2a\u5957\u63a5\u5b57\u4f1a\u4ece\u4e3b\u52a8\u8fde\u63a5\u7684\u5957\u63a5\u5b57\u53d8\u8eab\u4e3a\u4e00\u4e2a\u76d1\u542c\u5957\u63a5\u5b57\uff1b<\/span>\u800caccept\u51fd\u6570\u8fd4\u56de\u7684\u662f<\/span>\u5df2\u8fde\u63a5s<\/span>ocket\u63cf\u8ff0\u5b57(\u4e00\u4e2a\u8fde\u63a5\u5957\u63a5\u5b57<\/span>)<\/span>\uff0c\u5b83\u4ee3\u8868\u7740\u4e00\u4e2a\u7f51\u7edc\u5df2\u7ecf\u5b58\u5728\u7684\u70b9\u70b9\u8fde\u63a5\u3002<\/span><\/p>\n

              \u4e00\u4e2a\u670d\u52a1\u5668\u901a\u5e38\u901a\u5e38\u4ec5\u4ec5\u53ea\u521b\u5efa\u4e00\u4e2a\u76d1\u542csocket\u63cf\u8ff0\u5b57\uff0c\u5b83\u5728\u8be5\u670d\u52a1\u5668\u7684\u751f\u547d\u5468\u671f\u5185\u4e00\u76f4\u5b58\u5728\u3002\u5185\u6838\u4e3a\u6bcf\u4e2a\u7531\u670d\u52a1\u5668\u8fdb\u7a0b\u63a5\u53d7\u7684\u5ba2\u6237\u8fde\u63a5\u521b\u5efa\u4e86\u4e00\u4e2a\u5df2\u8fde\u63a5socket\u63cf\u8ff0\u5b57\uff0c\u5f53\u670d\u52a1\u5668\u5b8c\u6210\u4e86\u5bf9\u67d0\u4e2a\u5ba2\u6237\u7684\u670d\u52a1\uff0c\u76f8\u5e94\u7684\u5df2\u8fde\u63a5socket\u63cf\u8ff0\u5b57\u5c31\u88ab\u5173\u95ed\u3002<\/span><\/span><\/p>\n

              \u81ea\u7136\u8981\u95ee\u7684\u662f\uff1a\u4e3a\u4ec0\u4e48\u8981\u6709\u4e24\u79cd\u5957\u63a5\u5b57\uff1f\u539f\u56e0\u5f88\u7b80\u5355\uff0c\u5982\u679c\u4f7f\u7528\u4e00\u4e2a\u63cf\u8ff0\u5b57\u7684\u8bdd\uff0c\u90a3\u4e48\u5b83\u7684\u529f\u80fd\u592a\u591a\uff0c\u4f7f\u5f97\u4f7f\u7528\u5f88\u4e0d\u76f4\u89c2\uff0c\u540c\u65f6\u5728\u5185\u6838\u786e\u5b9e\u4ea7\u751f\u4e86\u4e00\u4e2a\u8fd9\u6837\u7684\u65b0\u7684\u63cf\u8ff0\u5b57\u3002<\/span><\/p>\n

      \u8fde\u63a5\u5957\u63a5\u5b57<\/span>socketfd_new \u5e76\u6ca1\u6709\u5360\u7528\u65b0\u7684\u7aef\u53e3\u4e0e\u5ba2\u6237\u7aef\u901a\u4fe1\uff0c\u4f9d\u7136\u4f7f\u7528\u7684\u662f\u4e0e\u76d1\u542c\u5957\u63a5\u5b57socketfd\u4e00\u6837\u7684\u7aef\u53e3\u53f7<\/span><\/span><\/p>\n

      4.5\u3001read()\u3001write()\u7b49\u51fd\u6570<\/h3>\n

      \u4e07\u4e8b\u5177\u5907\u53ea\u6b20\u4e1c\u98ce\uff0c\u81f3\u6b64\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u5df2\u7ecf\u5efa\u7acb\u597d\u8fde\u63a5\u4e86\u3002\u53ef\u4ee5\u8c03\u7528\u7f51\u7edcI\/O\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u4e86\uff0c\u5373\u5b9e\u73b0\u4e86\u7f51\u54af\u4e2d\u4e0d\u540c\u8fdb\u7a0b\u4e4b\u95f4\u7684\u901a\u4fe1\uff01\u7f51\u7edcI\/O\u64cd\u4f5c\u6709\u4e0b\u9762\u51e0\u7ec4\uff1a<\/p>\n

        \n
      • read()\/write()<\/li>\n
      • recv()\/send()<\/li>\n
      • readv()\/writev()<\/li>\n
      • recvmsg()\/sendmsg()<\/span><\/li>\n
      • recvfrom()\/sendto()<\/li>\n<\/ul>\n

        \u6211\u63a8\u8350\u4f7f\u7528recvmsg()\/sendmsg()<\/span>\u51fd\u6570\uff0c\u8fd9\u4e24\u4e2a\u51fd\u6570\u662f\u6700\u901a\u7528\u7684I\/O\u51fd\u6570\uff0c\u5b9e\u9645\u4e0a\u53ef\u4ee5\u628a\u4e0a\u9762\u7684\u5176\u5b83\u51fd\u6570\u90fd\u66ff\u6362\u6210\u8fd9\u4e24\u4e2a\u51fd\u6570\u3002\u5b83\u4eec\u7684\u58f0\u660e\u5982\u4e0b\uff1a<\/p>\n

         #include <unistd.h> ssize_t read(int fd, void *buf, size_t count); ssize_t write(int fd, const void *buf, size_t count); #include <sys\/types.h> #include <sys\/socket.h> ssize_t send(int sockfd, const void *buf, size_t len, int flags); ssize_t recv(int sockfd, void *buf, size_t len, int flags); ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen); ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen); ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags); ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags); <\/code><\/pre>\n

        read\u51fd\u6570\u662f\u8d1f\u8d23\u4ecefd\u4e2d\u8bfb\u53d6\u5185\u5bb9.\u5f53\u8bfb\u6210\u529f\u65f6\uff0cread\u8fd4\u56de\u5b9e\u9645\u6240\u8bfb\u7684\u5b57\u8282\u6570\uff0c\u5982\u679c\u8fd4\u56de\u7684\u503c\u662f0\u8868\u793a\u5df2\u7ecf\u8bfb\u5230\u6587\u4ef6\u7684\u7ed3\u675f\u4e86\uff0c\u5c0f\u4e8e0\u8868\u793a\u51fa\u73b0\u4e86\u9519\u8bef\u3002\u5982\u679c\u9519\u8bef\u4e3aEINTR\u8bf4\u660e\u8bfb\u662f\u7531\u4e2d\u65ad\u5f15\u8d77\u7684\uff0c\u5982\u679c\u662fECONNREST\u8868\u793a\u7f51\u7edc\u8fde\u63a5\u51fa\u4e86\u95ee\u9898\u3002<\/p>\n

        write\u51fd\u6570\u5c06buf\u4e2d\u7684nbytes\u5b57\u8282\u5185\u5bb9\u5199\u5165\u6587\u4ef6\u63cf\u8ff0\u7b26fd.\u6210\u529f\u65f6\u8fd4\u56de\u5199\u7684\u5b57\u8282\u6570\u3002\u5931\u8d25\u65f6\u8fd4\u56de-1\uff0c\u5e76\u8bbe\u7f6eerrno\u53d8\u91cf\u3002 \u5728\u7f51\u7edc\u7a0b\u5e8f\u4e2d\uff0c\u5f53\u6211\u4eec\u5411\u5957\u63a5\u5b57\u6587\u4ef6\u63cf\u8ff0\u7b26\u5199\u65f6\u6709\u4fe9\u79cd\u53ef\u80fd\u30021)write\u7684\u8fd4\u56de\u503c\u5927\u4e8e0\uff0c\u8868\u793a\u5199\u4e86\u90e8\u5206\u6216\u8005\u662f\u5168\u90e8\u7684\u6570\u636e\u30022)\u8fd4\u56de\u7684\u503c\u5c0f\u4e8e0\uff0c\u6b64\u65f6\u51fa\u73b0\u4e86\u9519\u8bef\u3002\u6211\u4eec\u8981\u6839\u636e\u9519\u8bef\u7c7b\u578b\u6765\u5904\u7406\u3002\u5982\u679c\u9519\u8bef\u4e3aEINTR\u8868\u793a\u5728\u5199\u7684\u65f6\u5019\u51fa\u73b0\u4e86\u4e2d\u65ad\u9519\u8bef\u3002\u5982\u679c\u4e3aEPIPE\u8868\u793a\u7f51\u7edc\u8fde\u63a5\u51fa\u73b0\u4e86\u95ee\u9898(\u5bf9\u65b9\u5df2\u7ecf\u5173\u95ed\u4e86\u8fde\u63a5)\u3002<\/p>\n

        \u5176\u5b83\u7684\u6211\u5c31\u4e0d\u4e00\u4e00\u4ecb\u7ecd\u8fd9\u51e0\u5bf9I\/O\u51fd\u6570\u4e86\uff0c\u5177\u4f53\u53c2\u89c1man\u6587\u6863\u6216\u8005baidu\u3001Google\uff0c\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u5c06\u4f7f\u7528\u5230send\/recv\u3002<\/p>\n

        4.6\u3001close()\u51fd\u6570<\/h3>\n

        \u5728\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\u5efa\u7acb\u8fde\u63a5\u4e4b\u540e\uff0c\u4f1a\u8fdb\u884c\u4e00\u4e9b\u8bfb\u5199\u64cd\u4f5c\uff0c\u5b8c\u6210\u4e86\u8bfb\u5199\u64cd\u4f5c\u5c31\u8981\u5173\u95ed\u76f8\u5e94\u7684socket\u63cf\u8ff0\u5b57\uff0c\u597d\u6bd4\u64cd\u4f5c\u5b8c\u6253\u5f00\u7684\u6587\u4ef6\u8981\u8c03\u7528fclose\u5173\u95ed\u6253\u5f00\u7684\u6587\u4ef6\u3002<\/p>\n

        #include <unistd.h> int close(int fd);<\/code><\/pre>\n

        close\u4e00\u4e2aTCP socket\u7684\u7f3a\u7701\u884c\u4e3a\u65f6\u628a\u8be5socket\u6807\u8bb0\u4e3a\u4ee5\u5173\u95ed\uff0c\u7136\u540e\u7acb\u5373\u8fd4\u56de\u5230\u8c03\u7528\u8fdb\u7a0b\u3002\u8be5\u63cf\u8ff0\u5b57\u4e0d\u80fd\u518d\u7531\u8c03\u7528\u8fdb\u7a0b\u4f7f\u7528\uff0c\u4e5f\u5c31\u662f\u8bf4\u4e0d\u80fd\u518d\u4f5c\u4e3aread\u6216write\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u3002<\/p>\n

        \u6ce8\u610f\uff1aclose\u64cd\u4f5c\u53ea\u662f\u4f7f\u76f8\u5e94socket\u63cf\u8ff0\u5b57\u7684\u5f15\u7528\u8ba1\u6570-1\uff0c\u53ea\u6709\u5f53\u5f15\u7528\u8ba1\u6570\u4e3a0\u7684\u65f6\u5019\uff0c\u624d\u4f1a\u89e6\u53d1TCP\u5ba2\u6237\u7aef\u5411\u670d\u52a1\u5668\u53d1\u9001\u7ec8\u6b62\u8fde\u63a5\u8bf7\u6c42\u3002<\/p>\n

        \n

        \n

        \u4e94. Socket\u4e2dTCP\u7684\u5efa\u7acb\uff08\u4e09\u6b21\u63e1\u624b\uff09<\/span><\/h2>\n
        \n

        TCP\u534f\u8bae\u901a\u8fc7\u4e09\u4e2a\u62a5\u6587\u6bb5\u5b8c\u6210\u8fde\u63a5\u7684\u5efa\u7acb\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a\u4e09\u6b21\u63e1\u624b(three-way handshake)\uff0c\u8fc7\u7a0b\u5982\u4e0b\u56fe\u6240\u793a\u3002<\/p>\n<\/p>\n

        \u7b2c\u4e00\u6b21\u63e1\u624b\uff1a\u5efa\u7acb\u8fde\u63a5\u65f6\uff0c\u5ba2\u6237\u7aef\u53d1\u9001syn\u5305(syn=j)\u5230\u670d\u52a1\u5668\uff0c\u5e76\u8fdb\u5165SYN_SEND\u72b6\u6001\uff0c\u7b49\u5f85\u670d\u52a1\u5668\u786e\u8ba4\uff1bSYN\uff1a\u540c\u6b65\u5e8f\u5217\u7f16\u53f7(Synchronize Sequence Numbers)\u3002<\/p>\n

        \u7b2c\u4e8c\u6b21\u63e1\u624b\uff1a\u670d\u52a1\u5668\u6536\u5230syn\u5305\uff0c\u5fc5\u987b\u786e\u8ba4\u5ba2\u6237\u7684SYN\uff08ack=j+1\uff09\uff0c\u540c\u65f6\u81ea\u5df1\u4e5f\u53d1\u9001\u4e00\u4e2aSYN\u5305\uff08syn=k\uff09\uff0c\u5373SYN+ACK\u5305\uff0c\u6b64\u65f6\u670d\u52a1\u5668\u8fdb\u5165SYN_RECV\u72b6\u6001\uff1b
        \u7b2c\u4e09\u6b21\u63e1\u624b\uff1a\u5ba2\u6237\u7aef\u6536\u5230\u670d\u52a1\u5668\u7684SYN+ACK\u5305\uff0c\u5411\u670d\u52a1\u5668\u53d1\u9001\u786e\u8ba4\u5305ACK(ack=k+1)\uff0c\u6b64\u5305\u53d1\u9001\u5b8c\u6bd5\uff0c\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u8fdb\u5165ESTABLISHED\u72b6\u6001\uff0c\u5b8c\u6210\u4e09\u6b21\u63e1\u624b\u3002
        \u4e00\u4e2a\u5b8c\u6574\u7684\u4e09\u6b21\u63e1\u624b\u4e5f\u5c31\u662f\uff1a \u8bf7\u6c42---\u5e94\u7b54---\u518d\u6b21\u786e\u8ba4\u3002<\/p>\n

        \u5bf9\u5e94\u7684\u51fd\u6570\u63a5\u53e3\uff1a
               \"\u4e3a\u4ec0\u4e48linux\u9002\u5408\u7f16\u7a0b_linux\u57fa\u7840\u77e5\u8bc6\u70b9<\/p>\n<\/p>\n

        \u4ece\u56fe\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u5f53\u5ba2\u6237\u7aef\u8c03\u7528connect<\/span>\u65f6\uff0c\u89e6\u53d1\u4e86\u8fde\u63a5\u8bf7\u6c42\uff0c\u5411\u670d\u52a1\u5668\u53d1\u9001\u4e86SYN J\u5305\uff0c\u8fd9\u65f6connect\u8fdb\u5165\u963b\u585e\u72b6\u6001\uff1b\u670d\u52a1\u5668\u76d1\u542c\u5230\u8fde\u63a5\u8bf7\u6c42\uff0c\u5373\u6536\u5230SYN J\u5305\uff0c\u8c03\u7528accept<\/span>\u51fd\u6570\u63a5\u6536\u8bf7\u6c42\u5411\u5ba2\u6237\u7aef\u53d1\u9001SYN K \uff0cACK J+1\uff0c\u8fd9\u65f6accept\u8fdb\u5165\u963b\u585e\u72b6\u6001\uff1b\u5ba2\u6237\u7aef\u6536\u5230\u670d\u52a1\u5668\u7684SYN K \uff0cACK J+1\u4e4b\u540e\uff0c\u8fd9\u65f6connect\u8fd4\u56de\uff0c\u5e76\u5bf9SYN K\u8fdb\u884c\u786e\u8ba4\uff1b\u670d\u52a1\u5668\u6536\u5230ACK K+1\u65f6\uff0caccept\u8fd4\u56de\uff0c\u81f3\u6b64\u4e09\u6b21\u63e1\u624b\u5b8c\u6bd5\uff0c\u8fde\u63a5\u5efa\u7acb\u3002<\/p>\n<\/p>\n

        \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7f51\u7edc\u6293\u5305\u7684\u67e5\u770b\u5177\u4f53\u7684\u6d41\u7a0b\uff1a<\/p>\n

        \u6bd4\u5982\u6211\u4eec\u670d\u52a1\u5668\u5f00\u542f9502\u7684\u7aef\u53e3\u3002\u4f7f\u7528tcpdump\u6765\u6293\u5305\uff1a<\/p>\n<\/p>\n

         tcpdump -iany tcp port 9502<\/span><\/strong><\/p>\n<\/p>\n

        \u7136\u540e\u6211\u4eec\u4f7f\u7528telnet 127.0.0.1 9502\u5f00\u8fde\u63a5.:<\/p>\n

        telnet 127.0.0.1 9502<\/p>\n<\/p>\n

        14:12:45. IP localhost.39870 > localhost.9502: Flags [S], seq , win 32792, options [mss 16396,sackOK,TS val ecr 0,nop,wscale 3], length 0\uff081\uff09<\/span><\/strong>
        14:12:45. IP localhost.9502 > localhost.39870: Flags [S.], seq , ack , win 32768, options [mss 16396,sackOK,TS val ecr ,nop,wscale 3], length 0  \uff082\uff09<\/strong>
        14:12:45. IP localhost.39870 > localhost.9502: Flags [.], ack 1, win 4099, options [nop,nop,TS val ecr ], length 0  \uff083\uff09<\/strong><\/p>\n

        14:13:01. IP localhost.39870 > localhost.9502: Flags [P.], seq 1:8, ack 1, win 4099, options [nop,nop,TS val ecr ], length 7
        14:13:01. IP localhost.9502 > localhost.39870: Flags [.], ack 8, win 4096, options [nop,nop,TS val ecr ], length 0
        14:13:01. IP localhost.9502 > localhost.39870: Flags [P.], seq 1:19, ack 8, win 4096, options [nop,nop,TS val ecr ], length 18
        14:13:01. IP localhost.39870 > localhost.9502: Flags [.], ack 19, win 4097, options [nop,nop,TS val ecr ], length 0<\/p>\n<\/p>\n

          \n
        • 114:12:45. \u65f6\u95f4\u5e26\u6709\u7cbe\u786e\u5230\u5fae\u5999<\/li>\n
        • localhost.39870 > localhost.9502 \u8868\u793a\u901a\u4fe1\u7684\u6d41\u5411\uff0c39870\u662f\u5ba2\u6237\u7aef\uff0c9502\u662f\u670d\u52a1\u5668\u7aef<\/li>\n
        • [S] \u8868\u793a\u8fd9\u662f\u4e00\u4e2aSYN\u8bf7\u6c42<\/li>\n
        • [S.] \u8868\u793a\u8fd9\u662f\u4e00\u4e2aSYN+ACK\u786e\u8ba4\u5305: <\/span><\/li>\n
        • [.] \u8868\u793a\u8fd9\u662f\u4e00\u4e2aACT\u786e\u8ba4\u5305\uff0c (client)SYN->(server)SYN->(client)ACT \u5c31\u662f3\u6b21\u63e1\u624b\u8fc7\u7a0b<\/li>\n
        • [P] \u8868\u793a\u8fd9\u4e2a\u662f\u4e00\u4e2a\u6570\u636e\u63a8\u9001\uff0c\u53ef\u4ee5\u662f\u4ece\u670d\u52a1\u5668\u7aef\u5411\u5ba2\u6237\u7aef\u63a8\u9001\uff0c\u4e5f\u53ef\u4ee5\u4ece\u5ba2\u6237\u7aef\u5411\u670d\u52a1\u5668\u7aef\u63a8<\/li>\n
        • [F] \u8868\u793a\u8fd9\u662f\u4e00\u4e2aFIN\u5305\uff0c\u662f\u5173\u95ed\u8fde\u63a5\u64cd\u4f5c\uff0cclient\/server\u90fd\u6709\u53ef\u80fd\u53d1\u8d77<\/li>\n
        • [R] \u8868\u793a\u8fd9\u662f\u4e00\u4e2aRST\u5305\uff0c\u4e0eF\u5305\u4f5c\u7528\u76f8\u540c\uff0c\u4f46RST\u8868\u793a\u8fde\u63a5\u5173\u95ed\u65f6\uff0c\u4ecd\u7136\u6709\u6570\u636e\u672a\u88ab\u5904\u7406\u3002\u53ef\u4ee5\u7406\u89e3\u4e3a\u662f\u5f3a\u5236\u5207\u65ad\u8fde\u63a5<\/li>\n
        • win 4099 \u662f\u6307\u6ed1\u52a8\u7a97\u53e3\u5927\u5c0f<\/li>\n
        • length 18\u6307\u6570\u636e\u5305\u7684\u5927\u5c0f<\/li>\n<\/ul>\n<\/p>\n

          \u6211\u4eec\u770b\u5230 \uff081\uff09\uff082\uff09\uff083\uff09\u4e09\u6b65\u662f\u5efa\u7acbtcp\uff1a<\/strong><\/strong><\/strong><\/p>\n

          \u7b2c\u4e00\u6b21\u63e1\u624b\uff1a<\/strong><\/strong><\/strong><\/p>\n

          14:12:45. IP localhost.39870 > localhost.9502: Flags [S], seq <\/p>\n

          \u5ba2\u6237\u7aefIP localhost.39870 (\u5ba2\u6237\u7aef\u7684\u7aef\u53e3\u4e00\u822c\u662f\u81ea\u52a8\u5206\u914d\u7684) \u5411\u670d\u52a1\u5668localhost.9502 <\/span>\u53d1\u9001syn\u5305(syn=j)\u5230\u670d\u52a1\u5668\u300b<\/p>\n

          syn\u5305(syn=j) \uff1a syn\u7684seq=   \uff08j=\uff09<\/p>\n<\/p>\n

          \u7b2c\u4e8c\u6b21\u63e1\u624b\uff1a<\/strong><\/p>\n

          14:12:45. IP localhost.9502 > localhost.39870: Flags [S.], seq , ack ,<\/p>\n

          \u6536\u5230\u8bf7\u6c42\u5e76\u786e\u8ba4\uff1a\u670d\u52a1\u5668\u6536\u5230syn\u5305\uff0c\u5e76\u5fc5\u987b\u786e\u8ba4\u5ba2\u6237\u7684SYN\uff08ack=j+1\uff09\uff0c\u540c\u65f6\u81ea\u5df1\u4e5f\u53d1\u9001\u4e00\u4e2aSYN\u5305\uff08syn=k\uff09\uff0c\u5373SYN+ACK\u5305\uff1a
          \u6b64\u65f6\u670d\u52a1\u5668\u4e3b\u673a\u81ea\u5df1\u7684SYN\uff1aseq\uff1ay= syn seq \u3002
          ACK\u4e3aj+1 =\uff08ack=j+1\uff09=ack  <\/p>\n

           <\/p>\n

          \u7b2c\u4e09\u6b21\u63e1\u624b\uff1a<\/strong><\/p>\n

          14:12:45. IP localhost.39870 > localhost.9502: Flags [.], ack 1,<\/p>\n

          \u5ba2\u6237\u7aef\u6536\u5230\u670d\u52a1\u5668\u7684SYN+ACK\u5305\uff0c\u5411\u670d\u52a1\u5668\u53d1\u9001\u786e\u8ba4\u5305ACK(ack=k+1)<\/p>\n<\/p>\n

          \u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u8fdb\u5165ESTABLISHED\u72b6\u6001\u540e\uff0c\u53ef\u4ee5\u8fdb\u884c\u901a\u4fe1\u6570\u636e\u4ea4\u4e92\u3002\u6b64\u65f6\u548caccept\u63a5\u53e3\u6ca1\u6709\u5173\u7cfb\uff0c\u5373\u4f7f\u6ca1\u6709accepte\uff0c\u4e5f\u8fdb\u884c3\u6b21\u63e1\u624b\u5b8c\u6210\u3002<\/p>\n

          \u8fde\u63a5\u51fa\u73b0\u8fde\u63a5\u4e0d\u4e0a\u7684\u95ee\u9898\uff0c\u4e00\u822c\u662f\u7f51\u8def\u51fa\u73b0\u95ee\u9898\u6216\u8005\u7f51\u5361\u8d85\u8d1f\u8377\u6216\u8005\u662f\u8fde\u63a5\u6570\u5df2\u7ecf\u6ee1\u5566\u3002<\/span><\/p>\n<\/p>\n

          \u7d2b\u8272\u80cc\u666f\u7684\u90e8\u5206\uff1a<\/p>\n

          IP localhost.39870 > localhost.9502: Flags [P.], seq 1:8, ack 1, win 4099, options [nop,nop,TS val ecr ], length 7<\/p>\n

          \u5ba2\u6237\u7aef\u5411\u670d\u52a1\u5668\u53d1\u9001\u957f\u5ea6\u4e3a7\u4e2a\u5b57\u8282\u7684\u6570\u636e\uff0c<\/p>\n<\/p>\n

          IP localhost.9502 > localhost.39870: Flags [.], ack 8, win 4096, options [nop,nop,TS val ecr ], length 0<\/p>\n

          \u670d\u52a1\u5668\u5411\u5ba2\u6237\u786e\u8ba4\u5df2\u7ecf\u6536\u5230\u6570\u636e<\/p>\n<\/p>\n

           IP localhost.9502 > localhost.39870: Flags [P.], seq 1:19, ack 8, win 4096, options [nop,nop,TS val ecr ], length 18<\/p>\n

          \u7136\u540e\u670d\u52a1\u5668\u540c\u65f6\u5411\u5ba2\u6237\u7aef\u5199\u5165\u6570\u636e\u3002<\/p>\n<\/p>\n

           IP localhost.39870 > localhost.9502: Flags [.], ack 19, win 4097, options [nop,nop,TS val ecr ], length 0<\/p>\n

          \u5ba2\u6237\u7aef\u5411\u670d\u52a1\u5668\u786e\u8ba4\u5df2\u7ecf\u6536\u5230\u6570\u636e<\/p>\n<\/p>\n

          \u8fd9\u4e2a\u5c31\u662ftcp\u53ef\u9760\u7684\u8fde\u63a5\uff0c\u6bcf\u6b21\u901a\u4fe1\u90fd\u9700\u8981\u5bf9\u65b9\u6765\u786e\u8ba4\u3002<\/p>\n<\/p>\n

          <\/h3>\n

          \u516d. TCP\u8fde\u63a5\u7684\u7ec8\u6b62\uff08\u56db\u6b21\u63e1\u624b\u91ca\u653e\uff09<\/span><\/h2>\n
          \n

          \u5efa\u7acb\u4e00\u4e2a\u8fde\u63a5\u9700\u8981\u4e09\u6b21\u63e1\u624b\uff0c\u800c\u7ec8\u6b62\u4e00\u4e2a\u8fde\u63a5\u8981\u7ecf\u8fc7\u56db\u6b21\u63e1\u624b\uff0c\u8fd9\u662f\u7531TCP\u7684\u534a\u5173\u95ed(half-close)\u9020\u6210\u7684\uff0c\u5982\u56fe\uff1a<\/p>\n

          \"\u4e3a\u4ec0\u4e48linux\u9002\u5408\u7f16\u7a0b_linux\u57fa\u7840\u77e5\u8bc6\u70b9<\/p>\n

           <\/p>\n

          \n

          \u7531\u4e8eTCP\u8fde\u63a5\u662f\u5168\u53cc\u5de5\u7684\uff0c\u56e0\u6b64\u6bcf\u4e2a\u65b9\u5411\u90fd\u5fc5\u987b\u5355\u72ec\u8fdb\u884c\u5173\u95ed\u3002\u8fd9\u4e2a\u539f\u5219\u662f\u5f53\u4e00\u65b9\u5b8c\u6210\u5b83\u7684\u6570\u636e\u53d1\u9001\u4efb\u52a1\u540e\u5c31\u80fd\u53d1\u9001\u4e00\u4e2aFIN\u6765\u7ec8\u6b62\u8fd9\u4e2a\u65b9\u5411\u7684\u8fde\u63a5\u3002\u6536\u5230\u4e00\u4e2a FIN\u53ea\u610f\u5473\u7740\u8fd9\u4e00\u65b9\u5411\u4e0a\u6ca1\u6709\u6570\u636e\u6d41\u52a8\uff0c\u4e00\u4e2aTCP\u8fde\u63a5\u5728\u6536\u5230\u4e00\u4e2aFIN\u540e\u4ecd\u80fd\u53d1\u9001\u6570\u636e\u3002\u9996\u5148\u8fdb\u884c\u5173\u95ed\u7684\u4e00\u65b9\u5c06\u6267\u884c\u4e3b\u52a8\u5173\u95ed\uff0c\u800c\u53e6\u4e00\u65b9\u6267\u884c\u88ab\u52a8\u5173\u95ed\u3002<\/p>\n

          \uff081\uff09\u5ba2\u6237\u7aefA\u53d1\u9001\u4e00\u4e2aFIN\uff0c\u7528\u6765\u5173\u95ed\u5ba2\u6237A\u5230\u670d\u52a1\u5668B\u7684\u6570\u636e\u4f20\u9001\uff08\u62a5\u6587\u6bb54\uff09\u3002<\/p>\n

          \uff082\uff09\u670d\u52a1\u5668B\u6536\u5230\u8fd9\u4e2aFIN\uff0c\u5b83\u53d1\u56de\u4e00\u4e2aACK\uff0c\u786e\u8ba4\u5e8f\u53f7\u4e3a\u6536\u5230\u7684\u5e8f\u53f7\u52a01\uff08\u62a5\u6587\u6bb55\uff09\u3002\u548cSYN\u4e00\u6837\uff0c\u4e00\u4e2aFIN\u5c06\u5360\u7528\u4e00\u4e2a\u5e8f\u53f7\u3002<\/p>\n

          \uff083\uff09\u670d\u52a1\u5668B\u5173\u95ed\u4e0e\u5ba2\u6237\u7aefA\u7684\u8fde\u63a5\uff0c\u53d1\u9001\u4e00\u4e2aFIN\u7ed9\u5ba2\u6237\u7aefA\uff08\u62a5\u6587\u6bb56\uff09\u3002<\/p>\n

          \uff084\uff09\u5ba2\u6237\u7aefA\u53d1\u56deACK\u62a5\u6587\u786e\u8ba4\uff0c\u5e76\u5c06\u786e\u8ba4\u5e8f\u53f7\u8bbe\u7f6e\u4e3a\u6536\u5230\u5e8f\u53f7\u52a01\uff08\u62a5\u6587\u6bb57\uff09\u3002<\/p>\n

          \u5bf9\u5e94\u51fd\u6570\u63a5\u53e3\u5982\u56fe\uff1a<\/p>\n

          \"\u4e3a\u4ec0\u4e48linux\u9002\u5408\u7f16\u7a0b_linux\u57fa\u7840\u77e5\u8bc6\u70b9<\/p>\n

           <\/p>\n

          \n

          \u8fc7\u7a0b\u5982\u4e0b\uff1a<\/p>\n

            \n
          • \n

            \u67d0\u4e2a\u5e94\u7528\u8fdb\u7a0b\u9996\u5148\u8c03\u7528close<\/span>\u4e3b\u52a8\u5173\u95ed\u8fde\u63a5\uff0c\u8fd9\u65f6TCP\u53d1\u9001\u4e00\u4e2aFIN M\uff1b<\/p>\n<\/li>\n

          • \n

            \u53e6\u4e00\u7aef\u63a5\u6536\u5230FIN M\u4e4b\u540e\uff0c\u6267\u884c\u88ab\u52a8\u5173\u95ed\uff0c\u5bf9\u8fd9\u4e2aFIN\u8fdb\u884c\u786e\u8ba4\u3002\u5b83\u7684\u63a5\u6536\u4e5f\u4f5c\u4e3a\u6587\u4ef6\u7ed3\u675f\u7b26\u4f20\u9012\u7ed9\u5e94\u7528\u8fdb\u7a0b\uff0c\u56e0\u4e3aFIN\u7684\u63a5\u6536\u610f\u5473\u7740\u5e94\u7528\u8fdb\u7a0b\u5728\u76f8\u5e94\u7684\u8fde\u63a5\u4e0a\u518d\u4e5f\u63a5\u6536\u4e0d\u5230\u989d\u5916\u6570\u636e\uff1b<\/p>\n<\/li>\n

          • \n

            \u4e00\u6bb5\u65f6\u95f4\u4e4b\u540e\uff0c\u63a5\u6536\u5230\u6587\u4ef6\u7ed3\u675f\u7b26\u7684\u5e94\u7528\u8fdb\u7a0b\u8c03\u7528close<\/span>\u5173\u95ed\u5b83\u7684socket\u3002\u8fd9\u5bfc\u81f4\u5b83\u7684TCP\u4e5f\u53d1\u9001\u4e00\u4e2aFIN N\uff1b<\/p>\n<\/li>\n

          • \n

            \u63a5\u6536\u5230\u8fd9\u4e2aFIN\u7684\u6e90\u53d1\u9001\u7aefTCP\u5bf9\u5b83\u8fdb\u884c\u786e\u8ba4\u3002<\/p>\n<\/li>\n<\/ul>\n

            \u8fd9\u6837\u6bcf\u4e2a\u65b9\u5411\u4e0a\u90fd\u6709\u4e00\u4e2aFIN\u548cACK\u3002<\/p>\n

            1\uff0e\u4e3a\u4ec0\u4e48\u5efa\u7acb\u8fde\u63a5\u534f\u8bae\u662f\u4e09\u6b21\u63e1\u624b\uff0c\u800c\u5173\u95ed\u8fde\u63a5\u5374\u662f\u56db\u6b21\u63e1\u624b\u5462\uff1f<\/p>\n

            \u8fd9\u662f\u56e0\u4e3a\u670d\u52a1\u7aef\u7684LISTEN\u72b6\u6001\u4e0b\u7684SOCKET\u5f53\u6536\u5230SYN\u62a5\u6587\u7684\u5efa\u8fde\u8bf7\u6c42\u540e\uff0c\u5b83\u53ef\u4ee5\u628aACK\u548cSYN\uff08ACK\u8d77\u5e94\u7b54\u4f5c\u7528\uff0c\u800cSYN\u8d77\u540c\u6b65\u4f5c\u7528\uff09\u653e\u5728\u4e00\u4e2a\u62a5\u6587\u91cc\u6765\u53d1\u9001\u3002\u4f46\u5173\u95ed\u8fde\u63a5\u65f6\uff0c\u5f53\u6536\u5230\u5bf9\u65b9\u7684FIN\u62a5\u6587\u901a\u77e5\u65f6\uff0c\u5b83\u4ec5\u4ec5\u8868\u793a\u5bf9\u65b9\u6ca1\u6709\u6570\u636e\u53d1\u9001\u7ed9\u4f60\u4e86\uff1b\u4f46\u672a\u5fc5\u4f60\u6240\u6709\u7684\u6570\u636e\u90fd\u5168\u90e8\u53d1\u9001\u7ed9\u5bf9\u65b9\u4e86\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5\u672a\u5fc5\u4f1a\u9a6c\u4e0a\u4f1a\u5173\u95edSOCKET,\u4e5f\u5373\u4f60\u53ef\u80fd\u8fd8\u9700\u8981\u53d1\u9001\u4e00\u4e9b\u6570\u636e\u7ed9\u5bf9\u65b9\u4e4b\u540e\uff0c\u518d\u53d1\u9001FIN\u62a5\u6587\u7ed9\u5bf9\u65b9\u6765\u8868\u793a\u4f60\u540c\u610f\u73b0\u5728\u53ef\u4ee5\u5173\u95ed\u8fde\u63a5\u4e86\uff0c\u6240\u4ee5\u5b83\u8fd9\u91cc\u7684ACK\u62a5\u6587\u548cFIN\u62a5\u6587\u591a\u6570\u60c5\u51b5\u4e0b\u90fd\u662f\u5206\u5f00\u53d1\u9001\u7684\u3002<\/p>\n

            \n

            2\uff0e\u4e3a\u4ec0\u4e48TIME_WAIT\u72b6\u6001\u8fd8\u9700\u8981\u7b492MSL\u540e\u624d\u80fd\u8fd4\u56de\u5230CLOSED\u72b6\u6001\uff1f<\/p>\n

            \u8fd9\u662f\u56e0\u4e3a\u867d\u7136\u53cc\u65b9\u90fd\u540c\u610f\u5173\u95ed\u8fde\u63a5\u4e86\uff0c\u800c\u4e14\u63e1\u624b\u76844\u4e2a\u62a5\u6587\u4e5f\u90fd\u534f\u8c03\u548c\u53d1\u9001\u5b8c\u6bd5\uff0c\u6309\u7406\u53ef\u4ee5\u76f4\u63a5\u56de\u5230CLOSED\u72b6\u6001\uff08\u5c31\u597d\u6bd4\u4eceSYN_SEND\u72b6\u6001\u5230ESTABLISH\u72b6\u6001\u90a3\u6837\uff09\uff1b\u4f46\u662f\u56e0\u4e3a\u6211\u4eec\u5fc5\u987b\u8981\u5047\u60f3\u7f51\u7edc\u662f\u4e0d\u53ef\u9760\u7684\uff0c\u4f60\u65e0\u6cd5\u4fdd\u8bc1\u4f60\u6700\u540e\u53d1\u9001\u7684ACK\u62a5\u6587\u4f1a\u4e00\u5b9a\u88ab\u5bf9\u65b9\u6536\u5230\uff0c\u56e0\u6b64\u5bf9\u65b9\u5904\u4e8eLAST_ACK\u72b6\u6001\u4e0b\u7684SOCKET\u53ef\u80fd\u4f1a\u56e0\u4e3a\u8d85\u65f6\u672a\u6536\u5230ACK\u62a5\u6587\uff0c\u800c\u91cd\u53d1FIN\u62a5\u6587\uff0c\u6240\u4ee5\u8fd9\u4e2aTIME_WAIT\u72b6\u6001\u7684\u4f5c\u7528\u5c31\u662f\u7528\u6765\u91cd\u53d1\u53ef\u80fd\u4e22\u5931\u7684ACK\u62a5\u6587\u3002<\/p>\n<\/p>\n

            <\/h3>\n

            \u4e03. Socket\u7f16\u7a0b\u5b9e\u4f8b<\/span><\/h2>\n
            \n

            \u670d\u52a1\u5668\u7aef\uff1a\u4e00\u76f4\u76d1\u542c\u672c\u673a\u76848000\u53f7\u7aef\u53e3\uff0c\u5982\u679c\u6536\u5230\u8fde\u63a5\u8bf7\u6c42\uff0c\u5c06\u63a5\u6536\u8bf7\u6c42\u5e76\u63a5\u6536\u5ba2\u6237\u7aef\u53d1\u6765\u7684\u6d88\u606f\uff0c\u5e76\u5411\u5ba2\u6237\u7aef\u8fd4\u56de\u6d88\u606f\u3002<\/span><\/p>\n<\/p>\n

            \/* File Name: server.c *\/ #include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> #include<sys\/types.h> #include<sys\/socket.h> #include<netinet\/in.h> #define DEFAULT_PORT 8000 #define MAXLINE 4096 int main(int argc, char** argv) { int socket_fd, connect_fd; struct sockaddr_in servaddr; char buff[4096]; int n; \/\/\u521d\u59cb\u5316Socket if( (socket_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ){ printf(\"create socket error: %s(errno: %d)\\n\",strerror(errno),errno); exit(0); } \/\/\u521d\u59cb\u5316 memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY);\/\/IP\u5730\u5740\u8bbe\u7f6e\u6210INADDR_ANY,\u8ba9\u7cfb\u7edf\u81ea\u52a8\u83b7\u53d6\u672c\u673a\u7684IP\u5730\u5740\u3002 servaddr.sin_port = htons(DEFAULT_PORT);\/\/\u8bbe\u7f6e\u7684\u7aef\u53e3\u4e3aDEFAULT_PORT \/\/\u5c06\u672c\u5730\u5730\u5740\u7ed1\u5b9a\u5230\u6240\u521b\u5efa\u7684\u5957\u63a5\u5b57\u4e0a if( bind(socket_fd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1){ printf(\"bind socket error: %s(errno: %d)\\n\",strerror(errno),errno); exit(0); } \/\/\u5f00\u59cb\u76d1\u542c\u662f\u5426\u6709\u5ba2\u6237\u7aef\u8fde\u63a5 if( listen(socket_fd, 10) == -1){ printf(\"listen socket error: %s(errno: %d)\\n\",strerror(errno),errno); exit(0); } printf(\"======waiting for client's request======\\n\"); while(1){ \/\/\u963b\u585e\u76f4\u5230\u6709\u5ba2\u6237\u7aef\u8fde\u63a5\uff0c\u4e0d\u7136\u591a\u6d6a\u8d39CPU\u8d44\u6e90\u3002 if( (connect_fd = accept(socket_fd, (struct sockaddr*)NULL, NULL)) == -1){ printf(\"accept socket error: %s(errno: %d)\",strerror(errno),errno); continue; } \/\/\u63a5\u53d7\u5ba2\u6237\u7aef\u4f20\u8fc7\u6765\u7684\u6570\u636e n = recv(connect_fd, buff, MAXLINE, 0); \/\/\u5411\u5ba2\u6237\u7aef\u53d1\u9001\u56de\u5e94\u6570\u636e if(!fork()){ \/*\u7d2b\u7981\u57ce*\/ if(send(connect_fd, \"Hello,you are connected!\\n\", 26,0) == -1) perror(\"send error\"); close(connect_fd); exit(0); } buff[n] = '\\0'; printf(\"recv msg from client: %s\\n\", buff); close(connect_fd); } close(socket_fd); }<\/code><\/pre>\n<\/p>\n

            \u5ba2\u6237\u7aef\uff1a<\/p>\n

            \n

            \/* File Name: client.c *\/ #include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> #include<sys\/types.h> #include<sys\/socket.h> #include<netinet\/in.h> #define MAXLINE 4096 int main(int argc, char** argv) { int sockfd, n,rec_len; char recvline[4096], sendline[4096]; char buf[MAXLINE]; struct sockaddr_in servaddr; if( argc != 2){ printf(\"usage: .\/client <ipaddress>\\n\"); exit(0); } if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){ printf(\"create socket error: %s(errno: %d)\\n\", strerror(errno),errno); exit(0); } memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(8000); if( inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0){ printf(\"inet_pton error for %s\\n\",argv[1]); exit(0); } if( connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0){ printf(\"connect error: %s(errno: %d)\\n\",strerror(errno),errno); exit(0); } printf(\"send msg to server: \\n\"); fgets(sendline, 4096, stdin); if( send(sockfd, sendline, strlen(sendline), 0) < 0) { printf(\"send msg error: %s(errno: %d)\\n\", strerror(errno), errno); exit(0); } if((rec_len = recv(sockfd, buf, MAXLINE,0)) == -1) { perror(\"recv error\"); exit(1); } buf[rec_len] = '\\0'; printf(\"Received : %s \",buf); close(sockfd); exit(0); }<\/code><\/pre>\n

            inet_pton \u662fLinux\u4e0bIP\u5730\u5740\u8f6c\u6362\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u5c06IP\u5730\u5740\u5728\u201c\u70b9\u5206\u5341\u8fdb\u5236\u201d\u548c\u201c\u6574\u6570\u201d\u4e4b\u95f4\u8f6c\u6362 \uff0c\u662finet_addr\u7684\u6269\u5c55\u3002<\/p>\n<\/p>\n

            int inet_pton(int af, const char *src, void *dst);\/\/\u8f6c\u6362\u5b57\u7b26\u4e32\u5230\u7f51\u7edc\u5730\u5740:<\/code><\/pre>\n

            \u7b2c\u4e00\u4e2a\u53c2\u6570af\u662f\u5730\u5740\u65cf\uff0c\u8f6c\u6362\u540e\u5b58\u5728dst\u4e2d
                af = AF_INET:src\u4e3a\u6307\u5411\u5b57\u7b26\u578b\u7684\u5730\u5740\uff0c\u5373ASCII\u7684\u5730\u5740\u7684\u9996\u5730\u5740\uff08ddd.ddd.ddd.ddd\u683c\u5f0f\u7684\uff09\uff0c\u51fd\u6570\u5c06\u8be5\u5730\u5740\u8f6c\u6362\u4e3ain_addr\u7684\u7ed3\u6784\u4f53\uff0c\u5e76\u590d\u5236\u5728*dst\u4e2d
            \u3000\u3000af =AF_INET6:src\u4e3a\u6307\u5411IPV6\u7684\u5730\u5740\uff0c\u51fd\u6570\u5c06\u8be5\u5730\u5740\u8f6c\u6362\u4e3ain6_addr\u7684\u7ed3\u6784\u4f53\uff0c\u5e76\u590d\u5236\u5728*dst\u4e2d
            \u5982\u679c\u51fd\u6570\u51fa\u9519\u5c06\u8fd4\u56de\u4e00\u4e2a\u8d1f\u503c\uff0c\u5e76\u5c06errno\u8bbe\u7f6e\u4e3aEAFNOSUPPORT\uff0c\u5982\u679c\u53c2\u6570af\u6307\u5b9a\u7684\u5730\u5740\u65cf\u548csrc\u683c\u5f0f\u4e0d\u5bf9\uff0c\u51fd\u6570\u5c06\u8fd4\u56de0\u3002<\/p>\n<\/p>\n

            \u6d4b\u8bd5\uff1a<\/p>\n

            \u7f16\u8bd1server.c<\/p>\n

            gcc -o server server.c<\/p>\n

            \u542f\u52a8\u8fdb\u7a0b\uff1a<\/p>\n

            .\/server<\/p>\n

            \u663e\u793a\u7ed3\u679c\uff1a<\/p>\n

            ======waiting for client's request======<\/p>\n

            \u5e76\u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5\u3002<\/p>\n

            \u7f16\u8bd1 client.c<\/p>\n

            gcc -o client client.c<\/p>\n

            \u5ba2\u6237\u7aef\u53bb\u8fde\u63a5server\uff1a<\/p>\n

            .\/client 127.0.0.1 <\/p>\n

            \u7b49\u5f85\u8f93\u5165\u6d88\u606f<\/p>\n

            \"\u4e3a\u4ec0\u4e48linux\u9002\u5408\u7f16\u7a0b_linux\u57fa\u7840\u77e5\u8bc6\u70b9<\/p>\n

             <\/p>\n

            \u53d1\u9001\u4e00\u6761\u6d88\u606f\uff0c\u8f93\u5165\uff1ac++<\/p>\n

            \"\u4e3a\u4ec0\u4e48linux\u9002\u5408\u7f16\u7a0b_linux\u57fa\u7840\u77e5\u8bc6\u70b9<\/p>\n

             <\/p>\n

            \u6b64\u65f6\u670d\u52a1\u5668\u7aef\u770b\u5230\uff1a<\/p>\n

            \"\u4e3a\u4ec0\u4e48linux\u9002\u5408\u7f16\u7a0b_linux\u57fa\u7840\u77e5\u8bc6\u70b9<\/p>\n

             <\/p>\n

            \u5ba2\u6237\u7aef\u6536\u5230\u6d88\u606f\uff1a<\/p>\n

            \"\u4e3a\u4ec0\u4e48linux\u9002\u5408\u7f16\u7a0b_linux\u57fa\u7840\u77e5\u8bc6\u70b9<\/p>\n

             <\/p>\n

             <\/p>\n

            \u5176\u5b9e\u53ef\u4ee5\u4e0d\u7528client,\u53ef\u4ee5\u4f7f\u7528telnet\u6765\u6d4b\u8bd5\uff1a<\/p>\n

            telnet 127.0.0.1 8000<\/p>\n

            \"\u4e3a\u4ec0\u4e48linux\u9002\u5408\u7f16\u7a0b_linux\u57fa\u7840\u77e5\u8bc6\u70b9<\/p>\n<\/p>\n

            \u6ce8\u610f\uff1a<\/p>\n

            \u5728ubuntu \u7f16\u8bd1\u6e90\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u5934\u6587\u4ef6types.h\u53ef\u80fd\u627e\u4e0d\u5230\u3002
            \u4f7f\u7528dpkg -L libc6-dev | grep types.h \u67e5\u770b\u3002
            \u5982\u679c\u6ca1\u6709\uff0c\u53ef\u4ee5\u4f7f\u7528
            apt-get install libc6-dev\u5b89\u88c5\u3002
            \u5982\u679c\u6709\u4e86\uff0c\u4f46\u4e0d\u5728\/usr\/include\/sys\/\u76ee\u5f55\u4e0b\uff0c\u624b\u52a8\u628a\u8fd9\u4e2a\u6587\u4ef6\u6dfb\u52a0\u5230\u8fd9\u4e2a\u76ee\u5f55\u4e0b\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n

            (\u90e8\u5206\u5185\u5bb9\u6765\u81ea\u5434\u79e6<\/span>\uff1aLinux Socket\u7f16\u7a0b\uff08\u4e0d\u9650Linux\uff09 - \u5434\u79e6 - \u535a\u5ba2\u56ed)<\/p>\n<\/p>\n

            \u611f\u8c22\u60a8\u7684\u652f\u6301\uff0c\u6211\u4f1a\u7ee7\u7eed\u52aa\u529b\u7684! \u626b\u7801\u6253\u8d4f\uff0c\u4f60\u8bf4\u591a\u5c11\u5c31\u591a\u5c11<\/p>\n

            \"\u4e3a\u4ec0\u4e48linux\u9002\u5408\u7f16\u7a0b_linux\u57fa\u7840\u77e5\u8bc6\u70b9         \"\u4e3a\u4ec0\u4e48linux\u9002\u5408\u7f16\u7a0b_linux\u57fa\u7840\u77e5\u8bc6\u70b9<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e3a\u4ec0\u4e48linux\u9002\u5408\u7f16\u7a0b_linux\u57fa\u7840\u77e5\u8bc6\u70b9Linux\u7684SOCKET\u7f16\u7a0b\u8be6\u89e31.\u7f51\u7edc\u4e2d\u8fdb\u7a0b\u4e4b\u95f4\u5982\u4f55\u901a\u4fe1\u8fdb\u7a0b\u901a\u4fe1\u7684\u6982\u5ff5\u6700\u521d\u6765\u6e90\u4e8e\u5355\u673a\u7cfb\u7edf","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/4919"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=4919"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/4919\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=4919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=4919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=4919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}