{"id":24050,"date":"2024-11-21T12:52:01","date_gmt":"2024-11-21T04:52:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24050\/"},"modified":"2024-11-21T12:52:01","modified_gmt":"2024-11-21T04:52:01","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8swoole%e4%b8%ad%e4%bd%bf%e7%94%a8%e5%8d%8f%e7%a8%8b%e5%ae%9e%e7%8e%b0%e9%ab%98%e5%b9%b6%e5%8f%91%e7%9a%84swoole_pop3%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24050\/","title":{"rendered":"\u5982\u4f55\u5728Swoole\u4e2d\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684swoole_pop3\u51fd\u6570"},"content":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u7684\u4e0d\u65ad\u53d1\u5c55\uff0c\u9ad8\u5e76\u53d1\u5df2\u7ecf\u6210\u4e3a\u4e86\u73b0\u4ee3\u4e92\u8054\u7f51\u5e94\u7528\u4e2d\u7684\u91cd\u8981\u95ee\u9898\u4e4b\u4e00\u3002\u5728\u7f51\u7edc\u5e94\u7528\u4e2d\uff0cpop3\u534f\u8bae\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u7535\u5b50\u90ae\u4ef6\u6536\u53d1\u534f\u8bae\uff0c\u56e0\u6b64\u5728\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684pop3\u5e94\u7528\u65f6\uff0c\u4f7f\u7528\u534f\u7a0b\u6210\u4e3a\u4e86\u4e00\u79cd\u6709\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728\u4e2d\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684swoole_pop3\u51fd\u6570\u3002<\/p>\n<p>\u4e00\u3001POP3\u57fa\u7840\u77e5\u8bc6<\/p>\n<p>POP3\u534f\u8bae\u662f\u4e00\u79cd\u7528\u4e8e\u90ae\u4ef6\u6536\u53d6\u7684\u6807\u51c6\u534f\u8bae\u3002POP3\u670d\u52a1\u5668\u662f\u90ae\u4ef6\u670d\u52a1\u5668\u4e0a\u7684\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u5b83\u7684\u4e3b\u8981\u529f\u80fd\u662f\u63a5\u6536\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42\uff0c\u6839\u636e\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u8fdb\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\uff0c\u6700\u7ec8\u5c06\u90ae\u4ef6\u4f20\u9001\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n<p>POP3\u534f\u8bae\u7684\u57fa\u672c\u5de5\u4f5c\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n<p>1\u3001\u5ba2\u6237\u7aef\u5411POP3\u670d\u52a1\u5668\u53d1\u9001\u8fde\u63a5\u8bf7\u6c42<\/p>\n<p>2\u3001POP3\u670d\u52a1\u5668\u63a5\u53d7\u8bf7\u6c42\u540e\uff0c\u5411\u5ba2\u6237\u7aef\u53d1\u9001\u6b22\u8fce\u6d88\u606f<\/p>\n<p>3\u3001\u5ba2\u6237\u7aef\u53d1\u9001\u7528\u6237\u540d\u548c\u5bc6\u7801<\/p>\n<p>4\u3001POP3\u670d\u52a1\u5668\u9a8c\u8bc1\u7528\u6237\u540d\u548c\u5bc6\u7801\uff0c\u8fd4\u56de\u6210\u529f\u6216\u5931\u8d25\u6d88\u606f<\/p>\n<p>5\u3001\u5982\u679c\u9a8c\u8bc1\u6210\u529f\uff0c\u5ba2\u6237\u7aef\u53ef\u4ee5\u53d1\u9001\u4e00\u4e9b\u547d\u4ee4\u7ed9POP3\u670d\u52a1\u5668\uff0c\u5982LIST\u3001RETR\u7b49<\/p>\n<p>6\u3001POP3\u670d\u52a1\u5668\u6839\u636e\u547d\u4ee4\u8fd4\u56de\u76f8\u5e94\u7684\u7ed3\u679c<\/p>\n<p>7\u3001\u5ba2\u6237\u7aef\u5173\u95ed\u8fde\u63a5<\/p>\n<p>\u4e8c\u3001swoole_pop3\u51fd\u6570\u5b9e\u73b0<\/p>\n<p>\u5728Swoole\u4e2d\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e2apop3\u670d\u52a1\u5668\u7684\u793a\u4f8b\uff0c\u4f7f\u7528swoole_server\u5b9e\u73b0\u3002\u5728\u6b64\u57fa\u7840\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06POP3\u670d\u52a1\u5668\u7684\u5904\u7406\u903b\u8f91\uff0c\u4ee5\u53caPOP3\u534f\u8bae\u7684\u89e3\u6790\u548c\u7ec4\u88c5\u5199\u5230swoole_pop3\u51fd\u6570\u4e2d\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php function swoole_pop3($host, $port, $username, $password, $callback)\n{\n    $server = new SwooleServer($host, $port, SWOOLE_BASE, SWOOLE_SOCK_TCP);\n    \n    $server-&gt;on('receive', function($server, $fd, $reactor_id, $data) use ($username, $password, $callback) {\n        $pop3 = new POP3($username, $password);\n        \n        $response = $pop3-&amp;gt;command($data);\n        \n        $server-&amp;gt;send($fd, $response);\n        \n        if ($response == \"+OK conection closed\") {\n            $server-&amp;gt;close($fd);\n            \n            $callback();\n        }\n    });\n    \n    $server-&amp;gt;start();\n}\n\nclass POP3\n{\n    private $username;\n    private $password;\n    private $connected = false;\n    private $command_history = array();\n\n    function __construct($username, $password)\n    {\n        $this-&amp;gt;username = $username;\n        $this-&amp;gt;password = $password;\n    }\n\n    function command($command_str)\n    {\n        $command = $this-&amp;gt;parse_command($command_str);\n\n        $command_name = strtoupper($command['name']);\n        $command_args = isset($command['args']) ? $command['args'] : array();\n\n        if ($command_name == \"USER\") {\n            $username = $command_args[0];\n            if ($username == $this-&amp;gt;username) {\n                return \"+OK Password required \n\";\n            } else {\n                return \"-ERR User not found \n\";\n            }\n        } elseif ($command_name == \"PASS\") {\n            $password = $command_args[0];\n            if ($password == $this-&amp;gt;password) {\n                $this-&amp;gt;connected = true;\n                return \"+OK connected \n\";\n            } else {\n                return \"-ERR Password incorrect \n\";\n            }\n        } else {\n            return \"-ERR command not supported \n\";\n        }\n    }\n\n    function parse_command($command_str)\n    {\n        $command_str = trim($command_str);\n        $command = array();\n\n        $name_end_pos = strpos($command_str, ' ');\n\n        if ($name_end_pos === false) {\n            $command['name'] = $command_str;\n        } else {\n            $command['name'] = substr($command_str, 0, $name_end_pos);\n            $args_str = substr($command_str, $name_end_pos);\n            $args = explode(' ', $args_str);\n            $args = array_filter($args);\n            $command['args'] = $args;\n        }\n        return $command;\n    }\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0cswoole_pop3\u51fd\u6570\u63a5\u6536\u4e94\u4e2a\u53c2\u6570\uff1a<\/p>\n<p>$host\uff1aPOP3\u670d\u52a1\u5668\u7684\u76d1\u542cIP\u5730\u5740<\/p>\n<p>$port\uff1aPOP3\u670d\u52a1\u5668\u7684\u76d1\u542c\u7aef\u53e3<\/p>\n<p>$username\uff1aPOP3\u670d\u52a1\u5668\u767b\u5f55\u7528\u6237\u540d<\/p>\n<p>$password\uff1aPOP3\u670d\u52a1\u5668\u767b\u5f55\u5bc6\u7801<\/p>\n<p>$callback\uff1a\u8fde\u63a5\u5173\u95ed\u65f6\u7684\u56de\u8c03\u51fd\u6570<\/p>\n<p>\u5728\u51fd\u6570\u5185\u90e8\uff0c\u6211\u4eec\u4f7f\u7528Swoole\u7684Server\u7c7b\u521b\u5efaPOP3\u670d\u52a1\u5668\u3002\u5728\u8fde\u63a5\u5efa\u7acb\u540e\uff0c\u5c06\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u6570\u636e\u4f20\u9012\u7ed9POP3\u7c7b\u5904\u7406\uff0c\u7136\u540e\u5c06\u8fd4\u56de\u7684\u54cd\u5e94\u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\u3002<\/p>\n<p>\u4e09\u3001\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1<\/p>\n<p>\u4e3a\u4e86\u5b9e\u73b0\u9ad8\u5e76\u53d1\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06swoole_pop3\u51fd\u6570\u5305\u88c5\u5728\u534f\u7a0b\u4e2d\u3002\u5728\u534f\u7a0b\u4e2d\u8c03\u7528swoole_pop3\u51fd\u6570\uff0c\u5c06\u5176\u4f5c\u4e3a\u5b50\u534f\u7a0b\u6267\u884c\u3002\u8fd9\u6837\uff0c\u5b50\u534f\u7a0b\u7684\u6267\u884c\u5c31\u4e0d\u4f1a\u5f71\u54cd\u5230\u4e3b\u534f\u7a0b\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u9ad8\u5e76\u53d1\u7684\u6548\u679c\u3002<\/p>\n<p>\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php use SwooleCoroutineChannel;\n\nfunction coroutine_pop3($count)\n{\n    $chan = new Channel($count);\n\n    for ($i = 0; $i &lt; $count; $i++) {\n        go(function() use ($i, $chan)\n        {\n            swoole_pop3('127.0.0.1', 9999, 'username', 'password', function() use ($i, $chan) {\n                $chan-&gt;push($i);\n            });\n        });\n    }\n\n    for ($i = 0; $i pop();\n    }\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Swoole\u7684Channel\u7c7b\u521b\u5efa\u4e00\u4e2a\u7528\u4e8e\u534f\u7a0b\u95f4\u901a\u4fe1\u7684\u901a\u9053\uff0c\u5e76\u542f\u52a8$count\u4e2a\u5b50\u534f\u7a0b\u6267\u884cswoole_pop3\u51fd\u6570\uff0c\u5f53\u6240\u6709\u5b50\u534f\u7a0b\u90fd\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u4e3b\u534f\u7a0b\u901a\u8fc7pop\u65b9\u6cd5\u4ece\u901a\u9053\u4e2d\u53d6\u51fa\u6570\u636e\u3002<\/p>\n<p>\u56db\u3001\u603b\u7ed3<\/p>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728Swoole\u4e2d\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684swoole_pop3\u51fd\u6570\u3002\u901a\u8fc7\u5c06POP3\u670d\u52a1\u5668\u7684\u5904\u7406\u903b\u8f91\uff0c\u4ee5\u53caPOP3\u534f\u8bae\u7684\u89e3\u6790\u548c\u7ec4\u88c5\u5199\u5165\u5230swoole_pop3\u51fd\u6570\u4e2d\uff0c\u5e76\u5c06\u5176\u5305\u88c5\u5728\u534f\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684POP3\u5e94\u7528\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728Swoole\u4e2d\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684_pop3\u51fd\u6570\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u7684\u4e0d\u65ad\u53d1\u5c55\uff0c\u9ad8\u5e76\u53d1\u5df2\u7ecf\u6210\u4e3a\u4e86\u73b0\u4ee3\u4e92\u8054\u7f51\u5e94\u7528\u4e2d\u7684\u91cd\u8981\u95ee\u9898\u4e4b\u4e00\u3002\u5728\u7f51\u7edc\u5e94\u7528\u4e2d\uff0cpop3\u534f\u8bae\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u7535\u5b50\u90ae\u4ef6\u6536\u53d1\u534f\u8bae\uff0c\u56e0\u6b64\u5728\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684pop3\u5e94\u7528\u65f6\uff0c\u4f7f\u7528\u534f\u7a0b\u6210\u4e3a\u4e86\u4e00\u79cd\u6709\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728\u4e2d\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684swoole_pop3\u51fd\u6570\u3002 \u4e00\u3001POP3\u57fa\u7840\u77e5\u8bc6 POP3\u534f\u8bae\u662f\u4e00\u79cd\u7528\u4e8e\u90ae\u4ef6\u6536\u53d6\u7684\u6807\u51c6\u534f\u8bae\u3002POP3\u670d\u52a1\u5668\u662f\u90ae\u4ef6\u670d\u52a1\u5668\u4e0a\u7684\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u5b83\u7684\u4e3b\u8981\u529f\u80fd\u662f\u63a5\u6536\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42\uff0c\u6839\u636e\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u8fdb\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\uff0c\u6700\u7ec8\u5c06\u90ae\u4ef6\u4f20\u9001\u7ed9\u5ba2\u6237\u7aef\u3002 POP3\u534f\u8bae\u7684\u57fa\u672c\u5de5\u4f5c\u6d41\u7a0b\u5982\u4e0b\uff1a 1\u3001\u5ba2\u6237\u7aef\u5411POP3\u670d\u52a1\u5668\u53d1\u9001\u8fde\u63a5\u8bf7\u6c42 2\u3001POP3\u670d\u52a1\u5668\u63a5\u53d7\u8bf7\u6c42\u540e\uff0c\u5411\u5ba2\u6237\u7aef\u53d1\u9001\u6b22\u8fce\u6d88\u606f 3\u3001\u5ba2\u6237\u7aef\u53d1\u9001\u7528\u6237\u540d\u548c\u5bc6\u7801 4\u3001POP3\u670d\u52a1\u5668\u9a8c\u8bc1\u7528\u6237\u540d\u548c\u5bc6\u7801\uff0c\u8fd4\u56de\u6210\u529f\u6216\u5931\u8d25\u6d88\u606f 5\u3001\u5982\u679c\u9a8c\u8bc1\u6210\u529f\uff0c\u5ba2\u6237\u7aef\u53ef\u4ee5\u53d1\u9001\u4e00\u4e9b\u547d\u4ee4\u7ed9POP3\u670d\u52a1\u5668\uff0c\u5982LIST\u3001RETR\u7b49 6\u3001POP3\u670d\u52a1\u5668\u6839\u636e\u547d\u4ee4\u8fd4\u56de\u76f8\u5e94\u7684\u7ed3\u679c 7\u3001\u5ba2\u6237\u7aef\u5173\u95ed\u8fde\u63a5 \u4e8c\u3001swoole_pop3\u51fd\u6570\u5b9e\u73b0 \u5728Swoole\u4e2d\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e2apop3\u670d\u52a1\u5668\u7684\u793a\u4f8b\uff0c\u4f7f\u7528swoole_server\u5b9e\u73b0\u3002\u5728\u6b64\u57fa\u7840\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06POP3\u670d\u52a1\u5668\u7684\u5904\u7406\u903b\u8f91\uff0c\u4ee5\u53caPOP3\u534f\u8bae\u7684\u89e3\u6790\u548c\u7ec4\u88c5\u5199\u5230swoole_pop3\u51fd\u6570\u4e2d\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a &lt;?php function swoole_pop3($host, $port, $username, $password, $callback) { $server = new SwooleServer($host, $port, SWOOLE_BASE, SWOOLE_SOCK_TCP); $server-&gt;on(&#8216;receive&#8217;, function($server, $fd, $reactor_id, $data) use ($username, $password, $callback) { $pop3 = new POP3($username, $password); $response = $pop3-&amp;gt;command($data); $server-&amp;gt;send($fd, $response); if ($response == &#8220;+OK conection closed&#8221;) { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-24050","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24050","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=24050"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24050\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}