{"id":40,"date":"2021-05-28T00:23:38","date_gmt":"2021-05-27T16:23:38","guid":{"rendered":"https:\/\/blog.luckytain.com\/?p=40"},"modified":"2021-05-28T11:05:28","modified_gmt":"2021-05-28T03:05:28","slug":"java%e4%b8%adsocketchannel%e4%bc%a0%e8%be%93%e4%b8%ad%e5%86%99%e5%85%a5%e5%92%8c%e8%af%bb%e5%8f%96string%e7%9a%84%e4%b8%80%e7%a7%8d%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/blog.luckytain.com\/?p=40","title":{"rendered":"Java\u4e2dSocketChannel\u4f20\u8f93\u4e2d\u5199\u5165\u548c\u8bfb\u53d6String\u7684\u4e00\u79cd\u65b9\u6cd5"},"content":{"rendered":"\n<p>Java\u7684SocketChannel\u4e2d\u5199\u5165\u548c\u8bfb\u53d6String\u6709\u51e0\u4e2a\u9700\u8981\u6ce8\u610f\u7684\u5751<\/p>\n\n\n\n<p>\u5982\u679c\u76f4\u63a5\u4f7f\u7528write()\u548cread()\u7684\u8bdd,\u6709\u53ef\u80fd\u4f1a\u51fa\u73b0\u53d1\u9001\u6216\u8005\u63a5\u53d7\u5230\u7a7a\u6d88\u606f\u7684\u60c5,\u6240\u4ee5\u8981\u52a0\u4e00\u4e2awhile()\u6765\u5faa\u73af\u68c0\u6d4b\u662f\u5426\u628a\u6570\u636e\u90fd\u5199\u5165\/\u8bfb\u53d6\u4e86<\/p>\n\n\n\n<p>\u4f8b\u5b50\u4e2d\u4f7f\u7528UTF-8\u6765\u4f20\u8f93\u5b57\u7b26\u4e32\u907f\u514d\u4e71\u7801<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"write\">write()<\/h4>\n\n\n\n<p>\u975e\u963b\u585e\u6a21\u5f0f\u4e0b\uff0cwrite()\u65b9\u6cd5\u5728\u5c1a\u672a\u5199\u51fa\u4efb\u4f55\u5185\u5bb9\u65f6\u53ef\u80fd\u5c31\u8fd4\u56de\u4e86\u3002\u6240\u4ee5\u9700\u8981\u5728\u5faa\u73af\u4e2d\u8c03\u7528write()\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"read\">read()<\/h4>\n\n\n\n<p>\u975e\u963b\u585e\u6a21\u5f0f\u4e0b,read()\u65b9\u6cd5\u5728\u5c1a\u672a\u8bfb\u53d6\u5230\u4efb\u4f55\u6570\u636e\u65f6\u53ef\u80fd\u5c31\u8fd4\u56de\u4e86\u3002\u6240\u4ee5\u9700\u8981\u5173\u6ce8\u5b83\u7684int\u8fd4\u56de\u503c\uff0c\u5b83\u4f1a\u544a\u8bc9\u4f60\u8bfb\u53d6\u4e86\u591a\u5c11\u5b57\u8282\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java line-numbers\"> \/\/ Send Method\npublic static void SCSend(SocketChannel sc, String s) throws IOException {\nCharset charset = Charset.forName(\"UTF-8\");\nByteBuffer buf = ByteBuffer.allocate(1024);\nbuf.clear();\nbuf = ByteBuffer.wrap(s.getBytes(charset));\nwhile(buf.hasRemaining()){\nsc.write(buf);\n}\n}\n\n \/\/ Receive Method\npublic static void SCReceive(SocketChannel sc) throws IOException {\nCharset charset =Charset.forName(\"UTf-8\");\nByteBuffer buf = ByteBuffer.allocate(1024);\nbuf.clear();\nint bytesRead = sc.read(buf);\nwhile(bytesRead==0){\nbytesRead = sc.read(buf);\n}\nbuf.flip();\nString s = charset.decode(buf).toString();\nSystem.out.println(s); \/\/\u53ef\u9009\u8f93\u51fa\u5185\u5bb9\n} <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Java\u7684SocketChannel\u4e2d\u5199\u5165\u548c\u8bfb\u53d6String\u6709\u51e0\u4e2a\u9700\u8981\u6ce8\u610f\u7684\u5751 \u5982\u679c\u76f4\u63a5\u4f7f\u7528write()\u548cr [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-40","post","type-post","status-publish","format-standard","hentry","category-13"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blog.luckytain.com\/index.php?rest_route=\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.luckytain.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.luckytain.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.luckytain.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.luckytain.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=40"}],"version-history":[{"count":1,"href":"https:\/\/blog.luckytain.com\/index.php?rest_route=\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":47,"href":"https:\/\/blog.luckytain.com\/index.php?rest_route=\/wp\/v2\/posts\/40\/revisions\/47"}],"wp:attachment":[{"href":"https:\/\/blog.luckytain.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.luckytain.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.luckytain.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}