{"id":15839,"date":"2024-07-10T15:45:49","date_gmt":"2024-07-10T07:45:49","guid":{"rendered":"https:\/\/92it.top\/?p=15839"},"modified":"2024-07-10T15:45:49","modified_gmt":"2024-07-10T07:45:49","slug":"%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%ef%bc%9asocket-%e6%98%af%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e7%9a%84%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=15839","title":{"rendered":"\u7f51\u7edc\u7f16\u7a0b\uff1aSocket \u662f\u5982\u4f55\u521b\u5efa\u7684\uff1f"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\uff1a<a href=\"https:\/\/mp.weixin.qq.com\/s?__biz=MzIwNDAyOTI2Nw==&amp;mid=2247484492&amp;idx=1&amp;sn=030d49fb8302b3a27d845ab95537b94d&amp;chksm=96c72870a1b0a1668abf20f3a9ca4d0b2af2684bdf7af68a6d8274fa10c4f43b852e4bca9758&amp;cur_album_id=2818595943724974083&amp;scene=190#rd\">\u7f51\u7edc\u7f16\u7a0b\uff1aSocket \u662f\u5982\u4f55\u521b\u5efa\u7684\uff1f<\/a><\/p>\n\n\n\n<p>\u8bf4\u8d77\u7f51\u7edc\u901a\u4fe1\uff0c\u5c31\u4e0d\u5f97\u4e0d\u63d0\u5230 Socket\uff0c\u4e0d\u7ba1\u4f7f\u7528\u7684\u662f Java \u8bed\u8a00\uff0c\u8fd8\u662f C\/C++\uff0cGo\uff0cPHP\uff0c\u53ea\u8981\u4f60\u8ddf\u7f51\u7edc\u7f16\u7a0b\u6253\u4ea4\u9053\uff0c\u57fa\u672c\u4e0a\u79bb\u4e0d\u5f00 Socket\u3002\u90a3\u4e48 Socket \u5230\u5e95\u662f\u4ec0\u4e48\uff1f \u5b83\u53c8\u662f\u5982\u4f55\u88ab\u521b\u5efa\u7684\uff1f \u8fd9\u7bc7\u6587\u7ae0\uff0c\u6211\u4eec\u5c31\u6765\u8bb2\u6e05\u695a\u3002<\/p>\n\n\n\n<p>\u5728\u6b63\u5f0f\u5206\u6790 Socket \u4e4b\u524d\uff0c\u6211\u4eec\u5148\u94fa\u57ab\u4e0b Linux \u64cd\u4f5c\u7cfb\u7edf\u4e0b\u7684\u5185\u6838\u6001\u548c\u7528\u6237\u6001\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u7406\u89e3 Socket\u3002<\/p>\n\n\n\n<p><strong>\u4e00\u3001Linux\u7528\u6237\u6001\u548c\u5185\u6838\u6001\u00a0<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Linux \u8bbe\u8ba1\u7684\u521d\u8877\u662f\u4e3a\u4e86\u5b89\u5168\uff0c\u7ed9\u4e0d\u540c\u7684\u64cd\u4f5c\u5206\u914d\u4e0d\u540c\u7684\u201c\u6743\u9650\u201d\uff0c\u56e0\u6b64\u5c06\u6743\u9650\u5206\u4e3a 2 \u4e2a\u7b49\u7ea7\uff1a\u5185\u6838\u6001\uff08\u5185\u6838\u7a7a\u95f4\uff09\u548c\u7528\u6237\u6001\uff08\u7528\u6237\u7a7a\u95f4\uff09\u3002<\/p>\n\n\n\n<ul>\n<li>\u7528\u6237\u6001\uff1a\u63d0\u4f9b\u5e94\u7528\u7a0b\u5e8f\u8fd0\u884c\u7684\u7a7a\u95f4\uff0c\u4e00\u822c\u6211\u4eec\u7f16\u5199\u7684\u4e1a\u52a1\u4ee3\u7801\u5c31\u662f\u8fd0\u884c\u5728\u7a7a\u95f4\u3002<\/li>\n\n\n\n<li>\u5185\u6838\u6001\uff1a\u63d0\u4f9b\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u4f7f\u7528\uff0c\u6bd4\u5982\uff1acpu \u53ef\u4ee5\u8bbf\u95ee\u5185\u5b58\u3001\u5916\u56f4\u8bbe\u5907\u7b49\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u4e3a\u4ec0\u4e48\u8981\u5212\u5206\u7528\u6237\u6001\u548c\u5185\u6838\u6001\uff1f<\/strong><\/p>\n\n\n\n<p>\u7b80\u5355\u6765\u8bf4\uff1a<\/p>\n\n\n\n<ul>\n<li>\u7981\u6b62\u7528\u6237\u7a0b\u5e8f\u548c\u5e95\u5c42\u786c\u4ef6\u5e73\u53f0\u76f4\u63a5\u4ea4\u4e92\u3002<\/li>\n\n\n\n<li>\u7981\u6b62\u7528\u6237\u7a0b\u5e8f\u76f4\u63a5\u8bbf\u95ee\u4efb\u610f\u5185\u5b58\u5730\u5740\u7a7a\u95f4\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u7528\u6237\u6001\u548c\u5185\u6838\u6001\u4e3b\u8981\u53ef\u4ee5\u901a\u8fc7 3 \u79cd\u65b9\u5f0f\u6765\u8fdb\u884c\u8f6c\u6362\uff1a<\/p>\n\n\n\n<ol>\n<li>\u7cfb\u7edf\u8c03\u7528\uff1a\u8c03\u7528\u7cfb\u7edf\u7684\u5e93\u51fd\u6570\u6216\u8005 shell \u8c03\u7528\uff1b<\/li>\n\n\n\n<li>\u5f02\u5e38\uff1a\u5982\u679c\u5f53\u524d\u8fdb\u7a0b\u8fd0\u884c\u5728\u7528\u6237\u6001\uff0c\u5982\u679c\u8fd9\u4e2a\u65f6\u5019\u53d1\u751f\u4e86\u5f02\u5e38\u4e8b\u4ef6\uff0c\u5c31\u4f1a\u89e6\u53d1\u5207\u6362\u3002\u4f8b\u5982\uff1a\u7f3a\u9875\u5f02\u5e38\uff1b<\/li>\n\n\n\n<li>\u5916\u8bbe\u4e2d\u65ad\uff1a\u5f53\u5916\u8bbe\u5b8c\u6210\u7528\u6237\u7684\u8bf7\u6c42\u65f6\uff0c\u4f1a\u5411 CPU \u53d1\u9001\u4e2d\u65ad\u4fe1\u53f7\uff1b<\/li>\n<\/ol>\n\n\n\n<p>\u4ea4\u4e92\u62bd\u8c61\u5982\u4e0b\u56fe\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"661\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-56-1024x661.png\" alt=\"\" class=\"wp-image-15840\" style=\"width:466px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-56-1024x661.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-56-300x194.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-56-768x496.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-56-830x536.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-56-230x149.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-56-350x226.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-56-480x310.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-56.png 1180w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u5728\u4e86\u89e3\u5b8c Linux\u7684\u7528\u6237\u6001\u548c\u5185\u6838\u6001\u4e4b\u540e\uff0c\u6211\u4eec\u5f00\u59cb\u5206\u6790 Socket \u53ca\u5176\u521b\u5efa\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<p><strong>\u4e8c\u3001\u4ec0\u4e48\u662fSocket\u00a0<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Socket\uff1a\u4e2d\u6587\u7ffb\u8bd1\u6709\u5f88\u591a\uff0c\u6240\u6709\u7684\u7ffb\u8bd1\u6574\u7406\u5982\u4e0b\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"206\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-57-1024x206.png\" alt=\"\" class=\"wp-image-15841\" style=\"width:554px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-57-1024x206.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-57-300x60.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-57-768x154.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-57-830x167.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-57-230x46.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-57-350x70.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-57-480x96.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-57.png 1274w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u4e1a\u5185\uff0c\u5f88\u591a\u4eba\u628a Socket \u7ffb\u8bd1\u6210\u201d\u5957\u63a5\u5b57\u201c\uff0c\u6216\u8bb8\u8fd9\u4e2a\u4e2d\u6587\u540d\u8bcd\u53ef\u4ee5\u5f88\u751f\u52a8\u5f62\u8c61\u7684\u4f20\u8fbe Socket \u6240\u8981\u8868\u8fbe\u7684\u610f\u601d\uff0c\u4f46\u662f\u6211\u67e5\u9605\u4e86\u5f88\u591a\u8d44\u6599\uff0c\u4e00\u76f4\u6ca1\u6709\u627e\u5230\u4e3a\u4ec0\u4e48\u7ffb\u8bd1\u6210\u201d\u5957\u63a5\u5b57\u201c\u7684\u6709\u529b\u6587\u6863\u8bf4\u660e\uff0c\u6240\u4ee5\u4e2a\u4eba\u8fd8\u662f\u559c\u6b22\u76f4\u63a5\u7528\u82f1\u6587 Socket \u6765\u8868\u8fbe\uff0c\u4e0d\u8fc7\u8fd9\u4e2a\u4e0d\u5f71\u54cd\u6574\u4f53\u5bf9 Socket \u7684\u7406\u89e3\u3002<\/p>\n\n\n\n<p>\u5728 Linux \u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c Socket \u662f\u7528\u6765\u66ff\u4ee3\u4f20\u8f93\u5c42\u4ee5\u4e0a\u534f\u8bae\u5b9e\u4f53\u7684\u6807\u51c6\u63a5\u53e3\uff0c\u5b83\u8d1f\u8d23\u5b9e\u73b0\u4f20\u8f93\u5c42\u4ee5\u4e0a\u6240\u6709\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u8bf4 Socket \u662f TCP\/IP \u534f\u8bae\u6808\u5bf9\u5916\u7684\u7a97\u53e3\u3002Socket \u662f\u4ecb\u4e8e\u5e94\u7528\u5c42\u4e0e\u4f20\u8f93\u5c42\u4e2d\u95f4\u7684\u8f6f\u4ef6\u62bd\u8c61\u5c42\uff0c\u5b83\u662f\u4e00\u7ec4\u63a5\u53e3\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"898\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-59-1024x898.png\" alt=\"\" class=\"wp-image-15843\" style=\"width:486px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-59-1024x898.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-59-300x263.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-59-768x673.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-59-830x728.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-59-230x202.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-59-350x307.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-59-480x421.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-59.png 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p><strong>\u4e09\u3001Socket\u7684\u6570\u636e\u7ed3\u6784\u00a0<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5728 Linux \u5185\u6838\u4e2d\uff0cSocket \u7684\u6570\u636e\u7ed3\u6784\u7531 struct socket \u4e0e struct sock 2 \u90e8\u5206\u7ec4\u6210\u3002<\/p>\n\n\n\n<p><strong>struct socket<\/strong><\/p>\n\n\n\n<p>\u6bcf\u4e2a Socket \u5728\u5185\u6838\u4e2d\u90fd\u552f\u4e00\u5bf9\u5e94\u4e00\u4e2a struct socket \u7ed3\u6784\uff0c\u5176\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"372\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-60-1024x372.png\" alt=\"\" class=\"wp-image-15844\" style=\"width:608px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-60-1024x372.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-60-300x109.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-60-768x279.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-60-830x301.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-60-230x84.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-60-350x127.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-60-480x174.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-60.png 1294w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p><strong>struct sock<\/strong><\/p>\n\n\n\n<p>struct sock \u662f Socket \u5728\u7f51\u7edc\u4e2d\u7684\u6700\u5c0f\u63cf\u8ff0\uff0c\u5b83\u5305\u542b\u4e86\u5185\u6838\u7ba1\u7406 Socket \u6700\u91cd\u8981\u7684\u4fe1\u606f\u96c6\u5408\uff0c\u5176\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"434\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-61-1024x434.png\" alt=\"\" class=\"wp-image-15845\" style=\"width:608px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-61-1024x434.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-61-300x127.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-61-768x325.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-61-830x352.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-61-230x97.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-61-350x148.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-61-480x203.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-61.png 1270w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u5728 Linux \u4e2d Socket \u5b58\u5728\u7684\u65b9\u5f0f\u662f\uff1a<strong>\u6587\u4ef6\uff0c<\/strong>\u5bf9\u5e94\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/p>\n\n\n\n<p>\u5f53 Socket \u8fde\u63a5\u5efa\u7acb\u540e\uff0c\u7528\u6237\u8fdb\u7a0b\u5c31\u53ef\u4ee5\u4f7f\u7528\u5e38\u89c4\u6587\u4ef6\u64cd\u4f5c\u8bbf\u95ee Socket\uff0c\u6bcf\u4e2a Socket \u90fd\u5206\u914d\u4e86\u4e00\u4e2a VFS inode\uff0cinode \u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"287\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-62-1024x287.png\" alt=\"\" class=\"wp-image-15846\" style=\"width:592px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-62-1024x287.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-62-300x84.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-62-768x215.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-62-830x233.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-62-230x64.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-62-350x98.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-62-480x134.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-62.png 1278w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p><strong>\u56db\u3001Socket\u5982\u4f55\u521b\u5efa\u00a0<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u4e0a\u6587\u6211\u4eec\u8bb2\u89e3\u4e86 Socket \u7684\u5b9a\u4e49\u4ee5\u53ca\u6570\u636e\u7ed3\u6784\uff0c\u63a5\u4e0b\u6765\u5c31\u8981\u5206\u6790\u57fa\u4e8e TCP \u534f\u8bae\u7684 Socket \u662f\u5982\u4f55\u521b\u5efa\u7684\uff0c\u4e00\u822c\u6765\u8bf4\uff0c\u521b\u5efa Socket \u9700\u8981\u7ecf\u8fc7\u4e0b\u9762 6 \u4e2a\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<ol>\n<li>\u521b\u5efaSocket\uff1b<\/li>\n\n\n\n<li>\u5c06Socket\u4e0e\u5730\u5740\u7ed1\u5b9a\uff0c\u8bbe\u7f6eSocket\u9009\u9879\uff1b<\/li>\n\n\n\n<li>\u5efa\u7acbSocket\u4e4b\u95f4\u7684\u8fde\u63a5\uff1b<\/li>\n\n\n\n<li>\u76d1\u542cSocket\uff1b<\/li>\n\n\n\n<li>\u63a5\u6536\u3001\u53d1\u9001\u6570\u636e\uff1b<\/li>\n\n\n\n<li>\u5173\u95ed\u3001\u91ca\u653eSocket\uff1b<\/li>\n<\/ol>\n\n\n\n<p>\u56e0\u4e3a Socket \u662f\u53cc\u901a\u9053\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u4ece\u670d\u52a1\u7aef\u548c\u5ba2\u6237\u7aef\u4e24\u4e2a\u90e8\u5206\u6765\u8bf4\u660e\u57fa\u4e8eTCP\u534f\u8bae\u7684 Socket \u7684\u521b\u5efa\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<p><strong>\u670d\u52a1\u7aef\u521b\u5efa Socket<\/strong><\/p>\n\n\n\n<ol>\n<li>\u8c03\u7528 socket()\u51fd\u6570\uff0c\u521b\u5efa\u4e00\u4e2a socket\uff0c\u8be5 Socket \u6210\u4e3a\u4e3b\u52a8 Socket\uff08Active Socket\uff09\uff1b<\/li>\n\n\n\n<li>\u8c03\u7528 bind()\u51fd\u6570\uff0c\u7ed9\u7b2c 1 \u6b65\u7684\u4e3b\u52a8 Socket \u7ed1\u5b9a\u4e00\u4e2a ip \u548c port\uff1b<\/li>\n\n\n\n<li>\u8c03\u7528 listen()\u51fd\u6570\uff0c\u5c06\u4e3b\u52a8 Socket \u8f6c\u6210\u76d1\u542c Socket\uff0c\u5f00\u59cb\u76d1\u542c\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42\uff1b<\/li>\n\n\n\n<li>\u8c03\u7528 accept()\u51fd\u6570\uff0c\u4ece\u5df2\u5b8c\u6210\u8fde\u63a5\u7684\u961f\u5217\u4e2d\u62ff\u51fa\u4e00\u4e2a\u8fde\u63a5\u8fdb\u884c\u5904\u7406\uff0c\u5982\u679c\u8fd8\u6ca1\u6709\u5b8c\u6210\uff0c\u5c31\u8981\u963b\u585e\u7b49\u5f85\u6709\u8fde\u63a5\u5b8c\u6210\uff1b<\/li>\n\n\n\n<li>\u82e5\u670d\u52a1\u7aef accept()\u51fd\u6570\u83b7\u53d6\u5230\u4e86\u4e00\u4e2a\u5df2\u8fde\u63a5 Socket(Connected Socket)\uff0c\u5219\u670d\u52a1\u7aef\u53ef\u4ee5\u5f80\u5df2\u8fde\u63a5 Socket \u8bfb\u6570\u636e\u6216\u8005\u5199\u6570\u636e\uff1b<\/li>\n<\/ol>\n\n\n\n<p>\u8bf4\u660e\uff1a\u5728 Linux \u5185\u6838\u4e2d\uff0c\u4f1a\u4e3a\u6bcf\u4e2a Socket \u7ef4\u62a4\u4e24\u4e2a\u961f\u5217\uff1a\u4e00\u4e2a\u662f\u5df2\u7ecf\u5efa\u7acb\u4e86\u8fde\u63a5\u7684\u961f\u5217\uff08\u4e09\u6b21\u63e1\u624b\u5df2\u7ecf\u5b8c\u6bd5\uff09\uff0c\u5904\u4e8e established \u72b6\u6001\uff1b\u4e00\u4e2a\u662f\u8fd8\u6ca1\u6709\u5b8c\u5168\u5efa\u7acb\u8fde\u63a5\u7684 \uff08\u672a\u5b8c\u6210\u4e09\u6b21\u63e1\u624b\uff09\uff0c\u5904\u4e8e syn_rcvd \u7684\u72b6\u6001\u3002<\/p>\n\n\n\n<p><strong>\u5ba2\u6237\u7aef\u521b\u5efa Socket<\/strong><\/p>\n\n\n\n<ol>\n<li>\u8c03\u7528 socket()\u51fd\u6570\uff0c\u521b\u5efa\u4e00\u4e2a socket\uff0c\u8be5 Socket \u6210\u4e3a\u4e3b\u52a8 Socket\uff08Active Socket\uff09\uff1b<\/li>\n\n\n\n<li>\u5f53\u670d\u52a1\u7aef\u8c03\u7528 accept()\u65f6\uff0c\u5ba2\u6237\u7aef\u53ef\u4ee5\u8c03\u7528 connect()\u5411\u670d\u52a1\u5668\u53d1\u8d77\u8fde\u63a5\u8bf7\u6c42\uff0c\u5185\u6838\u4f1a\u7ed9\u5ba2\u6237\u7aef\u5206\u914d\u4e00\u4e2a\u4e34\u65f6\u7684\u7aef\u53e3\uff0c\u4e00\u65e6\u63e1\u624b\u6210\u529f\uff0c\u670d\u52a1\u7aef\u7684 accept()\u5c31\u4f1a\u8fd4\u56de\u53e6\u4e00\u4e2a Socket\uff1b<\/li>\n\n\n\n<li>\u5ba2\u6237\u7aef\u53ef\u4ee5\u5411 \u5df2\u8fde\u63a5 Socket \u8bfb\u6570\u636e\u6216\u8005\u5199\u6570\u636e\uff1b<\/li>\n<\/ol>\n\n\n\n<p>\u57fa\u4e8e TCP \u534f\u8bae\u7684 Socket \u521b\u5efa\u8fc7\u7a0b\u53ef\u4ee5\u63cf\u8ff0\u6210\u5982\u4e0b\u56fe\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"525\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-63-1024x525.png\" alt=\"\" class=\"wp-image-15847\" style=\"width:540px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-63-1024x525.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-63-300x154.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-63-768x394.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-63-1536x788.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-63-2048x1051.png 2048w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-63-830x426.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-63-230x118.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-63-350x180.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-63-480x246.png 480w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>UDP \u548c TCP \u6709\u4e9b\u5dee\u5f02\uff0cUDP \u662f\u9762\u5411\u65e0\u8fde\u63a5\u534f\u8bae\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u4e09\u6b21\u63e1\u624b\uff0c\u4e5f\u5c31\u662f\u4e0d\u9700\u8981\u8c03\u7528 listen \u548c connect\uff0c\u53ea\u9700\u8981 IP \u548c\u7aef\u53e3\u53f7\uff0c\u56e0\u800c\u9700\u8981\u8c03\u7528 bind\u3002UDP \u662f\u6ca1\u6709\u7ef4\u62a4\u8fde\u63a5\u72b6\u6001\u7684\uff0c\u56e0\u800c\u4e0d\u9700\u8981\u6bcf\u5bf9\u8fde\u63a5\u5efa\u7acb\u4e00\u7ec4 Socket\uff0c\u800c\u662f\u53ea\u8981\u6709\u4e00\u4e2a Socket\uff0c\u5c31\u80fd\u591f\u548c\u591a\u4e2a\u5ba2\u6237\u7aef\u901a\u4fe1\u3002\u4e5f\u6b63\u662f\u56e0\u4e3a\u6ca1\u6709\u8fde\u63a5\u72b6\u6001\uff0c\u6bcf\u6b21\u901a\u4fe1\u7684\u65f6\u5019\uff0c\u90fd\u8c03\u7528 sendto \u548c recvfrom\uff0c\u90fd\u53ef\u4ee5\u4f20\u5165 IP \u5730\u5740\u548c\u7aef\u53e3\u3002<\/p>\n\n\n\n<p>\u57fa\u4e8e UDP \u534f\u8bae\u7684 Socket \u521b\u5efa\u8fc7\u7a0b\u53ef\u4ee5\u63cf\u8ff0\u6210\u5982\u4e0b\u56fe\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"453\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-65-1024x453.png\" alt=\"\" class=\"wp-image-15849\" style=\"width:586px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-65-1024x453.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-65-300x133.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-65-768x340.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-65-1536x679.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-65-830x367.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-65-230x102.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-65-350x155.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-65-480x212.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-65.png 1692w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p><strong>\u4e94\u3001Socket\u7c7b\u578b\u00a0<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Socket \u7c7b\u578b\u67094\u79cd\uff0c\u8bf4\u660e\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ol>\n<li>\u4e3b\u52a8 Socket\uff08Active Socket\uff09\uff1a\u901a\u8fc7\u7cfb\u7edf\u5e93\u51fd\u6570 socket()\u751f\u6210\u7684\u5c31\u662f\u4e3b\u52a8 Socket\uff0c\u4e3b\u8981\u662f\u7528\u4e8e\u5ba2\u6237\u7aef\u4e3b\u52a8\u5411\u670d\u52a1\u5668\u53d1\u9001\u8fde\u63a5\uff1b<\/li>\n\n\n\n<li>\u88ab\u52a8 Socket\uff08Passive Socket\uff09\uff1a\u901a\u8fc7\u8c03\u7528\u7cfb\u7edf\u5e93\u51fd\u6570 listen()\uff0c\u53ef\u4ee5\u5c06\u4e3b\u52a8 Socket \u6807\u8bb0\u4e3a\u88ab\u52a8 Socket\uff0c\u7528\u4e8e\u88ab\u52a8\u76d1\u542c\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u8fde\u63a5\uff0c\u4e5f\u53eb\u76d1\u542c Socket\u3002\u88ab\u52a8 Socket \u662f\u670d\u52a1\u7aef\u72ec\u6709\u7684\uff0c\u5c06\u4f34\u968f\u670d\u52a1\u7aef\u7684\u6574\u4e2a\u751f\u547d\u5468\u671f\u3002<\/li>\n\n\n\n<li>\u76d1\u542c Socket\uff08Listened Socket\uff09\uff1a\u540c\u88ab\u52a8 socket\uff08Passive Socket\uff09\u4e00\u6837\u3002<\/li>\n\n\n\n<li>\u5df2\u8fde\u63a5 Socket\uff08Connected Socket\uff09\uff1a\u901a\u8fc7\u7cfb\u7edf\u5e93\u51fd\u6570 accept()\u83b7\u53d6\u7684\u5df2\u5efa\u7acb\u8fde\u63a5\u7684 Socket\uff0c\u8be5 Socket \u662f\u7528\u4e8e\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u6570\u636e\u8bfb\u5199\u7684\u901a\u9053\uff0c\u5df2\u8fde\u63a5 Socket \u662f\u670d\u52a1\u5668\u72ec\u6709\u7684\uff0c\u751f\u547d\u5468\u671f\u4e3a \u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u7684\u7ef4\u6301\u7684\u8fde\u63a5\u65f6\u957f\uff0c\u5f53\u65ad\u5f00\u8fde\u63a5\uff0c\u751f\u547d\u5468\u671f\u7ed3\u675f\u3002<br><\/li>\n<\/ol>\n\n\n\n<p><strong>\u516d\u3001Socket Java\u5b9e\u4f8b\u00a0<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8bb2\u8ff0\u4e86 Socket \u8fd9\u4e48\u591a\u7684\u7406\u8bba\u77e5\u8bc6\uff0c\u6700\u540e\u6211\u4eec\u770b\u4e0b Socket \u5728\u5e94\u7528\u5c42\u8bed\u8a00\u662f\u600e\u4e48\u4f7f\u7528\u7684\uff0c\u8fd9\u91cc\u4ee5 Java \u4e3a\u4f8b\u3002<\/p>\n\n\n\n<p><strong>Server\u7aef\u6e90\u7801<\/strong><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"541\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-67-1024x541.png\" alt=\"\" class=\"wp-image-15851\" style=\"width:560px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-67-1024x541.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-67-300x158.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-67-768x405.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-67-830x438.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-67-230x121.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-67-350x185.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-67-480x253.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-67.png 1364w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>Java \u6e90\u7801 ServerSocket \u7c7b\u4e2d\u5c01\u88c5\u4e86\u591a\u4e2a\u6784\u9020\u5668\uff0c\u6e90\u7801\u8c03\u8bd5\u53ef\u4ee5\u770b\u4e0b\uff0c\u6784\u9020\u5668\u6700\u7ec8\u90fd\u6307\u5411 bind(SocketAddress endpoint, int backlog)\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u91cc\u9762\u5305\u542b\u91cd\u8981\u4e24\u4e2a\u6b65\u9aa4 bind()\u548c listen()\uff0c\u4fe9\u65b9\u6cd5\u53c8\u6307\u5411 PlainSocketImpl \u7c7b\u4e2d\u7684\u7684 native socketBind()\u548c native socketListen()\uff0cnative \u65b9\u6cd5\u5176\u5b9e\u6700\u7ec8\u64cd\u4f5c\u7cfb\u7edf\u7684 bind()\u548c listen()\u51fd\u6570\u5bf9\u5e94\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u901a\u8fc7\u4e0a\u9762\u7684 Java \u6e90\u7801\u53ef\u4ee5\u770b\u51fa\uff0cJava \u7684 ServerSocket \u7c7b\u53ea\u662f\u5bf9\u64cd\u4f5c\u7cfb\u7edf\u7684\u51fd\u6570\u505a\u4e86\u4e00\u5c42\u7b80\u5355\u7684\u5305\u88c5\uff0c\u4e0b\u9762\u6211\u4eec\u518d\u770b\u770b Java \u5bf9\u5ba2\u6237\u7aef\u7684\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<p><strong>Client\u7aef\u6e90\u7801<\/strong><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"473\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-68-1024x473.png\" alt=\"\" class=\"wp-image-15852\" style=\"width:576px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-68-1024x473.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-68-300x139.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-68-768x355.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-68-830x383.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-68-230x106.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-68-350x162.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-68-480x222.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/07\/image-68.png 1360w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>Java \u6e90\u7801\u5bf9\u5ba2\u6237\u7aef\u7684\u5305\u88c5\u548c\u670d\u52a1\u7aef\u5f88\u7c7b\u4f3c\uff0c\u6700\u540e\u90fd\u6307\u5411 native connect()\u65b9\u6cd5\u548c\u64cd\u4f5c\u7cfb\u7edf\u7684 connect()\u51fd\u6570\u5bf9\u5e94\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u5bf9 java.net \u5305\u4e2d ServerSocket \u7c7b\u548c Socket \u7c7b\u7684\u6e90\u7801\u5206\u6790\uff0c\u6211\u4eec\u770b\u51fa Java \u53ea\u662f\u5728 jdk \u91cc\u9762\u505a\u4e86\u4e00\u5c42\u4f7f\u7528\u5c01\u88c5\uff0c\u6700\u7ec8\u90fd\u662f\u6307\u5411 native \u7684\u65b9\u6cd5\uff0c\u548c\u64cd\u4f5c\u7cfb\u7edf\u7684\u51fd\u6570\u7ed1\u5b9a\uff0c\u8fd9\u4e2a\u6d41\u7a0b\u4e5f\u518d\u6b21\u548c\u4e0a\u9762\u57fa\u4e8e TCP \u534f\u8bae\u7684 Socket \u7a0b\u5e8f\u51fd\u6570\u8c03\u7528\u8fc7\u7a0b\u56fe \u543b\u5408\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1a\u7f51\u7edc\u7f16\u7a0b\uff1aSocket \u662f\u5982\u4f55\u521b\u5efa\u7684\uff1f \u8bf4\u8d77\u7f51\u7edc\u901a\u4fe1\uff0c\u5c31\u4e0d\u5f97\u4e0d\u63d0\u5230 Socket\uff0c\u4e0d\u7ba1\u4f7f\u7528\u7684\u662f Java [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/15839"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=15839"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/15839\/revisions"}],"predecessor-version":[{"id":15853,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/15839\/revisions\/15853"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=15839"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=15839"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=15839"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}