{"id":10006822,"date":"2022-11-07T17:01:27","date_gmt":"2022-11-07T09:01:27","guid":{"rendered":"https:\/\/ityet.com\/?p=10006822"},"modified":"2022-11-07T17:01:27","modified_gmt":"2022-11-07T09:01:27","slug":"%e7%90%86%e8%a7%a3nodejs%e4%b8%ad%e7%9a%84%e8%bf%9b%e7%a8%8b%e9%97%b4%e9%80%9a%e4%bf%a1","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006822","title":{"rendered":"\u7406\u89e3Nodejs\u4e2d\u7684\u8fdb\u7a0b\u95f4\u901a\u4fe1"},"content":{"rendered":"<h2>\u524d\u7f6e\u77e5\u8bc6<\/h2>\n<h3>\u6587\u4ef6\u63cf\u8ff0\u7b26<\/h3>\n<p>\u5728 Linux \u7cfb\u7edf\u4e2d\uff0c\u4e00\u5207\u90fd\u770b\u6210\u6587\u4ef6\uff0c\u5f53\u8fdb\u7a0b\u6253\u5f00\u73b0\u6709\u6587\u4ef6\u65f6\uff0c\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<br \/>\u6587\u4ef6\u63cf\u8ff0\u7b26\u662f\u64cd\u4f5c\u7cfb\u7edf\u4e3a\u4e86\u7ba1\u7406\u5df2\u7ecf\u88ab\u8fdb\u7a0b\u6253\u5f00\u7684\u6587\u4ef6\u6240\u521b\u5efa\u7684\u7d22\u5f15\uff0c\u7528\u6765\u6307\u5411\u88ab\u6253\u5f00\u7684\u6587\u4ef6\u3002<br \/>\u5f53\u6211\u4eec\u7684\u8fdb\u7a0b\u542f\u52a8\u4e4b\u540e\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u7ed9\u6bcf\u4e00\u4e2a\u8fdb\u7a0b\u5206\u914d\u4e00\u4e2a PCB \u63a7\u5236\u5757\uff0cPCB \u4e2d\u4f1a\u6709\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u8868\uff0c\u5b58\u653e\u5f53\u524d\u8fdb\u7a0b\u6240\u6709\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u5373\u5f53\u524d\u8fdb\u7a0b\u6253\u5f00\u7684\u6240\u6709\u6587\u4ef6\u3002<\/p>\n<p>\ud83e\udd14 \u8fdb\u7a0b\u4e2d\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u662f\u5982\u4f55\u548c\u7cfb\u7edf\u6587\u4ef6\u5bf9\u5e94\u8d77\u6765\u7684\uff1f<br \/>\u5728\u5185\u6838\u4e2d\uff0c\u7cfb\u7edf\u4f1a\u7ef4\u62a4\u53e6\u5916\u4e24\u79cd\u8868<\/p>\n<ul>\n<li>\u6253\u5f00\u6587\u4ef6\u8868(Open file table)<\/li>\n<li>i-node \u8868(i-node table)<\/li>\n<\/ul>\n<p>\u6587\u4ef6\u63cf\u8ff0\u7b26\u5c31\u662f\u6570\u7ec4\u7684\u4e0b\u6807\uff0c\u4ece0\u5f00\u59cb\u5f80\u4e0a\u9012\u589e\uff0c0\/1\/2 \u9ed8\u8ba4\u662f\u6211\u4eec\u7684\u8f93\u5165\/\u8f93\u51fa\/\u9519\u8bef\u6d41\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26<br \/>\u5728 PCB \u4e2d\u7ef4\u62a4\u7684\u6587\u4ef6\u63cf\u8ff0\u8868\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u6587\u4ef6\u63cf\u8ff0\u7b26\u627e\u5230\u5bf9\u5e94\u4e86\u6587\u4ef6\u6307\u9488\uff0c\u627e\u5230\u5bf9\u5e94\u7684\u6253\u5f00\u6587\u4ef6\u8868<br \/>\u6253\u5f00\u6587\u4ef6\u8868\u4e2d\u7ef4\u62a4\u4e86\uff1a\u6587\u4ef6\u504f\u79fb\u91cf(\u8bfb\u5199\u6587\u4ef6\u7684\u65f6\u5019\u4f1a\u66f4\u65b0)\uff1b\u5bf9\u4e8e\u6587\u4ef6\u7684\u72b6\u6001\u6807\u8bc6\uff1b\u6307\u5411 i-node \u8868\u7684\u6307\u9488<br \/>\u60f3\u8981\u771f\u6b63\u7684\u64cd\u4f5c\u6587\u4ef6\uff0c\u8fd8\u5f97\u9760 i-node \u8868\uff0c\u80fd\u591f\u83b7\u53d6\u5230\u771f\u5b9e\u6587\u4ef6\u7684\u76f8\u5173\u4fe1\u606f<\/p>\n<p>\u4ed6\u4eec\u4e4b\u95f4\u7684\u5173\u7cfb<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042754886\" alt=\"image\" title=\"image\" loading=\"lazy\"><\/p>\n<p><strong>\u56fe\u89e3<\/strong><\/p>\n<ul>\n<li>\u5728\u8fdb\u7a0b A \u4e2d\uff0c\u6587\u4ef6\u63cf\u8ff0\u7b261\/20\u5747\u6307\u5411\u4e86\u540c\u4e00\u6253\u5f00\u6587\u4ef6\u8868\u987923\uff0c\u8fd9\u53ef\u80fd\u662f\u5bf9\u540c\u4e00\u6587\u4ef6\u591a\u6b21\u8c03\u7528\u4e86 open \u51fd\u6570\u5f62\u6210\u7684<\/li>\n<li>\u8fdb\u7a0b A\/B \u7684\u6587\u4ef6\u63cf\u8ff0\u7b262\u90fd\u6307\u5411\u540c\u4e00\u6587\u4ef6\uff0c\u8fd9\u53ef\u80fd\u662f\u8c03\u7528\u4e86 fork \u521b\u5efa\u5b50\u8fdb\u7a0b\uff0cA\/B \u662f\u7236\u5b50\u5173\u7cfb\u8fdb\u7a0b<\/li>\n<li>\u8fdb\u7a0b A \u7684\u6587\u4ef6\u63cf\u8ff0\u7b260\u548c\u8fdb\u7a0b B \u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u6307\u5411\u4e86\u4e0d\u540c\u7684\u6253\u5f00\u6587\u4ef6\u8868\u9879\uff0c\u4f46\u8fd9\u4e9b\u8868\u9879\u6307\u5411\u4e86\u540c\u4e00\u4e2a\u6587\u4ef6\uff0c\u8fd9\u53ef\u80fd\u662f A\/B \u8fdb\u7a0b\u5206\u522b\u5bf9\u540c\u4e00\u6587\u4ef6\u53d1\u8d77\u4e86 open \u8c03\u7528<\/li>\n<\/ul>\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n<ul>\n<li>\u540c\u4e00\u8fdb\u7a0b\u7684\u4e0d\u540c\u6587\u4ef6\u63cf\u8ff0\u7b26\u53ef\u4ee5\u6307\u5411\u540c\u4e00\u4e2a\u6587\u4ef6<\/li>\n<li>\u4e0d\u540c\u8fdb\u7a0b\u53ef\u4ee5\u62e5\u6709\u76f8\u540c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26<\/li>\n<li>\u4e0d\u540c\u8fdb\u7a0b\u7684\u540c\u4e00\u6587\u4ef6\u63cf\u8ff0\u7b26\u53ef\u4ee5\u6307\u5411\u4e0d\u540c\u7684\u6587\u4ef6<\/li>\n<li>\u4e0d\u540c\u8fdb\u7a0b\u7684\u4e0d\u540c\u6587\u4ef6\u63cf\u8ff0\u7b26\u53ef\u4ee5\u6307\u5411\u540c\u4e00\u4e2a\u6587\u4ef6<\/li>\n<\/ul>\n<h3>\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u91cd\u5b9a\u5411<\/h3>\n<p>\u6bcf\u6b21\u8bfb\u5199\u8fdb\u7a0b\u7684\u65f6\u5019\uff0c\u90fd\u662f\u4ece\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0b\u624b\uff0c\u627e\u5230\u5bf9\u5e94\u7684\u6253\u5f00\u6587\u4ef6\u8868\u9879\uff0c\u518d\u627e\u5230\u5bf9\u5e94\u7684 i-node \u8868<\/p>\n<p>\ud83e\udd14\u5982\u4f55\u5b9e\u73b0\u6587\u4ef6\u63cf\u8ff0\u7b26\u91cd\u5b9a\u5411\uff1f<br \/>\u56e0\u4e3a\u5728\u6587\u4ef6\u63cf\u8ff0\u7b26\u8868\u4e2d\uff0c\u80fd\u591f\u627e\u5230\u5bf9\u5e94\u7684\u6587\u4ef6\u6307\u9488\uff0c\u5982\u679c\u6211\u4eec\u6539\u53d8\u4e86\u6587\u4ef6\u6307\u9488\uff0c\u662f\u4e0d\u662f\u540e\u7eed\u7684\u4e24\u4e2a\u8868\u5185\u5bb9\u5c31\u53d1\u751f\u4e86\u6539\u53d8<br \/>\u4f8b\u5982\uff1a\u6587\u4ef6\u63cf\u8ff0\u7b261\u6307\u5411\u7684\u663e\u793a\u5668\uff0c\u90a3\u4e48\u5c06\u6587\u4ef6\u63cf\u8ff0\u7b261\u6307\u5411 log.txt \u6587\u4ef6\uff0c\u90a3\u4e48\u6587\u4ef6\u63cf\u8ff0\u7b26 1 \u4e5f\u5c31\u548c log.txt \u5bf9\u5e94\u8d77\u6765\u4e86<\/p>\n<h4>shell \u5bf9\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u91cd\u5b9a\u5411<\/h4>\n<blockquote><p>\u662f\u8f93\u51fa\u91cd\u5b9a\u5411\u7b26\u53f7\uff0c&lt; \u662f\u8f93\u5165\u91cd\u5b9a\u5411\u7b26\u53f7\uff0c\u5b83\u4eec\u662f\u6587\u4ef6\u63cf\u8ff0\u7b26\u64cd\u4f5c\u7b26<br \/>\u548c &lt; \u901a\u8fc7\u4fee\u6539\u6587\u4ef6\u63cf\u8ff0\u7b26\u6539\u53d8\u4e86\u6587\u4ef6\u6307\u9488\u7684\u6307\u5411\uff0c\u6765\u80fd\u591f\u5b9e\u73b0\u91cd\u5b9a\u5411\u7684\u529f\u80fd<\/p><\/blockquote>\n<p>\u6211\u4eec\u4f7f\u7528<code>cat hello.txt<\/code>\u65f6\uff0c\u9ed8\u8ba4\u4f1a\u5c06\u7ed3\u679c\u8f93\u51fa\u5230\u663e\u793a\u5668\u4e0a\uff0c\u4f7f\u7528 &gt; \u6765\u91cd\u5b9a\u5411\u3002<code>cat hello.txt 1 &gt; log.txt<\/code> \u4ee5\u8f93\u51fa\u7684\u65b9\u5f0f\u6253\u5f00\u6587\u4ef6 log.txt\uff0c\u5e76\u7ed1\u5b9a\u5230\u6587\u4ef6\u63cf\u8ff0\u7b261\u4e0a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042754887\" alt=\"image\" title=\"image\" loading=\"lazy\"><\/p>\n<h4>c\u51fd\u6570\u5bf9\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u91cd\u5b9a\u5411<\/h4>\n<h5>dup<\/h5>\n<p>dup \u51fd\u6570\u662f\u7528\u6765\u6253\u5f00\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u6307\u5411\u548c oldfd \u540c\u4e00\u4e2a\u6587\u4ef6\uff0c\u5171\u4eab\u6587\u4ef6\u504f\u79fb\u91cf\u548c\u6587\u4ef6\u72b6\u6001\uff0c<\/p>\n<pre><code class=\"c\">int main(int argc, char const *argv[])\n{\n    int fd = open(&quot;log.txt&quot;);\n    int copyFd = dup(fd);\n    \/\/\u5c06fd\u9605\u8bfb\u6587\u4ef6\u7f6e\u4e8e\u6587\u4ef6\u672b\u5c3e\uff0c\u8ba1\u7b97\u504f\u79fb\u91cf\u3002\n    cout &lt;&lt; &quot;fd = &quot; &lt;&lt; fd &lt;&lt; &quot; \u504f\u79fb\u91cf\uff1a &quot; &lt;&lt; lseek(fd, 0, SEEK_END) &lt;&lt; endl;\n    \/\/\u73b0\u5728\u6211\u4eec\u8ba1\u7b97copyFd\u7684\u504f\u79fb\u91cf\n    cout &lt;&lt; &quot;copyFd = &quot; &lt;&lt; copyFd &lt;&lt; &quot;\u504f\u79fb\u91cf\uff1a&quot; &lt;&lt; lseek(copyFd, 0, SEEK_CUR) &lt;&lt; endl;\n    return 0;\n}<\/code><\/pre>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042754888\" alt=\"image\" title=\"image\" loading=\"lazy\"><\/p>\n<p>\u8c03\u7528 dup(3) \u7684\u65f6\u5019\uff0c\u4f1a\u6253\u5f00\u65b0\u7684\u6700\u5c0f\u63cf\u8ff0\u7b26\uff0c\u4e5f\u5c31\u662f4\uff0c\u8fd9\u4e2a4\u6307\u5411\u4e863\u6240\u6307\u5411\u7684\u6587\u4ef6\uff0c\u64cd\u4f5c\u4efb\u610f\u4e00\u4e2a fd \u90fd\u662f\u4fee\u6539\u7684\u4e00\u4e2a\u6587\u4ef6<\/p>\n<h5>dup2<\/h5>\n<p>dup2 \u51fd\u6570\uff0c\u628a\u6307\u5b9a\u7684 newfd \u4e5f\u6307\u5411 oldfd \u6307\u5411\u7684\u6587\u4ef6\u3002\u6267\u884c\u5b8cdup2\u4e4b\u540e\uff0cnewfd \u548c oldfd \u540c\u65f6\u6307\u5411\u540c\u4e00\u4e2a\u6587\u4ef6\uff0c\u5171\u4eab\u6587\u4ef6\u504f\u79fb\u91cf\u548c\u6587\u4ef6\u72b6\u6001<\/p>\n<pre><code class=\"c\">int main(int argc, char const *argv[])\n{\n    int oldfd = open(&quot;log.txt&quot;);\n    int newfd = open(&quot;log1.txt&quot;);\n    dup2(oldfd, newfd);\n    \/\/\u5c06fd\u9605\u8bfb\u6587\u4ef6\u7f6e\u4e8e\u6587\u4ef6\u672b\u5c3e\uff0c\u8ba1\u7b97\u504f\u79fb\u91cf\u3002\n    cout &lt;&lt; &quot;fd = &quot; &lt;&lt; fd &lt;&lt; &quot; \u504f\u79fb\u91cf\uff1a &quot; &lt;&lt; lseek(fd, 0, SEEK_END) &lt;&lt; endl;\n    \/\/\u73b0\u5728\u6211\u4eec\u8ba1\u7b97copyFd\u7684\u504f\u79fb\u91cf\n    cout &lt;&lt; &quot;copyFd = &quot; &lt;&lt; copyFd &lt;&lt; &quot;\u504f\u79fb\u91cf\uff1a&quot; &lt;&lt; lseek(copyFd, 0, SEEK_CUR) &lt;&lt; endl;\n    return 0;\n}<\/code><\/pre>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042754889\" alt=\"image\" title=\"image\" loading=\"lazy\"><\/p>\n<h2>Node\u4e2d\u901a\u4fe1\u539f\u7406<\/h2>\n<p>Node \u4e2d\u7684 IPC \u901a\u9053\u5177\u4f53\u5b9e\u73b0\u662f\u7531 libuv \u63d0\u4f9b\u7684\u3002\u6839\u636e\u7cfb\u7edf\u7684\u4e0d\u540c\u5b9e\u73b0\u65b9\u5f0f\u4e0d\u540c\uff0cwindow \u4e0b\u91c7\u7528\u547d\u540d\u7ba1\u9053\u5b9e\u73b0\uff0c*nix \u4e0b\u91c7\u7528 Domain Socket \u5b9e\u73b0\u3002\u5728\u5e94\u7528\u5c42\u53ea\u4f53\u73b0\u4e3a message \u4e8b\u4ef6\u548c send \u65b9\u6cd5\u3002\u53c2\u8003nodejs\u8fdb\u9636\u89c6\u9891\u8bb2\u89e3\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=Bk8hGOkocSyGSnKRYsDmtA%3D%3D.RCcx%2F20zSSDqTVOEayLrPdj9xeA%2FoHtiIAiWivWVXsRYuzQ0EN%2F%2FfDgn9VkFJuON\" rel=\"nofollow\">\u8fdb\u5165\u5b66\u4e60<\/a><\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042754890\" alt=\"image\" title=\"image\" loading=\"lazy\"><\/p>\n<p>\u7236\u8fdb\u7a0b\u5728\u5b9e\u9645\u521b\u5efa\u5b50\u8fdb\u7a0b\u4e4b\u524d\uff0c\u4f1a\u521b\u5efa IPC \u901a\u9053\u5e76\u76d1\u542c\u5b83\uff0c\u7b49\u5230\u521b\u5efa\u51fa\u771f\u5b9e\u7684\u5b50\u8fdb\u7a0b\u540e\uff0c\u901a\u8fc7\u73af\u5883\u53d8\u91cf(NODE_CHANNEL_FD)\u544a\u8bc9\u5b50\u8fdb\u7a0b\u8be5 IPC \u901a\u9053\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/p>\n<p>\u5b50\u8fdb\u7a0b\u5728\u542f\u52a8\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u6839\u636e\u8be5\u6587\u4ef6\u63cf\u8ff0\u7b26\u53bb\u8fde\u63a5 IPC \u901a\u9053\uff0c\u4ece\u800c\u5b8c\u6210\u7236\u5b50\u8fdb\u7a0b\u7684\u8fde\u63a5\u3002<\/p>\n<p>\u5efa\u7acb\u8fde\u63a5\u4e4b\u540e\u53ef\u4ee5\u81ea\u7531\u7684\u901a\u4fe1\u4e86\uff0cIPC \u901a\u9053\u662f\u4f7f\u7528\u547d\u540d\u7ba1\u9053\u6216\u8005 Domain Socket \u521b\u5efa\u7684\uff0c\u5c5e\u4e8e\u53cc\u5411\u901a\u4fe1\u3002\u5e76\u4e14\u5b83\u662f\u5728\u7cfb\u7edf\u5185\u6838\u4e2d\u5b8c\u6210\u7684\u8fdb\u7a0b\u901a\u4fe1<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042754891\" alt=\"image\" title=\"image\" loading=\"lazy\"><\/p>\n<p>\u26a0\ufe0f \u53ea\u6709\u5728\u542f\u52a8\u7684\u5b50\u8fdb\u7a0b\u662f Node \u8fdb\u7a0b\u65f6\uff0c\u5b50\u8fdb\u7a0b\u624d\u4f1a\u6839\u636e\u73af\u5883\u53d8\u91cf\u53bb\u8fde\u63a5\u5bf9\u5e94\u7684 IPC \u901a\u9053\uff0c\u5bf9\u4e8e\u5176\u4ed6\u7c7b\u578b\u7684\u5b50\u8fdb\u7a0b\u5219\u65e0\u6cd5\u5b9e\u73b0\u8fdb\u7a0b\u95f4\u901a\u4fe1\uff0c\u9664\u975e\u5176\u4ed6\u8fdb\u7a0b\u4e5f\u6309\u7740\u8be5\u7ea6\u5b9a\u53bb\u8fde\u63a5\u8fd9\u4e2a IPC \u901a\u9053\u3002<\/p>\n<h3>unix domain socket<\/h3>\n<h4>\u662f\u4ec0\u4e48<\/h4>\n<p>\u6211\u4eec\u77e5\u9053\u7ecf\u5178\u7684\u901a\u4fe1\u65b9\u5f0f\u662f\u6709 Socket\uff0c\u6211\u4eec\u5e73\u65f6\u719f\u77e5\u7684 Socket \u662f\u57fa\u4e8e\u7f51\u7edc\u534f\u8bae\u7684\uff0c\u7528\u4e8e\u4e24\u4e2a\u4e0d\u540c\u4e3b\u673a\u4e0a\u7684\u4e24\u4e2a\u8fdb\u7a0b\u901a\u4fe1\uff0c\u901a\u4fe1\u9700\u8981\u6307\u5b9a IP\/Host \u7b49\u3002<br \/>\u4f46\u5982\u679c\u6211\u4eec\u540c\u4e00\u53f0\u4e3b\u673a\u4e0a\u7684\u4e24\u4e2a\u8fdb\u7a0b\u60f3\u8981\u901a\u4fe1\uff0c\u5982\u679c\u4f7f\u7528 Socket \u9700\u8981\u6307\u5b9a IP\/Host\uff0c\u7ecf\u8fc7\u7f51\u7edc\u534f\u8bae\u7b49\uff0c\u4f1a\u663e\u5f97\u8fc7\u4e8e\u7e41\u7410\u3002\u6240\u4ee5 Unix Domain Socket \u8bde\u751f\u4e86\u3002<\/p>\n<p>UDS \u7684\u4f18\u52bf\uff1a<\/p>\n<ul>\n<li>\u7ed1\u5b9a socket \u6587\u4ef6\u800c\u4e0d\u662f\u7ed1\u5b9a IP\/Host\uff1b\u4e0d\u9700\u8981\u7ecf\u8fc7\u7f51\u7edc\u534f\u8bae\uff0c\u800c\u662f\u6570\u636e\u7684\u62f7\u8d1d<\/li>\n<li>\u4e5f\u652f\u6301 SOCK_STREAM(\u6d41\u5957\u63a5\u5b57)\u548c SOCK_DGRAM(\u6570\u636e\u5305\u5957\u63a5\u5b57)\uff0c\u4f46\u7531\u4e8e\u662f\u5728\u672c\u673a\u901a\u8fc7\u5185\u6838\u901a\u4fe1\uff0c\u4e0d\u4f1a\u4e22\u5305\u4e5f\u4e0d\u4f1a\u51fa\u73b0\u53d1\u9001\u5305\u7684\u6b21\u5e8f\u548c\u63a5\u6536\u5305\u7684\u6b21\u5e8f\u4e0d\u4e00\u81f4\u7684\u95ee\u9898<\/li>\n<\/ul>\n<h4>\u5982\u4f55\u5b9e\u73b0<\/h4>\n<h5>\u6d41\u7a0b\u56fe<\/h5>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042754892\" alt=\"image\" title=\"image\" loading=\"lazy\"><\/p>\n<h5>Server \u7aef<\/h5>\n<pre><code class=\"c++\">int main(int argc, char *argv[])\n{\n    int server_fd ,ret, client_fd;\n    struct sockaddr_un serv, client;\n    socklen_t len = sizeof(client);\n    char buf[1024] = {0};\n    int recvlen;\n\n    \/\/ \u521b\u5efa socket\n    server_fd = socket(AF_LOCAL, SOCK_STREAM, 0);\n\n    \/\/ \u521d\u59cb\u5316 server \u4fe1\u606f\n    serv.sun_family = AF_LOCAL;\n    strcpy(serv.sun_path, &quot;server.sock&quot;);\n\n    \/\/ \u7ed1\u5b9a\n    ret = bind(server_fd, (struct sockaddr *)&amp;serv, sizeof(serv));\n\n    \/\/\u8bbe\u7f6e\u76d1\u542c\uff0c\u8bbe\u7f6e\u80fd\u591f\u540c\u65f6\u548c\u670d\u52a1\u7aef\u8fde\u63a5\u7684\u5ba2\u6237\u7aef\u6570\u91cf\n    ret = listen(server_fd, 36);\n\n    \/\/\u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5\n    client_fd = accept(server_fd, (struct sockaddr *)&amp;client, &amp;len);\n    printf(&quot;=====client bind file:%s\/n&quot;, client.sun_path);\n\n    while (1) {\n        recvlen = recv(client_fd, buf, sizeof(buf), 0);\n        if (recvlen == -1) {\n            perror(&quot;recv error&quot;);\n            return -1;\n        } else if (recvlen == 0) {\n            printf(&quot;client disconnet...\/n&quot;);\n            close(client_fd);\n            break;\n        } else {\n            printf(&quot;recv buf %s\/n&quot;, buf);\n            send(client_fd, buf, recvlen, 0);\n        }\n    }\n\n    close(client_fd);\n    close(server_fd);\n    return 0;\n}<\/code><\/pre>\n<h5>Client \u7aef<\/h5>\n<pre><code class=\"c++\">int main(int argc, char *argv[])\n{\n    int client_fd ,ret;\n    struct sockaddr_un serv, client;\n    socklen_t len = sizeof(client);\n    char buf[1024] = {0};\n    int recvlen;\n\n    \/\/\u521b\u5efasocket\n    client_fd = socket(AF_LOCAL, SOCK_STREAM, 0);\n\n    \/\/\u7ed9\u5ba2\u6237\u7aef\u7ed1\u5b9a\u4e00\u4e2a\u5957\u63a5\u5b57\u6587\u4ef6\n    client.sun_family = AF_LOCAL;\n    strcpy(client.sun_path, &quot;client.sock&quot;);\n    ret = bind(client_fd, (struct sockaddr *)&amp;client, sizeof(client));\n\n    \/\/\u521d\u59cb\u5316server\u4fe1\u606f\n    serv.sun_family = AF_LOCAL;\n    strcpy(serv.sun_path, &quot;server.sock&quot;);\n    \/\/\u8fde\u63a5\n    connect(client_fd, (struct sockaddr *)&amp;serv, sizeof(serv));\n\n    while (1) {\n        fgets(buf, sizeof(buf), stdin);\n        send(client_fd, buf, strlen(buf)+1, 0);\n\n        recv(client_fd, buf, sizeof(buf), 0);\n        printf(&quot;recv buf %s\/n&quot;, buf);\n    }\n\n    close(client_fd);\n    return 0;\n}<\/code><\/pre>\n<h3>\u547d\u540d\u7ba1\u9053(Named Pipe)<\/h3>\n<h4>\u662f\u4ec0\u4e48<\/h4>\n<p>\u547d\u540d\u7ba1\u9053\u662f\u53ef\u4ee5\u5728\u540c\u4e00\u53f0\u8ba1\u7b97\u673a\u7684\u4e0d\u540c\u8fdb\u7a0b\u4e4b\u95f4\uff0c\u6216\u8005\u8de8\u8d8a\u4e00\u4e2a\u7f51\u7edc\u7684\u4e0d\u540c\u8ba1\u7b97\u673a\u7684\u4e0d\u540c\u8fdb\u7a0b\u4e4b\u95f4\u7684\u53ef\u9760\u7684\u5355\u5411\u6216\u8005\u53cc\u5411\u7684\u6570\u636e\u901a\u4fe1\u3002<br \/>\u521b\u5efa\u547d\u540d\u7ba1\u9053\u7684\u8fdb\u7a0b\u88ab\u79f0\u4e3a\u7ba1\u9053\u670d\u52a1\u7aef(Pipe Server)\uff0c\u8fde\u63a5\u5230\u8fd9\u4e2a\u7ba1\u9053\u7684\u8fdb\u7a0b\u79f0\u4e3a\u7ba1\u9053\u5ba2\u6237\u7aef(Pipe Client)\u3002<\/p>\n<p>\u547d\u540d\u7ba1\u9053\u7684\u547d\u540d\u89c4\u8303\uff1a\/server\/pipe[\/path]\/name<\/p>\n<ul>\n<li>\u5176\u4e2d server \u6307\u5b9a\u4e00\u4e2a\u670d\u52a1\u5668\u7684\u540d\u5b57\uff0c\u672c\u673a\u9002\u7528 . \u8868\u793a\uff0c\/192.10.10.1 \u8868\u793a\u7f51\u7edc\u4e0a\u7684\u670d\u52a1\u5668<\/li>\n<li>\/pipe \u662f\u4e00\u4e2a\u4e0d\u53ef\u53d8\u5316\u7684\u5b57\u4e32\uff0c\u7528\u4e8e\u6307\u5b9a\u8be5\u6587\u4ef6\u5c5e\u4e8e NPFS(Named Pipe File System)<\/li>\n<li>[\/path]\/name \u662f\u552f\u4e00\u547d\u540d\u7ba1\u9053\u540d\u79f0\u7684\u6807\u8bc6<\/li>\n<\/ul>\n<h4>\u600e\u4e48\u5b9e\u73b0<\/h4>\n<h5>\u6d41\u7a0b\u56fe<\/h5>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042754893\" alt=\"image\" title=\"image\" loading=\"lazy\"><\/p>\n<h5>Pipe Server<\/h5>\n<pre><code class=\"c++\">void ServerTest()\n{\n    HANDLE  serverNamePipe;\n    char    pipeName[MAX_PATH] = {0};\n    char    szReadBuf[MAX_BUFFER] = {0};\n    char    szWriteBuf[MAX_BUFFER] = {0};\n    DWORD   dwNumRead = 0;\n    DWORD   dwNumWrite = 0;\n\n    strcpy(pipeName, &quot;\/\/\/\/.\/\/pipe\/\/shuangxuPipeTest&quot;);\n    \/\/ \u521b\u5efa\u7ba1\u9053\u5b9e\u4f8b\n    serverNamePipe = CreateNamedPipeA(pipeName,\n        PIPE_ACCESS_DUPLEX|FILE_FLAG_WRITE_THROUGH,\n        PIPE_TYPE_BYTE|PIPE_READMODE_BYTE|PIPE_WAIT,\n        PIPE_UNLIMITED_INSTANCES, 0, 0, 0, NULL);\n    WriteLog(&quot;\u521b\u5efa\u7ba1\u9053\u6210\u529f...&quot;);\n    \/\/ \u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5\n    BOOL bRt= ConnectNamedPipe(serverNamePipe, NULL );\n    WriteLog( &quot;\u6536\u5230\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u6210\u529f...&quot;);\n    \/\/ \u63a5\u6536\u6570\u636e\n    memset( szReadBuf, 0, MAX_BUFFER );\n    bRt = ReadFile(serverNamePipe, szReadBuf, MAX_BUFFER-1, &amp;dwNumRead, NULL );\n    \/\/ \u4e1a\u52a1\u903b\u8f91\u5904\u7406 \uff08\u53ea\u4e3a\u6d4b\u8bd5\u7528\u8fd4\u56de\u539f\u6765\u7684\u6570\u636e\uff09\n    WriteLog( &quot;\u6536\u5230\u5ba2\u6237\u6570\u636e:[%s]&quot;, szReadBuf);\n    \/\/ \u53d1\u9001\u6570\u636e\n    if( !WriteFile(serverNamePipe, szWriteBuf, dwNumRead, &amp;dwNumWrite, NULL ) )\n    {\n        WriteLog(&quot;\u5411\u5ba2\u6237\u5199\u5165\u6570\u636e\u5931\u8d25:[%#x]&quot;, GetLastError());\n        return ;\n    }\n    WriteLog(&quot;\u5199\u5165\u6570\u636e\u6210\u529f...&quot;);\n}<\/code><\/pre>\n<h5>Pipe Client<\/h5>\n<pre><code class=\"c++\">void ClientTest()\n{\n    char    pipeName[MAX_PATH] = {0};\n    HANDLE  clientNamePipe;\n    DWORD   dwRet;\n    char    szReadBuf[MAX_BUFFER] = {0};\n    char    szWriteBuf[MAX_BUFFER] = {0};\n    DWORD   dwNumRead = 0;\n    DWORD   dwNumWrite = 0;\n\n    strcpy(pipeName, &quot;\/\/\/\/.\/\/pipe\/\/shuangxuPipeTest&quot;);\n    \/\/ \u68c0\u6d4b\u7ba1\u9053\u662f\u5426\u53ef\u7528\n    if(!WaitNamedPipeA(pipeName, 10000)){\n        WriteLog(&quot;\u7ba1\u9053[%s]\u65e0\u6cd5\u6253\u5f00&quot;, pipeName);\n        return ;\n    }\n    \/\/ \u8fde\u63a5\u7ba1\u9053\n    clientNamePipe = CreateFileA(pipeName,\n        GENERIC_READ|GENERIC_WRITE,\n        0,\n        NULL,\n        OPEN_EXISTING,\n        FILE_ATTRIBUTE_NORMAL,\n        NULL);\n    WriteLog(&quot;\u7ba1\u9053\u8fde\u63a5\u6210\u529f...&quot;);\n    scanf( &quot;%s&quot;, szWritebuf );\n    \/\/ \u53d1\u9001\u6570\u636e\n    if( !WriteFile(clientNamePipe, szWriteBuf, strlen(szWriteBuf), &amp;dwNumWrite, NULL)){\n        WriteLog(&quot;\u53d1\u9001\u6570\u636e\u5931\u8d25,GetLastError=[%#x]&quot;, GetLastError());\n        return ;\n    }\n    printf(&quot;\u53d1\u9001\u6570\u636e\u6210\u529f:%s\/n&quot;, szWritebuf );\n    \/\/ \u63a5\u6536\u6570\u636e\n    if( !ReadFile(clientNamePipe, szReadBuf, MAX_BUFFER-1, &amp;dwNumRead, NULL)){\n        WriteLog(&quot;\u63a5\u6536\u6570\u636e\u5931\u8d25,GetLastError=[%#x]&quot;, GetLastError() );\n        return ;\n    }\n    WriteLog( &quot;\u63a5\u6536\u5230\u670d\u52a1\u5668\u8fd4\u56de:%s&quot;, szReadBuf );\n    \/\/ \u5173\u95ed\u7ba1\u9053\n    CloseHandle(clientNamePipe);\n}<\/code><\/pre>\n<h2>Node \u521b\u5efa\u5b50\u8fdb\u7a0b\u7684\u6d41\u7a0b<\/h2>\n<h3>Unix<\/h3>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042754894\" alt=\"image\" title=\"image\" loading=\"lazy\"><\/p>\n<p>\u5bf9\u4e8e\u521b\u5efa\u5b50\u8fdb\u7a0b\u3001\u521b\u5efa\u7ba1\u9053\u3001\u91cd\u5b9a\u5411\u7ba1\u9053\u5747\u662f\u5728 c++ \u5c42\u5b9e\u73b0\u7684<\/p>\n<h4>\u521b\u5efa\u5b50\u8fdb\u7a0b<\/h4>\n<pre><code class=\"c\">int main(int argc,char *argv[]){\n    pid_t pid = fork();\n    if (pid &lt; 0) {\n        \/\/ \u9519\u8bef\n    } else if(pid == 0) {\n        \/\/ \u5b50\u8fdb\u7a0b\n    } else {\n        \/\/ \u7236\u8fdb\u7a0b\n    }\n}<\/code><\/pre>\n<h4>\u521b\u5efa\u7ba1\u9053<\/h4>\n<p>\u4f7f\u7528 socketpair \u521b\u5efa\u7ba1\u9053\uff0c\u5176\u521b\u5efa\u51fa\u6765\u7684\u7ba1\u9053\u662f\u5168\u53cc\u5de5\u7684\uff0c\u8fd4\u56de\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e2d\u7684\u4efb\u4f55\u4e00\u4e2a\u90fd\u53ef\u8bfb\u548c\u53ef\u5199<\/p>\n<pre><code class=\"c\">int main ()\n{\n    int fd[2];\n    int r = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);\n\n    if (fork()){ \/* \u7236\u8fdb\u7a0b *\/\n        int val = 0;\n        close(fd[1]);\n        while (1){\n            sleep(1);\n            ++val;\n            printf(&quot;\u53d1\u9001\u6570\u636e: %d\/n&quot;, val);\n            write(fd[0], &amp;val, sizeof(val));\n            read(fd[0], &amp;val, sizeof(val));\n            printf(&quot;\u63a5\u6536\u6570\u636e: %d\/n&quot;, val);\n        }\n    } else {  \/*\u5b50\u8fdb\u7a0b*\/\n        int val;\n        close(fd[0]);\n        while(1){\n            read(fd[1], &amp;val, sizeof(val));\n            ++val;\n            write(fd[1], &amp;val, sizeof(val));\n        }\n    }\n}<\/code><\/pre>\n<p>\u5f53\u6211\u4eec\u4f7f\u7528 socketpair \u521b\u5efa\u4e86\u7ba1\u9053\u4e4b\u540e\uff0c\u7236\u8fdb\u7a0b\u5173\u95ed\u4e86 fd[1]\uff0c\u5b50\u8fdb\u7a0b\u5173\u95ed\u4e86 fd[0]\u3002\u5b50\u8fdb\u7a0b\u53ef\u4ee5\u901a\u8fc7 fd[1] \u8bfb\u5199\u6570\u636e\uff1b\u540c\u7406\u4e3b\u8fdb\u7a0b\u901a\u8fc7 fd[0]\u8bfb\u5199\u6570\u636e\u5b8c\u6210\u901a\u4fe1\u3002<\/p>\n<h2>child_process.fork \u7684\u8be6\u7ec6\u8c03\u7528<\/h2>\n<p>fork \u51fd\u6570\u5f00\u542f\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u7684\u6d41\u7a0b<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042754896\" alt=\"image\" title=\"image\" loading=\"lazy\"><\/p>\n<ul>\n<li>\n<p>\u521d\u59cb\u5316\u53c2\u6570\u4e2d\u7684 options.stdio\uff0c\u5e76\u4e14\u8c03\u7528 spawn \u51fd\u6570<\/p>\n<pre><code class=\"javascript\">function spawn(file, args, options) {\n  const child = new ChildProcess();\n\n  child.spawn(options);\n}<\/code><\/pre>\n<\/li>\n<li>\n<p>\u521b\u5efa ChildProcess \u5b9e\u4f8b\uff0c\u521b\u5efa\u5b50\u8fdb\u7a0b\u4e5f\u662f\u8c03\u7528 C++ \u5c42 this._handle.spawn \u65b9\u6cd5<\/p>\n<pre><code class=\"javascript\">function ChildProcess() {\n    \/\/ C++\u5c42\u5b9a\u4e49\n    this._handle = new Process();\n}<\/code><\/pre>\n<\/li>\n<li>\n<p>\u901a\u8fc7 child.spawn \u8c03\u7528\u5230 ChildProcess.prototype.spawn \u65b9\u6cd5\u4e2d\u3002\u5176\u4e2d getValidStdio \u65b9\u6cd5\u4f1a\u6839\u636e options.stdio \u521b\u5efa\u548c C++ \u4ea4\u4e92\u7684 Pipe \u5bf9\u8c61\uff0c\u5e76\u83b7\u5f97\u5bf9\u5e94\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u5c06\u6587\u4ef6\u63cf\u8ff0\u7b26\u5199\u5165\u5230\u73af\u5883\u53d8\u91cf NODE_CHANNEL_FD \u4e2d\uff0c\u8c03\u7528 C++ \u5c42\u521b\u5efa\u5b50\u8fdb\u7a0b\uff0c\u5728\u8c03\u7528 setupChannel \u65b9\u6cd5<\/p>\n<pre><code class=\"javascript\">ChildProcess.prototype.spawn = function(options) {\n  \/\/ \u9884\u5904\u7406\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u6570\u636e\u7ed3\u6784\n    stdio = getValidStdio(stdio, false);\n    const ipc = stdio.ipc;\n    const ipcFd = stdio.ipcFd;\n    \/\/\u5c06\u6587\u4ef6\u63cf\u8ff0\u7b26\u5199\u5165\u73af\u5883\u53d8\u91cf\u4e2d\n    if (ipc !== undefined) {\n    ArrayPrototypePush(options.envPairs, `NODE_CHANNEL_FD=${ipcFd}`);\n  }\n    \/\/ \u521b\u5efa\u8fdb\u7a0b\n    const err = this._handle.spawn(options);\n    \/\/ \u6dfb\u52a0send\u65b9\u6cd5\u548c\u76d1\u542cIPC\u4e2d\u6570\u636e\n    if (ipc !== undefined) setupChannel(this, ipc, serialization);\n}<\/code><\/pre>\n<\/li>\n<li>\n<p>\u5b50\u8fdb\u7a0b\u542f\u52a8\u65f6\uff0c\u4f1a\u6839\u636e\u73af\u5883\u53d8\u91cf\u4e2d\u662f\u5426\u5b58\u5728 NODE_CHANNEL_FD \u5224\u65ad\u662f\u5426\u8c03\u7528 _forkChild \u65b9\u6cd5\uff0c\u521b\u5efa\u4e00\u4e2a Pipe \u5bf9\u8c61, \u540c\u65f6\u8c03\u7528 open \u65b9\u6cd5\u6253\u5f00\u5bf9\u5e94\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u5728\u8c03\u7528setupChannel<\/p>\n<pre><code class=\"javascript\">function _forkChild(fd, serializationMode) {\n  const p = new Pipe(PipeConstants.IPC);\n  p.open(fd);\n  p.unref();\n  const control = setupChannel(process, p, serializationMode);\n}<\/code><\/pre>\n<\/li>\n<\/ul>\n<h2>\u53e5\u67c4\u4f20\u9012<\/h2>\n<p>setupChannel<br \/>\u4e3b\u8981\u662f\u5b8c\u6210\u4e86\u5904\u7406\u63a5\u6536\u7684\u6d88\u606f\u3001\u53d1\u9001\u6d88\u606f\u3001\u5904\u7406\u6587\u4ef6\u63cf\u8ff0\u7b26\u4f20\u9012\u7b49<\/p>\n<pre><code class=\"javascript\">function setipChannel(){\n    channel.onread = function(arrayBuffer){\n        \/\/...\n    }\n    target.on('internalMessage', function(message, handle){\n        \/\/...\n    })\n    target.send = function(message, handle, options, callback){\n        \/\/...\n    }\n    target._send = function(message, handle, options, callback){\n        \/\/...\n    }\n    function handleMessage(message, handle, internal){\n        \/\/...\n    }\n}<\/code><\/pre>\n<ul>\n<li>target.send: process.send \u65b9\u6cd5\uff0c\u8fd9\u91cc target \u5c31\u662f\u8fdb\u7a0b\u5bf9\u8c61\u672c\u8eab.<\/li>\n<li>target._send: \u6267\u884c\u5177\u4f53 send \u903b\u8f91\u7684\u51fd\u6570, \u5f53\u53c2\u6570 handle \u4e0d\u5b58\u5728\u65f6, \u8868\u793a\u666e\u901a\u7684\u6d88\u606f\u4f20\u9012\uff1b\u82e5\u5b58\u5728\uff0c\u5305\u88c5\u4e3a\u5185\u90e8\u5bf9\u8c61\uff0c\u8868\u660e\u662f\u4e00\u4e2a internalMessage \u4e8b\u4ef6\u89e6\u53d1\u3002\u8c03\u7528\u4f7f\u7528JSON.stringify \u5e8f\u5217\u5316\u5bf9\u8c61, \u4f7f\u7528channel.writeUtf8String \u5199\u5165\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e2d<\/li>\n<li>channel.onread: \u83b7\u53d6\u5230\u6570\u636e\u65f6\u89e6\u53d1, \u8ddf channel.writeUtf8String \u76f8\u5bf9\u5e94\u3002\u901a\u8fc7 JSON.parse \u53cd\u5e8f\u5217\u5316 message \u4e4b\u540e, \u8c03\u7528 handleMessage \u8fdb\u800c\u89e6\u53d1\u5bf9\u5e94\u4e8b\u4ef6<\/li>\n<li>handleMessage: \u7528\u6765\u5224\u65ad\u662f\u89e6\u53d1 message \u4e8b\u4ef6\u8fd8\u662f internalMessage \u4e8b\u4ef6<\/li>\n<li>target.on(&#8216;internalMessage&#8217;): \u9488\u5bf9\u5185\u90e8\u5bf9\u8c61\u505a\u7279\u6b8a\u5904\u7406\uff0c\u5728\u8c03\u7528 message \u4e8b\u4ef6<\/li>\n<\/ul>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042754897\" alt=\"image\" title=\"image\" loading=\"lazy\"><\/p>\n<p>\u8fdb\u7a0b\u95f4\u6d88\u606f\u4f20\u9012<\/p>\n<ul>\n<li>\u7236\u8fdb\u7a0b\u901a\u8fc7 child.send \u53d1\u9001\u6d88\u606f \u548c server\/socket \u53e5\u67c4\u5bf9\u8c61<\/li>\n<li>\n<p>\u666e\u901a\u6d88\u606f\u76f4\u63a5 JSON.stringify \u5e8f\u5217\u5316\uff1b\u5bf9\u4e8e\u53e5\u67c4\u5bf9\u8c61\u6765\u8bf4\uff0c\u9700\u8981\u5148\u5305\u88c5\u6210\u4e3a\u5185\u90e8\u5bf9\u8c61<\/p>\n<pre><code class=\"javascript\">message = {\n    cmd: 'NODE_HANDLE',\n    type: null,\n    msg: message\n};<\/code><\/pre>\n<p>\u901a\u8fc7 handleConversion.[message.type].send \u7684\u65b9\u6cd5\u53d6\u51fa\u53e5\u67c4\u5bf9\u8c61\u5bf9\u5e94\u7684 C++ \u5c42\u9762\u7684 TCP \u5bf9\u8c61\uff0c\u5728\u91c7\u7528JSON.stringify \u5e8f\u5217\u5316<\/p>\n<pre><code class=\"javascript\">const handleConversion = {\n    'net.Server': {\n    simultaneousAccepts: true,\n\n    send(message, server, options) {\n      return server._handle;\n    },\n\n    got(message, handle, emit) {\n      const server = new net.Server();\n      server.listen(handle, () =&gt; {\n        emit(server);\n      });\n    }\n  }\n\/\/....\n}<\/code><\/pre>\n<\/li>\n<li>\u6700\u540e\u5c06\u5e8f\u5217\u5316\u540e\u7684\u5185\u90e8\u5bf9\u8c61\u548c TCP \u5bf9\u8c61\u5199\u5165\u5230 IPC \u901a\u9053\u4e2d<\/li>\n<li>\u5b50\u8fdb\u7a0b\u5728\u63a5\u6536\u5230\u6d88\u606f\u4e4b\u540e\uff0c\u4f7f\u7528 JSON.parse \u53cd\u5e8f\u5217\u5316\u6d88\u606f\uff0c\u5982\u679c\u4e3a\u5185\u90e8\u5bf9\u8c61\u89e6\u53d1 internalMessage \u4e8b\u4ef6<\/li>\n<li>\u68c0\u67e5\u662f\u5426\u5e26\u6709 TCP \u5bf9\u8c61\uff0c\u901a\u8fc7 handleConversion.[message.type].got \u5f97\u5230\u548c\u7236\u8fdb\u7a0b\u4e00\u6837\u7684\u53e5\u67c4\u5bf9\u8c61<\/li>\n<li>\u6700\u540e\u53d1\u89e6\u53d1 message \u4e8b\u4ef6\u4f20\u9012\u5904\u7406\u597d\u7684\u6d88\u606f\u548c\u53e5\u67c4\u5bf9\u8c61\uff0c\u5b50\u8fdb\u7a0b\u901a\u8fc7 process.on \u63a5\u6536<\/li>\n<\/ul>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042754884\">https:\/\/segmentfault.com\/a\/1190000042754884<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u7f6e\u77e5\u8bc6 \u6587\u4ef6\u63cf\u8ff0\u7b26 \u5728 Linux \u7cfb\u7edf\u4e2d\uff0c\u4e00\u5207\u90fd\u770b\u6210\u6587\u4ef6\uff0c\u5f53\u8fdb\u7a0b\u6253\u5f00\u73b0\u6709\u6587\u4ef6\u65f6\uff0c\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002\u6587\u4ef6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10006822","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006822","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006822"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006822\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}