{"id":151728,"date":"2023-03-21T09:50:53","date_gmt":"2023-03-21T01:50:53","guid":{"rendered":"http:\/\/www.idc.net\/help\/151728\/"},"modified":"2023-03-21T09:50:53","modified_gmt":"2023-03-21T01:50:53","slug":"linux-rpc%e5%bc%80%e5%8f%91%e7%ae%80%e4%bb%8b-linux-rpc-%e5%bc%80%e5%8f%91","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/151728\/","title":{"rendered":"Linux RPC\u5f00\u53d1\u7b80\u4ecb (linux rpc \u5f00\u53d1)"},"content":{"rendered":"<p>\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528(RPC, Remote Procedure Call)\u662f\u4e00\u79cd\u8ba1\u7b97\u673a\u901a\u4fe1\u534f\u8bae\uff0c\u5b83\u5141\u8bb8\u4e00\u4e2a\u7a0b\u5e8f\u5728\u4e00\u53f0\u8ba1\u7b97\u673a\u4e0a\u8c03\u7528\u53e6\u4e00\u4e2a\u8ba1\u7b97\u673a\u4e0a\u7684\u8fdc\u7a0b\u7a0b\u5e8f\uff0c\u800c\u4e0d\u9700\u8981\u8003\u8651\u5e95\u5c42\u7f51\u7edc\u534f\u8bae\u7684\u7ec6\u8282\u3002\u5728Linux\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0cRPC\u662f\u4e00\u4e2a\u652f\u6301\u7c7bUnix\u7cfb\u7edf\u95f4\u901a\u8baf\u7684\u673a\u5236\u3002RPC\u5728Linux\u4e2d\u901a\u5e38\u88ab\u7528\u6765\u5b9e\u73b0\u5206\u5e03\u5f0f\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>RPC\u901a\u4fe1\u6a21\u578b<\/p>\n<p>RPC\u901a\u4fe1\u6a21\u578b\u5206\u4e3a\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\uff0c\u5ba2\u6237\u7aef\u7a0b\u5e8f\u5c06\u8c03\u7528\u8bf7\u6c42\u53d1\u9001\u5230\u670d\u52a1\u7aef\uff0c\u670d\u52a1\u7aef\u63a5\u6536\u8bf7\u6c42\u5e76\u6267\u884c\u9700\u8981\u6267\u884c\u7684\u8fdc\u7a0b\u8fc7\u7a0b\uff0c\u5e76\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002RPC\u6240\u91c7\u7528\u7684\u534f\u8bae\u53ef\u4ee5\u662fTCP\/IP\u3001UDP\u6216B\u7b49\u534f\u8bae\u3002\u8c03\u7528\u8bf7\u6c42\u53ef\u4ee5\u662f\u540c\u6b65\u6216\u5f02\u6b65\uff0c\u540c\u6b65\u8c03\u7528\u5c06\u7b49\u5f85\u670d\u52a1\u7aef\u6267\u884c\u5b8c\u6bd5\u5e76\u8fd4\u56de\u7ed3\u679c\uff0c\u5f02\u6b65\u8c03\u7528\u5219\u5c06\u4e0d\u4f1a\u7b49\u5f85\uff0c\u800c\u662f\u76f4\u63a5\u8fd4\u56de\u3002<\/p>\n<p>RPC\u539f\u7406<\/p>\n<p>\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\u7684\u5b9e\u73b0\u5f88\u590d\u6742\uff0cRPC\u7684\u57fa\u672c\u5b9e\u73b0\u539f\u7406\u662f\u5c06\u672c\u5730\u4f7f\u7528\u7684\u8fdc\u7a0b\u8fc7\u7a0b\u4f5c\u4e3a\u672c\u5730\u8fc7\u7a0b\u8c03\u7528\uff0c\u7531RPC\u7cfb\u7edf\u6765\u8d1f\u8d23\u8fdc\u7a0b\u8fc7\u7a0b\u7684\u8c03\u7528\u548c\u53c2\u6570\u4f20\u9012\u3002RPC\u7cfb\u7edf\u63a5\u6536\u5230\u7a0b\u5e8f\u53d1\u51fa\u7684\u8c03\u7528\u8bf7\u6c42\u540e\uff0c\u6839\u636e\u7a0b\u5e8f\u7684\u8bf7\u6c42\u53d1\u9001\u7ed9\u8fdc\u7aef\u673a\u5668\uff0c\u8fdc\u7aef\u673a\u5668\u5c06\u7a0b\u5e8f\u6267\u884c\u7684\u7ed3\u679c\u4f20\u9012\u7ed9RPC\u7cfb\u7edf\uff0c\u7531RPC\u7cfb\u7edf\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9\u7a0b\u5e8f\u3002\u56e0\u6b64\uff0c\u7a0b\u5e8f\u53ea\u9700\u8981\u50cf\u8c03\u7528\u672c\u5730\u8fc7\u7a0b\u4e00\u6837\u8c03\u7528\u8fdc\u7a0b\u8fc7\u7a0b\u800c\u65e0\u9700\u5173\u5fc3RPC\u7cfb\u7edf\u7684\u5177\u4f53\u5b9e\u73b0\u3002<\/p>\n<p>RPC\u7684\u597d\u5904<\/p>\n<p>1. \u5206\u5e03\u5f0f\u5f00\u53d1\uff1aRPC\u7cfb\u7edf\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u5206\u6210\u82e5\u5e72\u4e2a\u5c0f\u7684\u903b\u8f91\u5355\u5143\uff0c\u6bcf\u4e2a\u903b\u8f91\u5355\u5143\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u8ba1\u7b97\u673a\u4e0a\u72ec\u7acb\u5f00\u53d1\uff0c\u5e76\u901a\u8fc7RPC\u534f\u8bae\u8fde\u63a5\u8d77\u6765\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5927\u578b\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u6548\u7387\u548c\u6027\u80fd\u3002<\/p>\n<p>2. \u591a\u8bed\u8a00\u652f\u6301\uff1aRPC\u534f\u8bae\u53ef\u4ee5\u652f\u6301\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\uff0c\u4f7f\u5f97\u5206\u5e03\u5f0f\u7684\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u91c7\u7528\u4e0d\u540c\u7684\u7f16\u7a0b\u8bed\u8a00\u5b9e\u73b0\u3002<\/p>\n<p>3. \u89e3\u8026\uff1a\u901a\u8fc7RPC\u53ef\u4ee5\u5c06\u5e94\u7528\u7a0b\u5e8f\u7684\u5404\u6a21\u5757\u5206\u6210\u72ec\u7acb\u7684\u5355\u5143\uff0c\u4f7f\u5f97\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u8f7b\u677e\u6269\u5c55\u548c\u4f18\u5316\uff0c\u5b9e\u73b0\u4e86\u677e\u8026\u5408\u7684\u5e94\u7528\u7a0b\u5e8f\u67b6\u6784\u3002<\/p>\n<p>Linux RPC\u5f00\u53d1<\/p>\n<p>\u5728Linux\u7cfb\u7edf\u4e0b\uff0cRPC\u6846\u67b6\u5728\u53ef\u79fb\u690d\u6027\u6709\u4e00\u5b9a\u4fdd\u969c\u7684\u540c\u65f6\uff0c\u80fd\u591f\u8f83\u597d\u5730\u6ee1\u8db3\u5728Linux\u7cfb\u7edf\u73af\u5883\u4e0b\u7684\u5f00\u53d1\u9700\u8981\u3002\u4e0b\u9762\u4ecb\u7ecdLinux RPC\u5f00\u53d1\u4e2d\u7684\u4e00\u4e9b\u76f8\u5173\u77e5\u8bc6\u70b9\u3002<\/p>\n<p>\u4ee3\u7801\u751f\u6210\u5668<\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528RPC\u751f\u6210\u5de5\u5177\u81ea\u52a8\u751f\u6210\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u7684\u4ee3\u7801\uff0c\u4f7f\u5f97\u6548\u7387\u5927\u5e45\u63d0\u9ad8\uff0c\u51cf\u5c11\u9519\u8bef\u7684\u4ea7\u751f\u3002\u5de5\u5177\u53ef\u4ee5\u6839\u636eIDL (interface description language) \u63a5\u53e3\u63cf\u8ff0\u8bed\u8a00\u81ea\u52a8\u751f\u6210\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u7684\u4ee3\u7801\uff0cIDL\u662f\u4e00\u79cd\u89c4\u8303\u7684\u63a5\u53e3\u63cf\u8ff0\u8bed\u8a00\uff0c\u5176\u6587\u4ef6\u5b9a\u4e49\u63cf\u8ff0\u4e86\u4e00\u4e2a\u5904\u4e8e\u5206\u5e03\u5f0f\u73af\u5883\u4e2d\u901a\u8fc7RPC\u4e92\u76f8\u8c03\u7528\u7684\u4e00\u7ec4\u63a5\u53e3\u3002<\/p>\n<p>\u603b\u7ebf\u8fde\u63a5<\/p>\n<p>\u603b\u7ebf\u8fde\u63a5\u63d0\u4f9b\u4e86\u4e00\u79cd\u5efa\u7acbRPC\u901a\u4fe1\u7684\u673a\u5236\uff0c\u4f7f\u5f97\u5728\u7f51\u7edc\u73af\u5883\u4e2d\u6570\u636e\u4f20\u8f93\u5b8c\u6574\u53ef\u9760\u3001\u9ad8\u6548\u3002Linux\u7cfb\u7edf\u4e2d\u652f\u6301\u7684RPC\u603b\u7ebf\u8fde\u63a5\u5305\u62ecThe Sun Ttie-RPC\u548cNFS(RPC protocol used for network file sharing)\uff0cThe Sun Ttie-RPC\u76ee\u524d\u5df2\u6210\u4e3aLinux\u4e0b\u5f00\u53d1RPC\u5fc5\u5907\u7684\u4e00\u79cd\u901a\u4fe1\u65b9\u5f0f\u3002<\/p>\n<p>\u7f51\u7edc\u7ed1\u5b9a<\/p>\n<p>\u7f51\u7edc\u7ed1\u5b9a\u7528\u4e8e\u5c06RPC\u5b88\u62a4\u8fdb\u7a0b\u4e0e\u6307\u5b9a\u7684\u7aef\u53e3\u7ed1\u5b9a\uff0c\u4f7f\u5f97\u5b88\u62a4\u8fdb\u7a0b\u53ef\u4ee5\u76d1\u542c\u6307\u5b9a\u7684\u7f51\u7edc\u5730\u5740\u548c\u7aef\u53e3\uff0c\u4ece\u800c\u63a5\u53d7\u8bf7\u6c42\u5e76\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\uff0c\u8fd4\u56de\u7ed3\u679c\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u662fRPC\u670d\u52a1\u542f\u52a8\u7684\u57fa\u7840\u3002<\/p>\n<p>\u7ebf\u7a0b\u5b89\u5168<\/p>\n<p>\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\uff0cRPC\u670d\u52a1\u5668\u9700\u8981\u8003\u8651\u7ebf\u7a0b\u5b89\u5168\u6027\u3002\u907f\u514d\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u540c\u4e00\u8d44\u6e90\u5bfc\u81f4\u6570\u636e\u6df7\u4e71\u3002Linux RPC\u7684\u5b9e\u73b0\u4e2d\uff0c\u5e38\u5e38\u4f1a\u4f7f\u7528POSIX\u7ebf\u7a0b\u5e93\u63d0\u4f9b\u7684\u4e92\u65a5\u9501\u3001\u8bfb\u5199\u9501\u7b49\u673a\u5236\u6765\u63a7\u5236\u7ebf\u7a0b\u8bbf\u95ee\u3002<\/p>\n<p>\u5728\u5927\u6570\u636e\u65f6\u4ee3\uff0c\u5206\u5e03\u5f0f\u5e94\u7528\u8d8a\u6765\u8d8a\u53d7\u5230\u91cd\u89c6\u3002RPC\u7684\u5b9e\u73b0\u53ef\u4ee5\u5c06\u5e94\u7528\u7a0b\u5e8f\u5206\u6210\u82e5\u5e72\u4e2a\u5c0f\u903b\u8f91\u5355\u5143\uff0c\u9632\u6b62\u4ee3\u7801\u8026\u5408\u548c\u6570\u636e\u6df7\u4e71\uff0c\u63d0\u9ad8\u4ee3\u7801\u590d\u7528\u548c\u5f00\u53d1\u6548\u7387\u3002\u800c\u5728Linux\u7cfb\u7edf\u4e0b\uff0c\u53ef\u5229\u7528Linux RPC\u4f5c\u4e3a\u5206\u5e03\u5f0f\u5e94\u7528\u7a0b\u5e8f\u7684\u901a\u4fe1\u65b9\u5f0f\uff0c\u5904\u7406\u6570\u636e\u91cf\u5de8\u5927\u7684\u60c5\u51b5\uff0c\u5b9e\u73b0\u66f4\u4f18\u6027\u80fd\u3002Linux RPC\u7684\u5f00\u53d1\u9700\u8981\u638c\u63e1\u57fa\u672c\u7684\u77e5\u8bc6\u70b9\u5982\u4ee3\u7801\u751f\u6210\u5668\u3001\u7f51\u7edc\u7ed1\u5b9a\u3001\u603b\u7ebf\u8fde\u63a5\u548c\u7ebf\u7a0b\u5b89\u5168\u7b49\uff0c\u540c\u65f6\u638c\u63e1RPC\u7684\u597d\u5904\uff0c\u5e76\u7075\u6d3b\u4f7f\u7528\uff0c\u5c31\u80fd\u4e3a\u5206\u5e03\u5f0f\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u63d0\u4f9b\u5f3a\u6709\u529b\u7684\u652f\u6491\u3002<\/p>\n<p><strong>\u76f8\u5173\u95ee\u9898\u62d3\u5c55\u9605\u8bfb\uff1a<\/strong><\/p>\n<ul>\n<li>\u600e\u4e48\u914d\u7f6elinux\u7684cron\u8ba1\u5212<\/li>\n<li>\u6c42\u524d\u8f88\u6307\u6559\u3002linux\u7684\u5957\u63a5\u5b57\u7f16\u7a0b\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u8fd0\u884c\uff0c\u6211\u7167\u7740\u6e90\u7801\u6572\uff0c\u51fa\u73b0\u4e00\u5806\u9519\u8bef\u3002\u7528\u7684\u662fvim\u548cgcc<\/li>\n<\/ul>\n<h3>\u600e\u4e48\u914d\u7f6elinux\u7684cron\u8ba1\u5212<\/h3>\n<p>\u5728\u5907\u578b\u69d0\u7ec8\u7aef\uff1a<\/p>\n<p>\u8f93\u5165crontab<\/p>\n<p>*<\/p>\n<p>*<\/p>\n<p>\/\u79df\u5219in\/shutdown<\/p>\n<p>-h<\/p>\n<p>now<\/p>\n<p>\u201cFOR<\/p>\n<p>MAINTANCE\u201d<\/p>\n<p>\u6309ctrl+d\u4fdd\u5b58\u9000\u51fa<\/p>\n<p>\u63a5\u7740\u8fd0\u884c\uff1a<\/p>\n<p>service<\/p>\n<p>portmap<\/p>\n<p>restart <\/p>\n<p>\u91cd\u65b0\u542f\u52a8RPC\u670d\u52a1\uff0c\u9ed8\u8ba4\u662f\u542f\u52a8\u7684\u3002<\/p>\n<p>service<\/p>\n<p>crond<\/p>\n<p>restart <\/p>\n<p>\u91cd\u65b0\u542f\u52a8crond\u670d\u52a1\uff0c\u5fc5\u987b\u7684\uff01\u8ba9\u4f60\u4e0a\u4eff\u53cb\u9762\u7684\u914d\u7f6e\u751f\u6548\uff01<\/p>\n<p>\u4f60\u5728\u63a7\u5236\u53f0\u4e5f\u5c31\u662f\u7ec8\u7aef\u4e0a\u6267\u884c\u6a58\u4ea9\u952e<\/p>\n<p>crontab<\/p>\n<p>-e<\/p>\n<p>\u7136\u540e\u4f1a\u51fa\u6765\u4e00\u4e2a\u7f16\u8f91\u753b\u9762\u4f60\u5c31\u8f93\u5165<\/p>\n<p>*<\/p>\n<p>*<\/p>\n<p>\/in\/shutdown<\/p>\n<p>-h<\/p>\n<p>now<\/p>\n<p>\u201cFOR<\/p>\n<p>MAINTANCE\u201d<\/p>\n<p>\u7136\u540ewq\u4fdd\u5706\u5de7\u6301\u8010\u6a21\u9000\u51fa<\/p>\n<p>service<\/p>\n<p>portmap<\/p>\n<p>start<\/p>\n<p>service<\/p>\n<p>crond<\/p>\n<p>start<\/p>\n<p>\u4f46\u8fd9\u4e24\u4e2a\u4e0d\u77e5\u9053\u6709\u4ec0\u4e48\u7528\uff0c\u548ccrontab<\/p>\n<p>\u6ca1\u5173\u7cfb\u7684<\/p>\n<p>\u8f93\u5165crontab<\/p>\n<p>*<\/p>\n<p>*<\/p>\n<p>\/in\/shutdown<\/p>\n<p>-h<\/p>\n<p>now<\/p>\n<p>\u201cFOR<\/p>\n<p>MAINTANCE\u201d<\/p>\n<p>service<\/p>\n<p>portmap<\/p>\n<p>start<\/p>\n<p>service<\/p>\n<p>crond<\/p>\n<p>start<\/p>\n<p>\u8c22\u8c22\u5982\u679c\u60a8\u89c9\u5f97\u548c\u95f9\u62ff\u6ee1\u610f\uff0c\u8bf7\u60a8\u91c7\u7eb3\uff01\u7ed9\u4e88\u52a0\u5206\uff01\u5982\u679c\u56de\u7b54\u611f\u89c9\u4e0d\u8be6\u5524\u642d\u7ec6\u5f2f\u96c0\u8bf7\u54a8\u8be2\uff01<\/p>\n<h3>\u6c42\u524d\u8f88\u6307\u6559\u3002linux\u7684\u5957\u63a5\u5b57\u7f16\u7a0b\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u8fd0\u884c\uff0c\u6211\u7167\u7740\u6e90\u7801\u6572\uff0c\u51fa\u73b0\u4e00\u5806\u9519\u8bef\u3002\u7528\u7684\u662fvim\u548cgcc<\/h3>\n<p>\u518d\u542f\u52a8\u65f6\u4f1a\u51fa\u73b0:<\/p>\n<p>Bind(): Address already in use<\/p>\n<p>\u7684\u9519\u8bef\u63d0\u793a\uff0c\u5e76\u5bfc\u81f4\u7a0b\u5e8f\u76f4\u63a5\u9000\u51fa; <\/p>\n<p>\u7528<\/p>\n<p>$netstat -an |grep 8080<\/p>\n<p>\u6216<\/p>\n<p>$ps aux |grep 8080<\/p>\n<p>\u90fd\u8fd8\u80fd\u770b\u5230\u521a\u624d\u7528Ctrl+C\u201c\u5f3a\u5236\u7ed3\u675f\u201d\u4e86\u7684\u8fdb\u7a0b\uff0c\u7aef\u53e3\u8fd8\u662f\u4f7f\u7528\u4e2d\uff0c<\/p>\n<p>\u53ea\u80fd\u7528kill\u7ed3\u675f\u8fdb\u7a0b\uff0c\u624d\u80fd\u6536\u56de\u7aef\u53e3\uff0c\u5f88\u662f\u9ebb\u70e6\u3002<\/p>\n<p>\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0:<\/p>\n<p>int optval\uff1b<\/p>\n<p>optval = 1;<\/p>\n<p>ret = setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, &amp;optval, sizeof(optval) );<\/p>\n<p>\u53ef\u4ee5\u89e3\u51b3\u8fd9\u95ee\u9898\u3002<\/p>\n<p>\u5728\u7f51\u4e0a\u67e5\u5230\u7684\u66f4\u597d\u7684\u89e3\u91ca\u5982\u4e0b:<\/p>\n<\/p>\n<p>\u5728 4.2 BSD UNIX\u00ae \u64cd\u4f5c\u7cfb\u7edf\u4e2d\u9996\u6b21\u5f15\u5165\uff0cSockets API \u73b0\u5728\u662f\u4efb\u4f55\u64cd\u4f5c\u7cfb\u7edf\u7684\u6807\u51c6\u7279\u6027\u3002<\/p>\n<p>\u4e8b\u5b9e\u4e0a\uff0c\u5f88\u96be\u627e\u5230\u4e00\u79cd\u4e0d\u652f\u6301 Sockets API \u7684\u73b0\u4ee3\u8bed\u8a00\u3002<\/p>\n<p>\u8be5 API \u76f8\u5f53\u7b80\u5355\uff0c\u4f46\u65b0\u7684\u5f00\u53d1\u4eba\u5458\u4ecd\u7136\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u7684\u9690\u60a3\u3002<\/p>\n<p>\u672c\u6587\u8bc6\u522b\u90a3\u4e9b\u9690\u60a3\u5e76\u5411\u60a8\u663e\u793a\u5982\u4f55\u907f\u5f00\u5b83\u4eec\u3002<\/p>\n<p>\u9690\u60a3 1\uff0e\u5ffd\u7565\u8fd4\u56de\u72b6\u6001<\/p>\n<p>\u4e4b\u4e00\u4e2a\u9690\u60a3\u5f88\u660e\u663e\uff0c\u4f46\u5b83\u662f\u5f00\u53d1\u65b0\u624b\u6700\u5bb9\u6613\u72af\u7684\u4e00\u4e2a\u9519\u8bef\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5ffd\u7565\u51fd\u6570\u7684\u8fd4\u56de\u72b6\u6001\uff0c\u5f53\u5b83\u4eec\u5931\u8d25\u6216\u90e8\u5206\u6210\u529f\u7684\u65f6\u5019\uff0c\u60a8\u4e5f\u8bb8\u4f1a\u8ff7\u5931\u3002<\/p>\n<p>\u53cd\u8fc7\u6765\uff0c\u8fd9\u53ef\u80fd\u4f20\u64ad\u9519\u8bef\uff0c\u4f7f\u5b9a\u4f4d\u95ee\u9898\u7684\u6e90\u5934\u53d8\u5f97\u56f0\u96be\u3002<\/p>\n<p>\u6355\u83b7\u5e76\u68c0\u67e5\u6bcf\u4e00\u4e2a\u8fd4\u56de\u72b6\u6001\uff0c\u800c\u4e0d\u662f\u5ffd\u7565\u5b83\u4eec\u3002<\/p>\n<p>\u8003\u8651\u6e05\u5355 1 \u663e\u793a\u7684\u4f8b\u5b50\uff0c\u4e00\u4e2a\u5957\u63a5\u5b57 send \u51fd\u6570\u3002<\/p>\n<p>\u6e05\u5355 1. \u5ffd\u7565 API \u51fd\u6570\u8fd4\u56de\u72b6\u6001<\/p>\n<p>int status, sock, mode; <\/p>\n<p>\/* Create a new stream (TCP) socket *\/ <\/p>\n<p>sock = socket( AF_INET, SOCK_STREAM, 0 );<\/p>\n<p> \u2026 <\/p>\n<p>status = send( sock, buffer, buflen, MSG_DONTWAIT ); <\/p>\n<p>if (status == -1)<\/p>\n<p>{ <\/p>\n<p> \/* send failed *\/ <\/p>\n<p> printf( \u201csend failed: %s\\n\u201d, strerror(errno) ); <\/p>\n<p>} <\/p>\n<p>else<\/p>\n<p>{<\/p>\n<p> \/* send succeeded \u2014 or did it? *\/ <\/p>\n<p>} <\/p>\n<p>\u6e05\u5355 1 \u63a2\u7a76\u4e00\u4e2a\u51fd\u6570\u7247\u65ad\uff0c\u5b83\u5b8c\u6210\u5957\u63a5\u5b57 send \u64cd\u4f5c\uff08\u901a\u8fc7\u5957\u63a5\u5b57\u53d1\u9001\u6570\u636e\uff09\u3002<\/p>\n<p>\u51fd\u6570\u7684\u9519\u8bef\u72b6\u6001\u88ab\u6355\u83b7\u5e76\u6d4b\u8bd5\uff0c\u4f46\u8fd9\u4e2a\u4f8b\u5b50\u5ffd\u7565\u4e86send \u5728\u65e0\u963b\u585e\u6a21\u5f0f\uff08\u7531 MSG_DONTWAIT \u6807\u5fd7\u542f\u7528\uff09\u4e0b\u7684\u4e00\u4e2a\u7279\u6027\u3002<\/p>\n<p>send API \u51fd\u6570\u6709\u4e09\u7c7b\u53ef\u4e8f\u518c\u69d0\u80fd\u7684\u8fd4\u56de\u503c\uff1a<\/p>\n<p>\u5982\u679c\u6570\u636e\u6210\u529f\u5730\u6392\u5230\u4f20\u8f93\u961f\u5217\uff0c\u5219\u8fd4\u56de 0\u3002<\/p>\n<p>\u5982\u679c\u6392\u961f\u5931\u8d25\uff0c\u5219\u8fd4\u56de -1\uff08\u901a\u8fc7\u4f7f\u7528 errno \u53d8\u91cf\u53ef\u4ee5\u4e86\u89e3\u5931\u8d25\u7684\u539f\u56e0\uff09\u3002<\/p>\n<p>\u5982\u679c\u4e0d\u662f\u6240\u6709\u7684\u5b57\u7b26\u90fd\u80fd\u591f\u5728\u51fd\u6570\u8c03\u7528\u65f6\u6392\u961f\uff0c\u5219\u6700\u7ec8\u7684\u8fd4\u56de\u503c\u662f\u53d1\u9001\u7684\u5b57\u7b26\u6570\u3002<\/p>\n<p>\u7531\u4e8e send \u7684 MSG_DONTWAIT \u53d8\u91cf\u7684\u65e0\u963b\u585e\u6027\u8d28\uff0c<\/p>\n<p>\u51fd\u6570\u8c03\u7528\u5728\u53d1\u9001\u5b8c\u6240\u6709\u7684\u6570\u636e\u3001\u4e00\u4e9b\u6570\u636e\u6216\u6ca1\u6709\u53d1\u9001\u4efb\u4f55\u6570\u636e\u540e\u8fd4\u56de\u3002<\/p>\n<p>\u5728\u8fd9\u91cc\u5ffd\u7565\u8fd4\u56de\u72b6\u6001\u5c06\u5bfc\u81f4\u4e0d\u5b8c\u5168\u7684\u53d1\u9001\u548c\u968f\u540e\u7684\u6570\u636e\u4e22\u5931\u3002<\/p>\n<p>\u9690\u60a3 2\uff0e\u5bf9\u59ff\u6e90\u7b49\u5957\u63a5\u5b57\u95ed\u5305<\/p>\n<p>UNIX \u6709\u8da3\u7684\u4e00\u9762\u662f\u60a8\u51e0\u4e4e\u53ef\u4ee5\u628a\u4efb\u4f55\u4e1c\u897f\u770b\u6210\u662f\u4e00\u4e2a\u6587\u9500\u53cb\u4ef6\u3002<\/p>\n<p>\u6587\u4ef6\u672c\u8eab\u3001\u76ee\u5f55\u3001\u7ba1\u9053\u3001\u8bbe\u5907\u548c\u5957\u63a5\u5b57\u90fd\u88ab\u5f53\u4f5c\u6587\u4ef6\u3002<\/p>\n<p>\u8fd9\u662f\u65b0\u9896\u7684\u62bd\u8c61\uff0c\u610f\u5473\u7740\u4e00\u6574\u5957\u7684 API \u53ef\u4ee5\u7528\u5728\u5e7f\u6cdb\u7684\u8bbe\u5907\u7c7b\u578b\u4e0a\u3002<\/p>\n<p>\u8003\u8651 read API \u51fd\u6570\uff0c\u5b83\u4ece\u6587\u4ef6\u8bfb\u53d6\u4e00\u5b9a\u6570\u91cf\u7684\u5b57\u8282\u3002<\/p>\n<p>read \u51fd\u6570\u8fd4\u56de\uff1a<\/p>\n<p>\u8bfb\u53d6\u7684\u5b57\u8282\u6570\uff08\u66f4\u9ad8\u4e3a\u60a8\u6307\u5b9a\u7684\u66f4\u5927\u503c\uff09\uff1b<\/p>\n<p>\u6216\u8005 -1\uff0c\u8868\u793a\u9519\u8bef\uff1b<\/p>\n<p>\u6216\u8005 0\uff0c\u5982\u679c\u5df2\u7ecf\u5230\u8fbe\u6587\u4ef6\u672b\u5c3e\u3002<\/p>\n<p>\u5982\u679c\u5728\u4e00\u4e2a\u5957\u63a5\u5b57\u4e0a\u5b8c\u6210\u4e00\u4e2a read \u64cd\u4f5c\u5e76\u5f97\u5230\u4e00\u4e2a\u4e3a 0 \u7684\u8fd4\u56de\u503c\uff0c\u8fd9\u8868\u660e\u8fdc\u7a0b\u5957\u63a5\u5b57\u7aef\u7684\u5bf9\u7b49\u5c42\u8c03\u7528\u4e86 close API \u65b9\u6cd5\u3002<\/p>\n<p>\u8be5\u6307\u793a\u4e0e\u6587\u4ef6\u8bfb\u53d6\u76f8\u540c \u2014\u2014 \u6ca1\u6709\u591a\u4f59\u7684\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u63cf\u8ff0\u7b26\u8bfb\u53d6\uff08\u53c2\u89c1 \u6e05\u5355 2\uff09\u3002<\/p>\n<p>\u6e05\u5355 2\uff0e\u9002\u5f53\u5904\u7406 read API \u51fd\u6570\u7684\u8fd4\u56de\u503c<\/p>\n<p>int sock, status; <\/p>\n<p>sock = socket( AF_INET, SOCK_STREAM, 0 ); <\/p>\n<p>\u2026 <\/p>\n<p>status = read( sock, buffer, buflen ); <\/p>\n<p>if (status &gt; 0)<\/p>\n<p> { <\/p>\n<p> \/* Data read from the socket *\/ <\/p>\n<p>} <\/p>\n<p>else if (status == -1) <\/p>\n<p>{ <\/p>\n<p> \/* Error, check errno, take action\u2026 *\/ <\/p>\n<p>} <\/p>\n<p>else if (status == 0) <\/p>\n<p>{ <\/p>\n<p> \/* Peer closed the socket, finish the close *\/<\/p>\n<p> close( sock );<\/p>\n<p> \/* Further processing\u2026 *\/ <\/p>\n<p>} <\/p>\n<p>\u540c\u6837\uff0c\u53ef\u4ee5\u7528 write API \u51fd\u6570\u6765\u63a2\u6d4b\u5bf9\u7b49\u5957\u63a5\u5b57\u7684\u95ed\u5305\u3002<\/p>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u63a5\u6536 SIGPIPE \u4fe1\u53f7\uff0c\u6216\u5982\u679c\u8be5\u4fe1\u53f7\u963b\u585e\uff0cwrite \u51fd\u6570\u5c06\u8fd4\u56de -1 \u5e76\u8bbe\u7f6e errno \u4e3a EPIPE\u3002<\/p>\n<p>\u9690\u60a3 3\uff0e\u5730\u5740\u4f7f\u7528\u9519\u8bef\uff08EADDRINUSE\uff09<\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 bind API \u51fd\u6570\u6765\u7ed1\u5b9a\u4e00\u4e2a\u5730\u5740\uff08\u4e00\u4e2a\u63a5\u53e3\u548c\u4e00\u4e2a\u7aef\u53e3\uff09\u5230\u4e00\u4e2a\u5957\u63a5\u5b57\u7aef\u70b9\u3002<\/p>\n<p>\u53ef\u4ee5\u5728\u670d\u52a1\u5668\u8bbe\u7f6e\u4e2d\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u4ee5\u4fbf\u9650\u5236\u53ef\u80fd\u6709\u8fde\u63a5\u5230\u6765\u7684\u63a5\u53e3\u3002<\/p>\n<p>\u4e5f\u53ef\u4ee5\u5728\u5ba2\u6237\u7aef\u8bbe\u7f6e\u4e2d\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u4ee5\u4fbf\u9650\u5236\u5e94\u5f53\u4f9b\u51fa\u53bb\u7684\u8fde\u63a5\u6240\u4f7f\u7528\u7684\u63a5\u53e3\u3002<\/p>\n<p>bind \u6700\u5e38\u89c1\u7684\u7528\u6cd5\u662f\u5173\u8054\u7aef\u53e3\u53f7\u548c\u670d\u52a1\u5668\uff0c\u5e76\u4f7f\u7528\u901a\u914d\u7b26\u5730\u5740\uff08INADDR_ANY\uff09\uff0c\u5b83\u5141\u8bb8\u4efb\u4f55\u63a5\u53e3\u4e3a\u5230\u6765\u7684\u8fde\u63a5\u6240\u4f7f\u7528\u3002<\/p>\n<p>bind \u666e\u904d\u906d\u9047\u7684\u95ee\u9898\u662f\u8bd5\u56fe\u7ed1\u5b9a\u4e00\u4e2a\u5df2\u7ecf\u5728\u4f7f\u7528\u7684\u7aef\u53e3\u3002<\/p>\n<p>\u8be5\u9677\u9631\u662f\u4e5f\u8bb8\u6ca1\u6709\u6d3b\u52a8\u7684\u5957\u63a5\u5b57\u5b58\u5728\uff0c\u4f46\u4ecd\u7136\u7981\u6b62\u7ed1\u5b9a\u7aef\u53e3\uff08bind \u8fd4\u56deEADDRINUSE\uff09\uff0c<\/p>\n<p>\u5b83\u7531 TCP \u5957\u63a5\u5b57\u72b6\u6001 TIME_WAIT \u5f15\u8d77\u3002<\/p>\n<p>\u8be5\u72b6\u6001\u5728\u5957\u63a5\u5b57\u5173\u95ed\u540e\u7ea6\u4fdd\u7559 2 \u5230 4 \u5206\u949f\u3002<\/p>\n<p>\u5728 TIME_WAIT \u72b6\u6001\u9000\u51fa\u4e4b\u540e\uff0c\u5957\u63a5\u5b57\u88ab\u5220\u9664\uff0c\u8be5\u5730\u5740\u624d\u80fd\u88ab\u91cd\u65b0\u7ed1\u5b9a\u800c\u4e0d\u51fa\u95ee\u9898\u3002<\/p>\n<p>\u7b49\u5f85 TIME_WAIT \u7ed3\u675f\u53ef\u80fd\u662f\u4ee4\u4eba\u607c\u706b\u7684\u4e00\u4ef6\u4e8b\uff0c<\/p>\n<p>\u7279\u522b\u662f\u5982\u679c\u60a8\u6b63\u5728\u5f00\u53d1\u4e00\u4e2a\u5957\u63a5\u5b57\u670d\u52a1\u5668\uff0c\u5c31\u9700\u8981\u505c\u6b62\u670d\u52a1\u5668\u6765\u505a\u4e00\u4e9b\u6539\u52a8\uff0c\u7136\u540e\u91cd\u542f\u3002<\/p>\n<p>\u5e78\u8fd0\u7684\u662f\uff0c\u6709\u65b9\u6cd5\u53ef\u4ee5\u907f\u5f00 TIME_WAIT \u72b6\u6001\u3002\u53ef\u4ee5\u7ed9\u5957\u63a5\u5b57\u5e94\u7528 SO_REUSEADDR \u5957\u63a5\u5b57\u9009\u9879\uff0c\u4ee5\u4fbf\u7aef\u53e3\u53ef\u4ee5\u9a6c\u4e0a\u91cd\u7528\u3002<\/p>\n<p>\u8003\u8651\u6e05\u5355 3 \u7684\u4f8b\u5b50\u3002<\/p>\n<p>\u5728\u7ed1\u5b9a\u5730\u5740\u4e4b\u524d\uff0c\u6211\u4ee5 SO_REUSEADDR \u9009\u9879\u8c03\u7528 setsockopt\u3002<\/p>\n<p>\u4e3a\u4e86\u5141\u8bb8\u5730\u5740\u91cd\u7528\uff0c\u6211\u8bbe\u7f6e\u6574\u578b\u53c2\u6570\uff08on\uff09\u4e3a 1 \uff08\u4e0d\u7136\uff0c\u53ef\u4ee5\u8bbe\u4e3a 0 \u6765\u7981\u6b62\u5730\u5740\u91cd\u7528\uff09\u3002<\/p>\n<p>\u6e05\u5355 3\uff0e\u4f7f\u7528 SO_REUSEADDR \u5957\u63a5\u5b57\u9009\u9879\u907f\u514d\u5730\u5740\u4f7f\u7528\u9519\u8bef<\/p>\n<p>int sock, ret, on; struct sockaddr_in servaddr; \/* Create a new stream (TCP) socket *\/ sock = socket( AF_INET, SOCK_STREAM, 0 ): <\/p>\n<p>\/* Enable address reuse *\/ <\/p>\n<p>on = 1;<\/p>\n<p> ret = setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, &amp;on, sizeof(on) ); <\/p>\n<p>\/* Allow connections to port 8080 from any available interface *\/ <\/p>\n<p>memset( &amp;servaddr, 0, sizeof(servaddr) ); <\/p>\n<p>servaddr.sin_family = AF_INET; <\/p>\n<p>servaddr.sin_addr.s_addr = htonl( INADDR_ANY ); <\/p>\n<p>servaddr.sin_port = htons(); <\/p>\n<p>\/* Bind to the address (interface\/port) *\/ <\/p>\n<p>ret = bind( sock, (struct sockaddr *)&amp;servaddr, sizeof(servaddr) ); <\/p>\n<p>\u5728\u5e94\u7528\u4e86 SO_REUSEADDR \u9009\u9879\u4e4b\u540e\uff0cbind API \u51fd\u6570\u5c06\u5141\u8bb8\u5730\u5740\u7684\u7acb\u5373\u91cd\u7528\u3002<\/p>\n<p>\u9690\u60a3 4\uff0e\u53d1\u9001\u7ed3\u6784\u5316\u6570\u636e<\/p>\n<p>\u5957\u63a5\u5b57\u662f\u53d1\u9001\u65e0\u7ed3\u6784\u4e8c\u8fdb\u5236\u5b57\u8282\u6d41\u6216 ASCII \u6570\u636e\u6d41\uff08\u6bd4\u5982 HTTP \u4e0a\u7684 HTTP \u9875\u9762\uff0c\u6216 TP \u4e0a\u7684\u7535\u5b50\u90ae\u4ef6\uff09\u7684\u5b8c\u7f8e\u5de5\u5177\u3002\u4f46\u662f\u5982\u679c\u8bd5\u56fe\u5728\u4e00\u4e2a\u5957\u63a5\u5b57\u4e0a\u53d1\u9001\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u4e8b\u60c5\u5c06\u4f1a\u53d8\u5f97\u66f4\u52a0\u590d\u6742\u3002<\/p>\n<p>\u6bd4\u5982\u8bf4\uff0c\u60a8\u60f3\u8981\u53d1\u9001\u4e00\u4e2a\u6574\u6570\uff1a\u60a8\u53ef\u4ee5\u80af\u5b9a\uff0c\u63a5\u6536\u8005\u5c06\u4f7f\u7528\u540c\u6837\u7684\u65b9\u5f0f\u6765\u89e3\u91ca\u8be5\u6574\u6570\u5417\uff1f<\/p>\n<p>\u8fd0\u884c\u5728\u540c\u4e00\u67b6\u6784\u4e0a\u7684\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u4f9d\u8d56\u5b83\u4eec\u5171\u540c\u7684\u5e73\u53f0\u6765\u5bf9\u8be5\u7c7b\u578b\u7684\u6570\u636e\u505a\u51fa\u76f8\u540c\u7684\u89e3\u91ca\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u5982\u679c\u4e00\u4e2a\u8fd0\u884c\u5728\u9ad8\u4f4d\u4f18\u5148\u7684 IBM PowerPC \u4e0a\u7684\u5ba2\u6237\u7aef\u53d1\u9001\u4e00\u4e2a 32 \u4f4d\u7684\u6574\u6570\u5230\u4e00\u4e2a\u4f4e\u4f4d\u4f18\u5148\u7684 Intel x86\uff0c<\/p>\n<p>\u90a3\u5c06\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f<\/p>\n<p>\u5b57\u8282\u6392\u5217\u5c06\u5f15\u8d77\u4e0d\u6b63\u786e\u7684\u89e3\u91ca\u3002<\/p>\n<p>\u5b57\u8282\u4ea4\u6362\u8fd8\u662f\u4e0d\u5462\uff1f<\/p>\n<p>Endianness \u662f\u6307\u5185\u5b58\u4e2d\u5b57\u8282\u7684\u6392\u5217\u987a\u5e8f\u3002\u9ad8\u4f4d\u4f18\u5148\uff08big endian\uff09 \u6309\u66f4\u9ad8\u6709\u6548\u5b57\u8282\u5728\u524d\u6392\u5217\uff0c\u7136\u800c \u4f4e\u4f4d\u4f18\u5148\uff08little endian\uff09 \u6309\u7167\u66f4\u4f4e\u6709\u6548\u5b57\u8282\u5728\u524d\u6392\u5e8f\u3002<\/p>\n<p>\u9ad8\u4f4d\u4f18\u5148\u67b6\u6784\uff08\u6bd4\u5982 PowerPC\u00ae\uff09\u6bd4\u4f4e\u4f4d\u4f18\u5148\u67b6\u6784\uff08\u6bd4\u5982 Intel\u00ae Pentium\u00ae \u7cfb\u5217\uff0c\u5176\u7f51\u7edc\u5b57\u8282\u987a\u5e8f\u662f\u9ad8\u4f4d\u4f18\u5148\uff09\u6709\u4f18\u52bf\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5bf9\u9ad8\u4f4d\u4f18\u5148\u7684\u673a\u5668\u6765\u8bf4\uff0c\u5728 TCP\/IP \u5185\u63a7\u5236\u6570\u636e\u662f\u81ea\u7136\u6709\u5e8f\u7684\u3002\u4f4e\u4f4d\u4f18\u5148\u67b6\u6784\u8981\u6c42\u5b57\u8282\u4ea4\u6362 \u2014\u2014 \u5bf9\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u6765\u8bf4\uff0c\u8fd9\u662f\u4e00\u4e2a\u8f7b\u5fae\u7684\u6027\u80fd\u5f31\u70b9\u3002<\/p>\n<p>\u901a\u8fc7\u5957\u63a5\u5b57\u53d1\u9001\u4e00\u4e2a C \u7ed3\u6784\u4f1a\u600e\u4e48\u6837\u5462\uff1f\u8fd9\u91cc\uff0c\u4e5f\u4f1a\u9047\u5230\u9ebb\u70e6\uff0c\u56e0\u4e3a\u4e0d\u662f\u6240\u6709\u7684\u7f16\u8bd1\u5668\u90fd\u4ee5\u76f8\u540c\u7684\u65b9\u5f0f\u6392\u5217\u4e00\u4e2a\u7ed3\u6784\u7684\u5143\u7d20\u3002\u7ed3\u6784\u4e5f\u53ef\u80fd\u88ab\u538b\u7f29\u4ee5\u4fbf\u4f7f\u6d6a\u8d39\u7684\u7a7a\u95f4\u6700\u5c11\uff0c\u8fd9\u8fdb\u4e00\u6b65\u4f7f\u7ed3\u6784\u4e2d\u7684\u5143\u7d20\u9519\u4f4d\u3002<\/p>\n<p>\u5e78\u597d\uff0c\u6709\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u65b9\u6848\uff0c\u80fd\u591f\u4fdd\u8bc1\u4e24\u7aef\u6570\u636e\u7684\u4e00\u81f4\u89e3\u91ca\u3002\u8fc7\u53bb\uff0c\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\uff08Remote Procedure Call\uff0cRPC\uff09\u5957\u88c5\u5de5\u5177\u63d0\u4f9b\u6240\u8c13\u7684\u5916\u90e8\u6570\u636e\u8868\u793a\uff08External Data Representation\uff0cXDR\uff09\u3002XDR \u4e3a\u6570\u636e\u5b9a\u4e49\u4e00\u4e2a\u6807\u51c6\u7684\u8868\u793a\u6765\u652f\u6301\u5f02\u6784\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u901a\u4fe1\u7684\u5f00\u53d1\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u6709\u4e24\u4e2a\u65b0\u7684\u534f\u8bae\u63d0\u4f9b\u76f8\u4f3c\u7684\u529f\u80fd\u3002\u53ef\u6269\u5c55\u6807\u8bb0\u8bed\u8a00\/\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528\uff08XML\/RPC\uff09\u4ee5 XML \u683c\u5f0f\u5b89\u6392 HTTP \u4e0a\u7684\u8fc7\u7a0b\u8c03\u7528\u3002\u6570\u636e\u548c\u5143\u6570\u636e\u7528 XML \u8fdb\u884c\u7f16\u7801\u5e76\u4f5c\u4e3a\u5b57\u7b26\u4e32\u4f20\u8f93\uff0c\u5e76\u901a\u8fc7\u4e3b\u673a\u67b6\u6784\u628a\u503c\u548c\u5b83\u4eec\u7684\u7269\u7406\u8868\u793a\u5206\u5f00\u3002SOAP \u8ddf\u968f XML-RPC\uff0c\u4ee5\u66f4\u597d\u7684\u7279\u6027\u548c\u529f\u80fd\u6269\u5c55\u4e86\u5b83\u7684\u601d\u60f3\u3002\u53c2\u89c1 \u53c2\u8003\u8d44\u6599 \u5c0f\u8282\uff0c\u83b7\u53d6\u66f4\u591a\u5173\u4e8e\u6bcf\u4e2a\u534f\u8bae\u7684\u4fe1\u606f\u3002<\/p>\n<p>\u56de\u9875\u9996<\/p>\n<p>\u9690\u60a3 5\uff0eTCP \u4e2d\u7684\u5e27\u540c\u6b65\u5047\u5b9a<\/p>\n<p>TCP \u4e0d\u63d0\u4f9b\u5e27\u540c\u6b65\uff0c\u8fd9\u4f7f\u5f97\u5b83\u5bf9\u4e8e\u9762\u5411\u5b57\u8282\u6d41\u7684\u534f\u8bae\u662f\u5b8c\u7f8e\u7684\u3002<\/p>\n<p>\u8fd9\u662f TCP \u4e0e UDP\uff08User Datagram Protocol\uff0c\u7528\u6237\u6570\u636e\u62a5\u534f\u8bae\uff09\u7684\u4e00\u4e2a\u91cd\u8981\u533a\u522b\u3002<\/p>\n<p>UDP \u662f\u9762\u5411\u6d88\u606f\u7684\u534f\u8bae\uff0c\u5b83\u4fdd\u7559\u53d1\u9001\u8005\u548c\u63a5\u6536\u8005\u4e4b\u95f4\u7684\u6d88\u606f\u8fb9\u754c\u3002<\/p>\n<p>TCP \u662f\u4e00\u4e2a\u9762\u5411\u6d41\u7684\u534f\u8bae\uff0c\u5b83\u5047\u5b9a\u6b63\u5728\u901a\u4fe1\u7684\u6570\u636e\u662f\u65e0\u7ed3\u6784\u7684\uff0c<\/p>\n<p>\u5982\u56fe 1 \u6240\u793a\u3002<\/p>\n<p>\u56fe 1\uff0eUDP \u7684\u5e27\u540c\u6b65\u80fd\u529b\u548c\u7f3a\u4e4f\u5e27\u540c\u6b65\u7684 TCP<\/p>\n<p>\u56fe 1 \u7684\u4e0a\u90e8\u8bf4\u660e\u4e00\u4e2a UDP \u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u3002<\/p>\n<p>\u5de6\u8fb9\u7684\u5bf9\u7b49\u5c42\u5b8c\u6210\u4e24\u4e2a\u5957\u63a5\u5b57\u7684\u5199\u64cd\u4f5c\uff0c\u6bcf\u4e2a 100 \u5b57\u8282\u3002<\/p>\n<p>\u534f\u8bae\u6808\u7684 UDP \u5c42\u8ffd\u8e2a\u5199\u7684\u6570\u91cf\uff0c\u5e76\u786e\u4fdd\u5f53\u53f3\u8fb9\u7684\u63a5\u6536\u8005\u901a\u8fc7\u5957\u63a5\u5b57\u83b7\u53d6\u6570\u636e\u65f6\uff0c\u5b83\u4ee5\u540c\u6837\u6570\u91cf\u7684\u5b57\u8282\u5230\u8fbe\u3002<\/p>\n<p>\u6362\u53e5\u8bdd\u8bf4\uff0c\u4e3a\u8bfb\u8005\u4fdd\u7559\u4e86\u5199\u8005\u63d0\u4f9b\u7684\u6d88\u606f\u8fb9\u754c\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u770b\u56fe 1 \u7684\u5e95\u90e8\uff0e\u5b83\u4e3a TCP \u5c42\u6f14\u793a\u4e86\u76f8\u540c\u7c92\u5ea6\u7684\u5199\u64cd\u4f5c\u3002<\/p>\n<p>\u4e24\u4e2a\u72ec\u7acb\u7684\u5199\u64cd\u4f5c\uff08\u6bcf\u4e2a 100 \u5b57\u8282\uff09\u5199\u5165\u6d41\u5957\u63a5\u5b57\u3002<\/p>\n<p>\u4f46\u5728\u672c\u4f8b\u4e2d\uff0c\u6d41\u5957\u63a5\u5b57\u7684\u8bfb\u8005\u5f97\u5230\u7684\u662f 200 \u5b57\u8282\u3002<\/p>\n<p>\u534f\u8bae\u6808\u7684 TCP \u5c42\u805a\u5408\u4e86\u4e24\u6b21\u5199\u64cd\u4f5c\u3002<\/p>\n<p>\u8fd9\u79cd\u805a\u5408\u53ef\u4ee5\u53d1\u751f\u5728 TCP\/IP \u534f\u8bae\u6808\u7684\u53d1\u9001\u8005\u6216\u63a5\u6536\u8005\u4e2d\u4efb\u4f55\u4e00\u65b9\u3002<\/p>\n<p>\u91cd\u8981\u7684\u662f\uff0c\u8981\u6ce8\u610f\u5230\u805a\u5408\u4e5f\u8bb8\u4e0d\u4f1a\u53d1\u751f \u2014\u2014 TCP \u53ea\u4fdd\u8bc1\u6570\u636e\u7684\u6709\u5e8f\u53d1\u9001\u3002<\/p>\n<p>\u5bf9\u5927\u591a\u6570\u5f00\u53d1\u4eba\u5458\u6765\u8bf4\uff0c\u8be5\u9677\u9631\u4f1a\u5f15\u8d77\u56f0\u60d1\u3002<\/p>\n<p>\u60a8\u60f3\u8981\u83b7\u5f97 TCP \u7684\u53ef\u9760\u6027\u548c UDP \u7684\u5e27\u540c\u6b65\u3002<\/p>\n<p>\u9664\u975e\u6539\u7528\u5176\u4ed6\u7684\u4f20\u8f93\u534f\u8bae\uff0c\u6bd4\u5982\u6d41\u4f20\u8f93\u63a7\u5236\u534f\u8bae\uff08STCP\uff09\uff0c<\/p>\n<p>\u5426\u5219\u5c31\u8981\u6c42\u5e94\u7528\u5c42\u5f00\u53d1\u4eba\u5458\u6765\u5b9e\u73b0\u7f13\u51b2\u548c\u5206\u6bb5\u529f\u80fd\u3002<\/p>\n<p>\u8c03\u8bd5\u5957\u63a5\u5b57\u5e94\u7528\u7a0b\u5e8f\u7684\u5de5\u5177<\/p>\n<p>GNU\/Linux \u63d0\u4f9b\u51e0\u4e2a\u5de5\u5177\uff0c\u5b83\u4eec\u53ef\u4ee5\u5e2e\u52a9\u60a8\u53d1\u73b0\u5957\u63a5\u5b57\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u4e00\u4e9b\u95ee\u9898\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u8fd8\u6709\u6559\u80b2\u610f\u4e49\uff0c\u800c\u4e14\u80fd\u591f\u5e2e\u52a9\u89e3\u91ca\u5e94\u7528\u7a0b\u5e8f\u548c TCP\/IP \u534f\u8bae\u6808\u7684\u884c\u4e3a\u3002<\/p>\n<p>\u5728\u8fd9\u91cc\uff0c\u60a8\u5c06\u770b\u5230\u5bf9\u51e0\u4e2a\u5de5\u5177\u7684\u6982\u8ff0\u3002\u67e5\u9605\u4e0b\u9762\u7684 \u53c2\u8003\u8d44\u6599 \u4e86\u89e3\u66f4\u591a\u7684\u4fe1\u606f\u3002<\/p>\n<p>\u67e5\u770b\u7f51\u7edc\u5b50\u7cfb\u7edf\u7684\u7ec6\u8282<\/p>\n<p>netstat \u5de5\u5177\u63d0\u4f9b\u67e5\u770b GNU\/Linux \u7f51\u7edc\u5b50\u7cfb\u7edf\u7684\u80fd\u529b\u3002<\/p>\n<p>\u4f7f\u7528 netstat\uff0c\u53ef\u4ee5\u67e5\u770b\u5f53\u524d\u6d3b\u52a8\u7684\u8fde\u63a5\uff08\u6309\u5355\u4e2a\u534f\u8bae\u8fdb\u884c\u67e5\u770b\uff09\uff0c<\/p>\n<p>\u67e5\u770b\u7279\u5b9a\u72b6\u6001\u7684\u8fde\u63a5\uff08\u6bd4\u5982\u5904\u4e8e\u76d1\u542c\u72b6\u6001\u7684\u670d\u52a1\u5668\u5957\u63a5\u5b57\uff09\u548c\u8bb8\u591a\u5176\u4ed6\u7684\u4fe1\u606f\u3002<\/p>\n<p>\u6e05\u5355 4 \u663e\u793a\u4e86 netstat \u63d0\u4f9b\u7684\u4e00\u4e9b\u9009\u9879\u548c\u5b83\u4eec\u542f\u7528\u7684\u7279\u6027\u3002<\/p>\n<p>\u6e05\u5355 4\uff0enetstat \u5b9e\u7528\u7a0b\u5e8f\u7684\u7528\u6cd5\u6a21\u5f0f<\/p>\n<p>View all TCP sockets currently active $ netstat \u2013tcp View all UDP sockets $ netstat \u2013udp View all TCP sockets in the listening state $ netstat \u2013listening View the multicast group membership information $ netstat \u2013groups Display the list of masqueraded connections $ netstat \u2013masquerade View statistics for each protocol $ netstat \u2013statistics <\/p>\n<p>\u5c3d\u7ba1\u5b58\u5728\u8bb8\u591a\u5176\u4ed6\u7684\u5b9e\u7528\u7a0b\u5e8f\uff0c\u4f46 netstat \u7684\u529f\u80fd\u5f88\u5168\u9762\uff0c<\/p>\n<p>\u5b83\u8986\u76d6\u4e86 route\u3001ifconfig \u548c\u5176\u4ed6\u6807\u51c6 GNU\/Linux \u5de5\u5177\u7684\u529f\u80fd\u3002<\/p>\n<p>\u76d1\u89c6\u6d41\u91cf<\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 GNU\/Linux \u7684\u51e0\u4e2a\u5de5\u5177\u6765\u68c0\u67e5\u7f51\u7edc\u4e0a\u7684\u4f4e\u5c42\u6d41\u91cf\u3002<\/p>\n<p>tcpdump \u5de5\u5177\u662f\u4e00\u4e2a\u6bd4\u8f83\u8001\u7684\u5de5\u5177\uff0c\u5b83\u4ece\u7f51\u4e0a\u201c\u55c5\u63a2\u201d\u7f51\u7edc\u6570\u636e\u5305\uff0c\u6253\u5370\u5230stdout \u6216\u8bb0\u5f55\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u3002<\/p>\n<p>\u8be5\u529f\u80fd\u5141\u8bb8\u67e5\u770b\u5e94\u7528\u7a0b\u5e8f\u4ea7\u751f\u7684\u6d41\u91cf\u548c TCP \u751f\u6210\u7684\u4f4e\u5c42\u6d41\u63a7\u5236\u673a\u5236\u3002<\/p>\n<p>\u4e00\u4e2a\u53eb\u505a tcpflow \u7684\u65b0\u5de5\u5177\u4e0etcpdump \u76f8\u8f85\u76f8\u6210\uff0c<\/p>\n<p>\u5b83\u63d0\u4f9b\u534f\u8bae\u6d41\u5206\u6790\u548c\u9002\u5f53\u5730\u91cd\u6784\u6570\u636e\u6d41\u7684\u65b9\u6cd5\uff0c\u800c\u4e0d\u7ba1\u6570\u636e\u5305\u7684\u987a\u5e8f\u6216\u91cd\u53d1\u3002<\/p>\n<p>\u6e05\u5355 5 \u663e\u793a tcpdump \u7684\u4e24\u4e2a\u7528\u6cd5\u6a21\u5f0f\u3002<\/p>\n<p>\u6e05\u5355 5\uff0etcpdump \u5de5\u5177\u7684\u7528\u6cd5\u6a21\u5f0f<\/p>\n<p>Display all traffic on the eth0 interface for the local host <\/p>\n<p>$ tcpdump -l -i eth\/\/ Show all traffic on the network coming from or going to host plato<\/p>\n<p>$ tcpdump host plato\/\/ Show all HTTP traffic for host camus <\/p>\n<p>$ tcpdump host camus and (port http) \/\/View traffic coming from or going to TCP porton the local host<\/p>\n<p>$ tcpdump tcp port<\/p>\n<p>tcpdump \u548c tcpflow \u5de5\u5177\u6709\u5927\u91cf\u7684\u9009\u9879\uff0c\u5305\u62ec\u521b\u5efa\u590d\u6742\u8fc7\u6ee4\u8868\u8fbe\u5f0f\u7684\u80fd\u529b\u3002<\/p>\n<p>\u67e5\u9605\u4e0b\u9762\u7684 \u53c2\u8003\u8d44\u6599 \u83b7\u53d6\u66f4\u591a\u5173\u4e8e\u8fd9\u4e9b\u5de5\u5177\u7684\u4fe1\u606f\u3002<\/p>\n<p>tcpdump \u548c tcpflow \u90fd\u662f\u57fa\u4e8e\u6587\u672c\u7684\u547d\u4ee4\u884c\u5de5\u5177\u3002<\/p>\n<p>\u5982\u679c\u60a8\u66f4\u559c\u6b22\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\uff0c\u6709\u4e00\u4e2a\u5f00\u653e\u6e90\u7801\u5de5\u5177 Ethereal \u4e5f\u8bb8\u9002\u5408\u60a8\u7684\u9700\u8981\u3002<\/p>\n<p>Ethereal \u662f\u4e00\u4e2a\u4e13\u4e1a\u7684\u534f\u8bae\u5206\u6790\u8f6f\u4ef6\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u8c03\u8bd5\u5e94\u7528\u5c42\u534f\u8bae\u3002<\/p>\n<p>\u5b83\u7684\u63d2\u5165\u5f0f\u67b6\u6784\uff08plug-in architecture\uff09\u53ef\u4ee5\u5206\u89e3\u534f\u8bae\uff0c<\/p>\n<p>\u6bd4\u5982 HTTP \u548c\u60a8\u80fd\u60f3\u5230\u7684\u4efb\u4f55\u534f\u8bae\uff08\u5199\u672c\u6587\u7684\u65f6\u5019\u5171\u6709 637 \u4e2a\u534f\u8bae\uff09\u3002<\/p>\n<p>\u56de\u9875\u9996<\/p>\n<p>\u603b\u7ed3<\/p>\n<p>\u5957\u63a5\u5b57\u7f16\u7a0b\u662f\u5bb9\u6613\u800c\u6709\u8da3\u7684\uff0c\u4f46\u662f\u60a8\u8981\u907f\u514d\u5f15\u5165\u9519\u8bef\u6216\u81f3\u5c11\u4f7f\u5b83\u4eec\u5bb9\u6613\u88ab\u53d1\u73b0\uff0c<\/p>\n<p>\u8fd9\u5c31\u9700\u8981\u8003\u8651\u672c\u6587\u4e2d\u63cf\u8ff0\u7684\u8fd9 5 \u4e2a\u5e38\u89c1\u7684\u9677\u9631\uff0c\u5e76\u4e14\u91c7\u7528\u6807\u51c6\u7684\u9632\u9519\u6027\u7a0b\u5e8f\u8bbe\u8ba1\u5b9e\u8df5\u3002<\/p>\n<p>GNU\/Linux \u5de5\u5177\u548c\u5b9e\u7528\u7a0b\u5e8f\u8fd8\u53ef\u4ee5\u5e2e\u52a9\u53d1\u73b0\u4e00\u4e9b\u7a0b\u5e8f\u4e2d\u7684\u5c0f\u95ee\u9898\u3002<\/p>\n<p>\u8bb0\u4f4f\uff1a\u5728\u67e5\u770b\u5b9e\u7528\u7a0b\u5e8f\u7684\u5e2e\u52a9\u624b\u518c\u65f6\u5019\uff0c\u8ddf\u8e2a\u76f8\u5173\u7684\u6216\u201c\u8bf7\u53c2\u89c1\u201d\u5de5\u5177\u3002<\/p>\n<p>\u60a8\u4e5f\u8bb8\u4f1a\u53d1\u73b0\u4e00\u4e2a\u5fc5\u8981\u7684\u65b0\u5de5\u5177\u3002<\/p>\n<p>\u5173\u4e8elinux rpc \u5f00\u53d1\u7684\u4ecb\u7ecd\u5230\u6b64\u5c31\u7ed3\u675f\u4e86\uff0c\u4e0d\u77e5\u9053\u4f60\u4ece\u4e2d\u627e\u5230\u4f60\u9700\u8981\u7684\u4fe1\u606f\u4e86\u5417 \uff1f\u5982\u679c\u4f60\u8fd8\u60f3\u4e86\u89e3\u66f4\u591a\u8fd9\u65b9\u9762\u7684\u4fe1\u606f\uff0c\u8bb0\u5f97\u6536\u85cf\u5173\u6ce8\u672c\u7ad9\u3002<\/p>\n<p><a href=\"http:https:\/\/idc.net\/\">\u9999\u6e2f\u670d\u52a1\u5668<\/a>\u9996\u9009\u540e\u6d6a\u4e91\uff0c2H2G\u9996\u670810\u5143\u5f00\u901a\u3002<br \/>\u540e\u6d6a\u4e91\uff08www.IDC.Net\uff09\u63d0\u4f9b\u7b80\u5355\u597d\u7528\uff0c\u4ef7\u683c\u539a\u9053\u7684\u9999\u6e2f\/\u7f8e\u56fd\u4e91\u670d\u52a1\u5668\u548c\u72ec\u7acb\u670d\u52a1\u5668\u3002IDC+ISP+ICP\u8d44\u8d28\u3002ARIN\u548cAPNIC\u4f1a\u5458\u3002\u6210\u719f\u6280\u672f\u56e2\u961f15\u5e74\u884c\u4e1a\u7ecf\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fdc\u7a0b\u8fc7\u7a0b\u8c03\u7528(RPC, Remote Procedure Call)\u662f\u4e00\u79cd\u8ba1\u7b97\u673a\u901a\u4fe1\u534f\u8bae\uff0c\u5b83\u5141\u8bb8\u4e00\u4e2a\u7a0b\u5e8f\u5728\u4e00\u53f0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[],"class_list":["post-151728","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/151728","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=151728"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/151728\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=151728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=151728"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=151728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}