{"id":159425,"date":"2019-10-19T12:05:41","date_gmt":"2019-10-19T04:05:41","guid":{"rendered":"https:\/\/gulass.cn\/?p=159425"},"modified":"2019-09-28T11:09:56","modified_gmt":"2019-09-28T03:09:56","slug":"java-tcp-itpub","status":"publish","type":"post","link":"https:\/\/gulass.cn\/java-tcp-itpub.html","title":{"rendered":"Java \u7f51\u7edc\u7f16\u7a0b – TCP\u534f\u8bae\u57fa\u672c\u6b65\u9aa4"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n
\n\u4f20\u8f93\u63a7\u5236\u534f\u8bae\uff08TCP\uff0cTransmission Control Protocol\uff09\u662f\u4e00\u79cd\u9762\u5411\u8fde\u63a5\u7684\u3001\u53ef\u9760\u7684\u3001\u57fa\u4e8e\u5b57\u8282\u6d41\u7684\u4f20\u8f93\u5c42\u901a\u4fe1\u534f\u8bae\uff0c\u7531IETF\u7684RFC 793\u5b9a\u4e49\u3002TCP\u65e8\u5728\u9002\u5e94\u652f\u6301\u591a\u7f51\u7edc\u5e94\u7528\u7684\u5206\u5c42\u534f\u8bae\u5c42\u6b21\u7ed3\u6784\u3002 \u8fde\u63a5\u5230\u4e0d\u540c\u4f46\u4e92\u8fde\u7684\u8ba1\u7b97\u673a\u901a\u4fe1\u7f51\u7edc\u7684\u4e3b\u8ba1\u7b97\u673a\u4e2d\u7684\u6210\u5bf9\u8fdb\u7a0b\u4e4b\u95f4\u4f9d\u9760TCP\u63d0\u4f9b\u53ef\u9760\u7684\u901a\u4fe1\u670d\u52a1\u3002TCP\u5047\u8bbe\u5b83\u53ef\u4ee5\u4ece\u8f83\u4f4e\u7ea7\u522b\u7684\u534f\u8bae\u83b7\u5f97\u7b80\u5355\u7684\uff0c\u53ef\u80fd\u4e0d\u53ef\u9760\u7684\u6570\u636e\u62a5\u670d\u52a1\u3002 \u539f\u5219\u4e0a\uff0cTCP\u5e94\u8be5\u80fd\u591f\u5728\u4ece\u786c\u7ebf\u8fde\u63a5\u5230\u5206\u7ec4\u4ea4\u6362\u6216\u7535\u8def\u4ea4\u6362\u7f51\u7edc\u7684\u5404\u79cd\u901a\u4fe1\u7cfb\u7edf\u4e4b\u4e0a\u64cd\u4f5c\u3002
\nTCP:TCP\u534f\u8bae\u57fa\u4e8e\u8bf7\u6c42-\u54cd\u5e94\u6a21\u5f0f,\u5229\u7528io\u6d41\u5b9e\u73b0\u6570\u636e\u7684\u4f20\u8f93\u3002
\n<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u521b\u5efa\u670d\u52a1\u5668<\/strong><\/div>\n

1\u3001\u6307\u5b9a\u7aef\u53e3 \u4f7f\u7528ServerSocket\u521b\u5efa\u670d\u52a1\u5668
\n2\u3001\u963b\u585e\u5f0f\u7b49\u5f85\u8fde\u63a5accept\uff0c\u6709\u4e00\u4e2aaccept\u5c31\u5efa\u7acb\u4e86\u4e00\u4e2a\u5ba2\u6237\u7aef
\n3\u3001\u64cd\u4f5c\uff1aio\u6d41
\n4\u3001\u91ca\u653e\u8d44\u6e90<\/p>\n

\r\npublic class tcp {\r\npublic static void main(String[]args) throws IOException\r\n{\r\nSystem.out.println(\"-----Server-----\");\r\n\/\/ 1\u3001\u6307\u5b9a\u7aef\u53e3 \u4f7f\u7528ServerSocket\u521b\u5efa\u670d\u52a1\u5668\r\nServerSocket server=new ServerSocket(8888);\r\n\r\n\/\/ 2\u3001\u963b\u585e\u5f0f\u7b49\u5f85\u8fde\u63a5accept\r\nSocket client=server.accept();\/\/\u8fd4\u56de\u4e00\u4e2aSocket\u5bf9\u8c61\r\nSystem.out.println(\"\u4e00\u4e2a\u5ba2\u6237\u7aef\u5efa\u7acb\u4e86\u8fde\u63a5\");\r\n\/\/ 3\u3001\u64cd\u4f5c\uff1aio\u6d41\r\nDataInputStream dis=new DataInputStream(client.getInputStream());\/\/\u8f93\u5165\uff0c\r\nclient.getInputStream()\u8fd4\u56de\u4e00\u4e2a\u5b57\u8282\u8f93\u5165\u6d41\r\n\r\nString data=dis.readUTF();\r\nSystem.out.println(data);\r\n\/\/ 4\u3001\u91ca\u653e\u8d44\u6e90\r\ndis.close();\r\nclient.close();\r\n\r\nserver.close();\r\n\r\n}\r\n}\r\n<\/pre>\n
\u521b\u5efa\u5ba2\u6237\u7aef<\/strong><\/div>\n

1\u3001\u5efa\u7acb\u8fde\u63a5:\u4f7f\u7528Socket\u521b\u5efa\u5ba2\u6237\u7aef+\u670d\u52a1\u7684\u5730\u5740\u548c\u7aef\u53e3
\n2\u3001\u64cd\u4f5c\uff1a\u8f93\u5165\u8f93\u51fa\u6d41\u64cd\u4f5c
\n3\u3001\u91ca\u653e\u8d44\u6e90<\/p>\n

\r\npublic class tcp2 {\r\npublic static void main(String[]args) throws IOException\r\n{\r\nSystem.out.println(\"--------Client---------\");\r\n\/\/1\u3001\u5efa\u7acb\u8fde\u63a5:\u4f7f\u7528Socket\u521b\u5efa\u5ba2\u6237\u7aef+\u670d\u52a1\u7684\u5730\u5740\u548c\u7aef\u53e3\r\nSocket client2=new Socket(\"localhost\",8888);\r\n\/\/2\u3001\u64cd\u4f5c\uff1a\u8f93\u5165\u8f93\u51fa\u6d41\u64cd\u4f5c\r\nDataOutputStream dos=new DataOutputStream(client2.getOutputStream());\/\/\u8f93\u51fa\r\n\r\nString data=\"\u675c\u96e8\u9f99\u6700\u5e05\";\r\ndos.writeUTF(data);\r\ndos.flush();\r\n\/\/3\u3001\u91ca\u653e\u8d44\u6e90\r\ndos.close();\r\nclient2.close();\r\n\r\n}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

1\u3001\u6307\u5b9a\u7aef\u53e3 \u4f7f\u7528ServerSocket\u521b\u5efa\u670d\u52a1\u5668 2\u3001\u963b\u585e\u5f0f\u7b49\u5f85\u8fde\u63a5accept\uff0c\u6709\u4e00\u4e2aaccept\u5c31\u5efa\u7acb […]<\/p>\n","protected":false},"author":63,"featured_media":159432,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-159425","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/159425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=159425"}],"version-history":[{"count":11,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/159425\/revisions"}],"predecessor-version":[{"id":160425,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/159425\/revisions\/160425"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/159432"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=159425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=159425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=159425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}