{"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":"
\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 \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 |