{"id":81375,"date":"2026-07-25T01:21:47","date_gmt":"2026-07-24T17:21:47","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/81375.html"},"modified":"2026-07-25T01:21:47","modified_gmt":"2026-07-24T17:21:47","slug":"socket%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e5%85%a5%e9%97%a8%ef%bc%9audptcp%e5%8f%8c%e6%a1%88%e4%be%8b%e6%89%8b%e6%8a%8a%e6%89%8b%e6%95%99%e4%bd%a0%e5%86%99%e6%9c%8d%e5%8a%a1%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/81375.html","title":{"rendered":"Socket\u7f51\u7edc\u7f16\u7a0b\u5165\u95e8\uff1aUDP+TCP\u53cc\u6848\u4f8b\u624b\u628a\u624b\u6559\u4f60\u5199\u670d\u52a1\u5668"},"content":{"rendered":"<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"-toc\" style=\"margin-left:0px\">\n<p id=\"%E5%89%8D%E8%A8%80%EF%BC%9A%E4%B8%BA%E4%BB%80%E4%B9%88%E8%A6%81%E5%AD%A6%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B%EF%BC%9F-toc\" style=\"margin-left:0px\">\u524d\u8a00&#xff1a;\u4e3a\u4ec0\u4e48\u8981\u5b66\u7f51\u7edc\u7f16\u7a0b&#xff1f;<\/p>\n<p id=\"%E4%B8%80%E3%80%81%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80%E6%A6%82%E5%BF%B5-toc\" style=\"margin-left:0px\">\u4e00\u3001\u7f51\u7edc\u7f16\u7a0b\u57fa\u7840\u6982\u5ff5<\/p>\n<p id=\"1.1%20%E4%BB%80%E4%B9%88%E6%98%AF%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B-toc\" style=\"margin-left:40px\">1.1 \u4ec0\u4e48\u662f\u7f51\u7edc\u7f16\u7a0b<\/p>\n<p id=\"1.2%20%E5%8F%91%E9%80%81%E7%AB%AF%E4%B8%8E%E6%8E%A5%E6%94%B6%E7%AB%AF-toc\" style=\"margin-left:40px\">1.2 \u53d1\u9001\u7aef\u4e0e\u63a5\u6536\u7aef<\/p>\n<p id=\"1.4%20%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%B8%8E%E6%9C%8D%E5%8A%A1%E7%AB%AF-toc\" style=\"margin-left:40px\">1.4 \u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u7aef<\/p>\n<p id=\"%E4%BA%8C%E3%80%81Socket%E5%A5%97%E6%8E%A5%E5%AD%97%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F-toc\" style=\"margin-left:0px\">\u4e8c\u3001Socket\u5957\u63a5\u5b57\u662f\u4ec0\u4e48&#xff1f;<\/p>\n<p id=\"2.1%20%E5%A5%97%E6%8E%A5%E5%AD%97%E7%9A%84%E6%A6%82%E5%BF%B5-toc\" style=\"margin-left:40px\">2.1 \u5957\u63a5\u5b57\u7684\u6982\u5ff5<\/p>\n<p id=\"2.3%20TCP%20vs%20UDP%20%E6%A0%B8%E5%BF%83%E5%AF%B9%E6%AF%94-toc\" style=\"margin-left:40px\">2.3 TCP vs UDP \u6838\u5fc3\u5bf9\u6bd4<\/p>\n<p id=\"%E4%B8%89%E3%80%81UDP%E6%95%B0%E6%8D%AE%E6%8A%A5%E5%A5%97%E6%8E%A5%E5%AD%97%E7%BC%96%E7%A8%8B-toc\" style=\"margin-left:0px\">\u4e09\u3001UDP\u6570\u636e\u62a5\u5957\u63a5\u5b57\u7f16\u7a0b<\/p>\n<p id=\"3.1%20UDP%E9%80%9A%E4%BF%A1%E6%A8%A1%E5%9E%8B-toc\" style=\"margin-left:40px\">3.1 UDP\u901a\u4fe1\u6a21\u578b<\/p>\n<p id=\"3.2%20%E6%A0%B8%E5%BF%83API%E4%BB%8B%E7%BB%8D-toc\" style=\"margin-left:40px\">3.2 \u6838\u5fc3API\u4ecb\u7ecd<\/p>\n<p id=\"DatagramSocket-toc\" style=\"margin-left:80px\">DatagramSocket<\/p>\n<p id=\"DatagramPacket-toc\" style=\"margin-left:80px\">DatagramPacket<\/p>\n<p id=\"3.3%20%E5%AE%9E%E6%88%98%E4%B8%80%EF%BC%9AEcho%E5%9B%9E%E6%98%BE%E6%9C%8D%E5%8A%A1%E5%99%A8-toc\" style=\"margin-left:40px\">3.3 \u5b9e\u6218\u4e00&#xff1a;Echo\u56de\u663e\u670d\u52a1\u5668<\/p>\n<p id=\"UDP%20Echo%20%E6%9C%8D%E5%8A%A1%E7%AB%AF-toc\" style=\"margin-left:80px\">UDP Echo \u670d\u52a1\u7aef<\/p>\n<p id=\"UDP%20Echo%20%E5%AE%A2%E6%88%B7%E7%AB%AF-toc\" style=\"margin-left:80px\">UDP Echo \u5ba2\u6237\u7aef<\/p>\n<p id=\"3.4%20%E5%AE%9E%E6%88%98%E4%BA%8C%EF%BC%9A%E8%8B%B1%E8%AF%91%E6%B1%89%E8%AF%8D%E5%85%B8%E6%9C%8D%E5%8A%A1%E5%99%A8-toc\" style=\"margin-left:40px\">3.4 \u5b9e\u6218\u4e8c&#xff1a;\u82f1\u8bd1\u6c49\u8bcd\u5178\u670d\u52a1\u5668<\/p>\n<p id=\"%E5%9B%9B%E3%80%81TCP%E6%B5%81%E5%A5%97%E6%8E%A5%E5%AD%97%E7%BC%96%E7%A8%8B-toc\" style=\"margin-left:0px\">\u56db\u3001TCP\u6d41\u5957\u63a5\u5b57\u7f16\u7a0b<\/p>\n<p id=\"4.1%20TCP%E9%80%9A%E4%BF%A1%E6%A8%A1%E5%9E%8B-toc\" style=\"margin-left:40px\">4.1 TCP\u901a\u4fe1\u6a21\u578b<\/p>\n<p id=\"4.2%20%E6%A0%B8%E5%BF%83API%E4%BB%8B%E7%BB%8D-toc\" style=\"margin-left:40px\">4.2 \u6838\u5fc3API\u4ecb\u7ecd<\/p>\n<p id=\"ServerSocket-toc\" style=\"margin-left:80px\">ServerSocket<\/p>\n<p id=\"Socket-toc\" style=\"margin-left:80px\">Socket<\/p>\n<p id=\"4.3%20%E5%AE%9E%E6%88%98%EF%BC%9AEcho%E5%9B%9E%E6%98%BE%E6%9C%8D%E5%8A%A1%E5%99%A8-toc\" style=\"margin-left:40px\">4.3 \u5b9e\u6218&#xff1a;Echo\u56de\u663e\u670d\u52a1\u5668<\/p>\n<p id=\"TCP%20Echo%20%E6%9C%8D%E5%8A%A1%E7%AB%AF-toc\" style=\"margin-left:80px\">TCP Echo \u670d\u52a1\u7aef<\/p>\n<p id=\"TCP%20Echo%20%E5%AE%A2%E6%88%B7%E7%AB%AF-toc\" style=\"margin-left:80px\">TCP Echo \u5ba2\u6237\u7aef<\/p>\n<p id=\"%E4%BA%94%E3%80%81%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96-toc\" style=\"margin-left:0px\">\u4e94\u3001\u670d\u52a1\u5668\u6027\u80fd\u4f18\u5316<\/p>\n<p id=\"5.1%20%E4%B8%BA%E4%BB%80%E4%B9%88%E9%9C%80%E8%A6%81%E5%A4%9A%E7%BA%BF%E7%A8%8B%EF%BC%9F-toc\" style=\"margin-left:40px\">5.1 \u4e3a\u4ec0\u4e48\u9700\u8981\u591a\u7ebf\u7a0b&#xff1f;<\/p>\n<p id=\"5.2%20%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%89%88%E6%9C%AC%E6%9C%8D%E5%8A%A1%E5%99%A8-toc\" style=\"margin-left:40px\">5.2 \u591a\u7ebf\u7a0b\u7248\u672c\u670d\u52a1\u5668<\/p>\n<p id=\"5.3%20%E7%BA%BF%E7%A8%8B%E6%B1%A0%E7%89%88%E6%9C%AC%E6%9C%8D%E5%8A%A1%E5%99%A8-toc\" style=\"margin-left:40px\">5.3 \u7ebf\u7a0b\u6c60\u7248\u672c\u670d\u52a1\u5668<\/p>\n<p id=\"%E5%85%AD%E3%80%81%E9%95%BF%E7%9F%AD%E8%BF%9E%E6%8E%A5%E8%AF%A6%E8%A7%A3-toc\" style=\"margin-left:0px\">\u516d\u3001\u957f\u77ed\u8fde\u63a5\u8be6\u89e3<\/p>\n<p id=\"%E7%9F%AD%E8%BF%9E%E6%8E%A5-toc\" style=\"margin-left:80px\">\u77ed\u8fde\u63a5<\/p>\n<p id=\"%E9%95%BF%E8%BF%9E%E6%8E%A5-toc\" style=\"margin-left:80px\">\u957f\u8fde\u63a5<\/p>\n<p id=\"%E4%B8%A4%E8%80%85%E5%AF%B9%E6%AF%94-toc\" style=\"margin-left:80px\">\u4e24\u8005\u5bf9\u6bd4<\/p>\n<p id=\"%E4%B8%83%E3%80%81%E5%B8%B8%E8%A7%81%E5%9D%91%E7%82%B9%EF%BC%9A%E7%AB%AF%E5%8F%A3%E8%A2%AB%E5%8D%A0%E7%94%A8%E6%80%8E%E4%B9%88%E5%8A%9E%EF%BC%9F-toc\" style=\"margin-left:0px\">\u4e03\u3001\u5e38\u89c1\u5751\u70b9&#xff1a;\u7aef\u53e3\u88ab\u5360\u7528\u600e\u4e48\u529e&#xff1f;<\/p>\n<p id=\"%E6%80%8E%E4%B9%88%E6%9F%A5%E5%93%AA%E4%B8%AA%E8%BF%9B%E7%A8%8B%E5%8D%A0%E4%BA%86%E7%AB%AF%E5%8F%A3%EF%BC%9F-toc\" style=\"margin-left:80px\">\u600e\u4e48\u67e5\u54ea\u4e2a\u8fdb\u7a0b\u5360\u4e86\u7aef\u53e3&#xff1f;<\/p>\n<p id=\"%E6%80%8E%E4%B9%88%E8%A7%A3%E5%86%B3%EF%BC%9F-toc\" style=\"margin-left:80px\">\u600e\u4e48\u89e3\u51b3&#xff1f;<\/p>\n<p id=\"%E5%85%AB%E3%80%81%E5%85%A8%E6%96%87%E6%80%BB%E7%BB%93-toc\" style=\"margin-left:0px\">\u516b\u3001\u5168\u6587\u603b\u7ed3<\/p>\n<hr id=\"hr-toc\" \/>\n<h2 id=\"%E5%89%8D%E8%A8%80%EF%BC%9A%E4%B8%BA%E4%BB%80%E4%B9%88%E8%A6%81%E5%AD%A6%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B%EF%BC%9F\">\u524d\u8a00&#xff1a;\u4e3a\u4ec0\u4e48\u8981\u5b66\u7f51\u7edc\u7f16\u7a0b&#xff1f;<\/h2>\n<p>\u5927\u5bb6\u597d&#xff0c;\u4eca\u5929\u6211\u4eec\u6765\u804a\u4e00\u4e2aJava\u540e\u7aef\u5f00\u53d1\u7ed5\u4e0d\u5f00\u7684\u8bdd\u9898\u2014\u2014\u7f51\u7edc\u7f16\u7a0b\u3002<\/p>\n<p>\u4f60\u6709\u6ca1\u6709\u60f3\u8fc7&#xff0c;\u6211\u4eec\u6bcf\u5929\u5237\u6296\u97f3\u3001\u901b\u6dd8\u5b9d\u3001\u73a9\u7f51\u6e38&#xff0c;\u8fd9\u4e9b\u6570\u636e\u662f\u600e\u4e48\u4ece\u670d\u52a1\u5668\u4f20\u5230\u4f60\u624b\u673a\u4e0a\u7684&#xff1f;\u80cc\u540e\u7684\u6280\u672f\u652f\u6491\u5c31\u662f\u7f51\u7edc\u7f16\u7a0b\u3002\u5b66\u4f1a\u4e86Socket\u5957\u63a5\u5b57&#xff0c;\u4f60\u5c31\u80fd\u81ea\u5df1\u5199\u670d\u52a1\u5668\u3001\u5199\u5ba2\u6237\u7aef&#xff0c;\u771f\u6b63\u7406\u89e3\u6570\u636e\u662f\u600e\u4e48\u5728\u7f51\u7edc\u4e2d\u4f20\u8f93\u7684\u3002<\/p>\n<p>\u8fd9\u7bc7\u6587\u7ae0\u6211\u4f1a\u7528\u6700\u901a\u4fd7\u7684\u8bed\u8a00&#xff0c;\u914d\u5408\u5b8c\u6574\u7684\u4ee3\u7801\u6848\u4f8b&#xff0c;\u5e26\u4f60\u4ece\u96f6\u5165\u95e8UDP\u548cTCP\u4e24\u79cd\u5957\u63a5\u5b57\u7f16\u7a0b\u3002\u770b\u5b8c\u4f60\u5c31\u80fd\u52a8\u624b\u5199\u51fa\u81ea\u5df1\u7684\u7b2c\u4e00\u4e2a\u7f51\u7edc\u7a0b\u5e8f&#xff01;<\/p>\n<p>\u672c\u6587\u9762\u5411\u4eba\u7fa4&#xff1a;\u6709Java\u57fa\u7840\u3001\u60f3\u5165\u95e8\u7f51\u7edc\u7f16\u7a0b\u7684\u540c\u5b66<\/p>\n<p>\u4f60\u5c06\u6536\u83b7&#xff1a;Socket\u6982\u5ff5 &#043; UDP\/TCP\u539f\u7406 &#043; 4\u4e2a\u5b8c\u6574\u5b9e\u6218\u4ee3\u7801 &#043; \u5e38\u89c1\u5751\u70b9\u907f\u5751\u6307\u5357<\/p>\n<h2 id=\"%E4%B8%80%E3%80%81%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80%E6%A6%82%E5%BF%B5\">\u4e00\u3001\u7f51\u7edc\u7f16\u7a0b\u57fa\u7840\u6982\u5ff5<\/h2>\n<h3 id=\"1.1%20%E4%BB%80%E4%B9%88%E6%98%AF%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B\">1.1 \u4ec0\u4e48\u662f\u7f51\u7edc\u7f16\u7a0b<\/h3>\n<p>\u5148\u7ed9\u5927\u5bb6\u4e00\u4e2a\u76f4\u89c2\u7684\u7406\u89e3&#xff1a;\u4f60\u5728\u6d4f\u89c8\u5668\u6253\u5f00\u4f18\u9177\u770b\u89c6\u9891&#xff0c;\u672c\u8d28\u4e0a\u5c31\u662f\u901a\u8fc7\u7f51\u7edc&#xff0c;\u83b7\u53d6\u5230\u4e86\u7f51\u7edc\u4e0a\u7684\u4e00\u4e2a\u89c6\u9891\u8d44\u6e90\u3002\u548c\u4f60\u672c\u5730\u6253\u5f00\u4e00\u4e2a\u89c6\u9891\u6587\u4ef6\u7c7b\u4f3c&#xff0c;\u53ea\u662f\u8fd9\u4e2a\u8d44\u6e90\u7684\u6765\u6e90\u662f\u7f51\u7edc\u3002<\/p>\n<p>\u7f51\u7edc\u7f16\u7a0b&#xff0c;\u5c31\u662f\u6307\u7f51\u7edc\u4e0a\u7684\u4e0d\u540c\u4e3b\u673a&#xff0c;\u901a\u8fc7\u4e0d\u540c\u7684\u8fdb\u7a0b&#xff0c;\u4ee5\u7f16\u7a0b\u7684\u65b9\u5f0f\u5b9e\u73b0\u7f51\u7edc\u901a\u4fe1&#xff08;\u4e5f\u5c31\u662f\u7f51\u7edc\u6570\u636e\u4f20\u8f93&#xff09;\u3002<\/p>\n<p>\u5212\u91cd\u70b9&#xff1a;\u53ea\u8981\u662f\u4e0d\u540c\u8fdb\u7a0b\u4e4b\u95f4\u901a\u8fc7\u7f51\u7edc\u4f20\u8f93\u6570\u636e&#xff0c;\u5c31\u7b97\u7f51\u7edc\u7f16\u7a0b\u3002\u54ea\u6015\u662f\u540c\u4e00\u53f0\u7535\u8111\u4e0a\u5f00\u4e24\u4e2a\u7a0b\u5e8f\u4e92\u76f8\u53d1\u6570\u636e&#xff0c;\u4e5f\u5c5e\u4e8e\u7f51\u7edc\u7f16\u7a0b\u7684\u8303\u7574\u3002<\/p>\n<p>\u6211\u4eec\u5f00\u53d1\u7684\u65f6\u5019&#xff0c;\u4e3a\u4e86\u65b9\u4fbf\u8c03\u8bd5&#xff0c;\u901a\u5e38\u90fd\u662f\u5728\u81ea\u5df1\u7535\u8111\u4e0a\u5f00\u4e24\u4e2a\u8fdb\u7a0b\u6a21\u62df\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u3002\u4f46\u771f\u5b9e\u573a\u666f\u4e0b&#xff0c;\u80af\u5b9a\u662f\u5206\u5e03\u5728\u4e0d\u540c\u4e3b\u673a\u4e0a\u7684&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u8fdb\u7a0bA&#xff1a;\u8d1f\u8d23\u83b7\u53d6\u7f51\u7edc\u8d44\u6e90&#xff08;\u6bd4\u5982\u4f60\u7684\u6d4f\u89c8\u5668&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u8fdb\u7a0bB&#xff1a;\u8d1f\u8d23\u63d0\u4f9b\u7f51\u7edc\u8d44\u6e90&#xff08;\u6bd4\u5982\u7f51\u7ad9\u7684\u670d\u52a1\u5668&#xff09;<\/p>\n<\/li>\n<\/ul>\n<h3 id=\"1.2%20%E5%8F%91%E9%80%81%E7%AB%AF%E4%B8%8E%E6%8E%A5%E6%94%B6%E7%AB%AF\">1.2 \u53d1\u9001\u7aef\u4e0e\u63a5\u6536\u7aef<\/h3>\n<p>\u5728\u4e00\u6b21\u7f51\u7edc\u6570\u636e\u4f20\u8f93\u4e2d&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u53d1\u9001\u7aef&#xff1a;\u6570\u636e\u7684\u53d1\u9001\u65b9\u8fdb\u7a0b&#xff0c;\u53d1\u9001\u7aef\u6240\u5728\u7684\u4e3b\u673a\u53eb\u6e90\u4e3b\u673a<\/p>\n<\/li>\n<li>\n<p>\u63a5\u6536\u7aef&#xff1a;\u6570\u636e\u7684\u63a5\u6536\u65b9\u8fdb\u7a0b&#xff0c;\u63a5\u6536\u7aef\u6240\u5728\u7684\u4e3b\u673a\u53eb\u76ee\u7684\u4e3b\u673a<\/p>\n<\/li>\n<\/ul>\n<p>\u8fd9\u91cc\u8981\u6ce8\u610f&#xff0c;\u53d1\u9001\u7aef\u548c\u63a5\u6536\u7aef\u662f\u76f8\u5bf9\u7684\u6982\u5ff5&#xff0c;\u53ea\u662f\u4e00\u6b21\u6570\u636e\u4f20\u8f93\u4ea7\u751f\u6d41\u5411\u540e\u7684\u53eb\u6cd5\u3002A\u7ed9B\u53d1\u6570\u636e\u65f6&#xff0c;A\u662f\u53d1\u9001\u7aef\u3001B\u662f\u63a5\u6536\u7aef&#xff1b;\u53cd\u8fc7\u6765B\u7ed9A\u56de\u6570\u636e\u65f6&#xff0c;B\u5c31\u53d8\u6210\u4e86\u53d1\u9001\u7aef\u3001A\u53d8\u6210\u4e86\u63a5\u6536\u7aef\u3002<\/p>\n<h3 id=\"1.4%20%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%B8%8E%E6%9C%8D%E5%8A%A1%E7%AB%AF\">1.4 \u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u7aef<\/h3>\n<p>\u5728\u5e38\u89c1\u7684\u7f51\u7edc\u573a\u666f\u4e2d&#xff0c;\u6211\u4eec\u628a\u63d0\u4f9b\u670d\u52a1\u7684\u4e00\u65b9\u53eb\u670d\u52a1\u7aef&#xff0c;\u83b7\u53d6\u670d\u52a1\u7684\u4e00\u65b9\u53eb\u5ba2\u6237\u7aef\u3002<\/p>\n<p>\u670d\u52a1\u7aef\u4e00\u822c\u63d0\u4f9b\u4e24\u79cd\u670d\u52a1&#xff1a;<\/p>\n<li>\n<p>\u5ba2\u6237\u7aef\u83b7\u53d6\u8d44\u6e90&#xff1a;\u6bd4\u5982\u4f60\u5237B\u7ad9&#xff0c;\u89c6\u9891\u8d44\u6e90\u90fd\u5728\u670d\u52a1\u5668\u4e0a&#xff0c;\u4f60\u4f5c\u4e3a\u5ba2\u6237\u7aef\u53bb\u53d6<\/p>\n<\/li>\n<li>\n<p>\u5ba2\u6237\u7aef\u4fdd\u5b58\u8d44\u6e90&#xff1a;\u6bd4\u5982\u4f60\u53d1\u670b\u53cb\u5708&#xff0c;\u628a\u56fe\u7247\u6587\u5b57\u4e0a\u4f20\u5230\u5fae\u4fe1\u670d\u52a1\u5668\u5b58\u8d77\u6765<\/p>\n<\/li>\n<p>\u6700\u7ecf\u5178\u7684\u5ba2\u6237\u7aef-\u670d\u52a1\u7aef\u6a21\u578b\u6d41\u7a0b\u662f\u8fd9\u6837\u7684&#xff1a;<\/p>\n<li>\n<p>\u5ba2\u6237\u7aef\u5148\u53d1\u9001\u8bf7\u6c42\u5230\u670d\u52a1\u7aef<\/p>\n<\/li>\n<li>\n<p>\u670d\u52a1\u7aef\u6839\u636e\u8bf7\u6c42\u6570\u636e&#xff0c;\u6267\u884c\u4e1a\u52a1\u5904\u7406<\/p>\n<\/li>\n<li>\n<p>\u670d\u52a1\u7aef\u8fd4\u56de\u54cd\u5e94&#xff1a;\u53d1\u9001\u5904\u7406\u7ed3\u679c<\/p>\n<\/li>\n<li>\n<p>\u5ba2\u6237\u7aef\u6839\u636e\u54cd\u5e94\u6570\u636e&#xff0c;\u5c55\u793a\u5904\u7406\u7ed3\u679c<\/p>\n<\/li>\n<h2 id=\"%E4%BA%8C%E3%80%81Socket%E5%A5%97%E6%8E%A5%E5%AD%97%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F\">\u4e8c\u3001Socket\u5957\u63a5\u5b57\u662f\u4ec0\u4e48&#xff1f;<\/h2>\n<h3 id=\"2.1%20%E5%A5%97%E6%8E%A5%E5%AD%97%E7%9A%84%E6%A6%82%E5%BF%B5\">2.1 \u5957\u63a5\u5b57\u7684\u6982\u5ff5<\/h3>\n<p>Socket&#xff08;\u5957\u63a5\u5b57&#xff09;&#xff0c;\u662f\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u7684\u7528\u4e8e\u7f51\u7edc\u901a\u4fe1\u7684\u6280\u672f&#xff0c;\u662f\u57fa\u4e8eTCP\/IP\u534f\u8bae\u7684\u7f51\u7edc\u901a\u4fe1\u7684\u57fa\u672c\u64cd\u4f5c\u5355\u5143\u3002\u7b80\u5355\u8bf4&#xff0c;Socket\u5c31\u662f\u64cd\u4f5c\u7cfb\u7edf\u7ed9\u6211\u4eec\u7a0b\u5e8f\u5458\u63d0\u4f9b\u7684\u4e00\u5957API&#xff0c;\u8ba9\u6211\u4eec\u80fd\u65b9\u4fbf\u5730\u8fdb\u884c\u7f51\u7edc\u6570\u636e\u4f20\u8f93\u3002<\/p>\n<p>\u57fa\u4e8eSocket\u5957\u63a5\u5b57\u7684\u7f51\u7edc\u7a0b\u5e8f\u5f00\u53d1&#xff0c;\u5c31\u662f\u6211\u4eec\u8bf4\u7684\u7f51\u7edc\u7f16\u7a0b\u3002<\/p>\n<h3 id=\"2.3%20TCP%20vs%20UDP%20%E6%A0%B8%E5%BF%83%E5%AF%B9%E6%AF%94\">2.3 TCP vs UDP \u6838\u5fc3\u5bf9\u6bd4<\/h3>\n<p>\u5f88\u591a\u540c\u5b66\u521a\u5b66\u7684\u65f6\u5019\u5bb9\u6613\u641e\u6df7TCP\u548cUDP&#xff0c;\u8fd9\u91cc\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u4e00\u5f20\u5bf9\u6bd4\u8868&#xff0c;\u4e00\u76ee\u4e86\u7136&#xff1a;<\/p>\n<table>\n<tr>\n<p>\u5bf9\u6bd4\u7ef4\u5ea6<\/p>\n<p>TCP&#xff08;\u4f20\u8f93\u63a7\u5236\u534f\u8bae&#xff09;<\/p>\n<p>UDP&#xff08;\u7528\u6237\u6570\u636e\u62a5\u534f\u8bae&#xff09;<\/p>\n<\/tr>\n<tbody>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u8fde\u63a5\u6027<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u6709\u8fde\u63a5&#xff0c;\u901a\u4fe1\u524d\u9700\u4e09\u6b21\u63e1\u624b\u5efa\u7acb<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u65e0\u8fde\u63a5&#xff0c;\u76f4\u63a5\u53d1\u9001\u6570\u636e<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u53ef\u9760\u6027<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u53ef\u9760\u4f20\u8f93&#xff0c;\u786e\u8ba4\u3001\u91cd\u4f20\u3001\u6d41\u91cf\u63a7\u5236<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u4e0d\u53ef\u9760&#xff0c;\u4e0d\u4fdd\u8bc1\u9001\u8fbe&#xff0c;\u4e22\u5305\u4e0d\u91cd\u4f20<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u4f20\u8f93\u5f62\u5f0f<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u9762\u5411\u5b57\u8282\u6d41&#xff0c;\u65e0\u8fb9\u754c<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u9762\u5411\u6570\u636e\u62a5&#xff0c;\u6709\u660e\u786e\u8fb9\u754c<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u4f20\u8f93\u6548\u7387<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u76f8\u5bf9\u8f83\u4f4e&#xff0c;\u6709\u989d\u5916\u5f00\u9500<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u9ad8&#xff0c;\u5f00\u9500\u5c0f\u901f\u5ea6\u5feb<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u6570\u636e\u5927\u5c0f<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u65e0\u9650\u5236<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u5355\u6b21\u6700\u591a64K<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u5e94\u7528\u573a\u666f<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u7f51\u9875\u3001\u6587\u4ef6\u4f20\u8f93\u3001\u90ae\u4ef6\u7b49\u9700\u53ef\u9760\u7684\u573a\u666f<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u76f4\u64ad\u3001\u6e38\u620f\u3001\u89c6\u9891\u4f1a\u8bae\u7b49\u5b9e\u65f6\u6027\u4f18\u5148\u573a\u666f<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u9762\u8bd5\u5e38\u8003&#xff1a;TCP\u548cUDP\u7684\u533a\u522b\u662f\u540e\u7aef\u9762\u8bd5\u9ad8\u9891\u9898&#xff0c;\u4e00\u5b9a\u8981\u8bb0\u7262&#xff01;<\/p>\n<h2 id=\"%E4%B8%89%E3%80%81UDP%E6%95%B0%E6%8D%AE%E6%8A%A5%E5%A5%97%E6%8E%A5%E5%AD%97%E7%BC%96%E7%A8%8B\">\u4e09\u3001UDP\u6570\u636e\u62a5\u5957\u63a5\u5b57\u7f16\u7a0b<\/h2>\n<h3 id=\"3.1%20UDP%E9%80%9A%E4%BF%A1%E6%A8%A1%E5%9E%8B\">3.1 UDP\u901a\u4fe1\u6a21\u578b<\/h3>\n<p>UDP\u662f\u65e0\u8fde\u63a5\u3001\u9762\u5411\u6570\u636e\u62a5\u7684\u534f\u8bae&#xff0c;\u610f\u601d\u5c31\u662f\u6bcf\u6b21\u53d1\u6570\u636e\u90fd\u4e0d\u7528\u5efa\u7acb\u8fde\u63a5&#xff0c;\u4e00\u6b21\u628a\u6574\u4e2a\u6570\u636e\u62a5\u53d1\u51fa\u53bb&#xff0c;\u63a5\u6536\u7aef\u4e5f\u5fc5\u987b\u4e00\u6b21\u6536\u5b8c\u3002<\/p>\n<p>Java\u4e2d\u4f7f\u7528UDP\u534f\u8bae\u901a\u4fe1&#xff0c;\u4e3b\u8981\u57fa\u4e8e\u4e24\u4e2a\u7c7b&#xff1a;<\/p>\n<ul>\n<li>\n<p>DatagramSocket&#xff1a;\u6570\u636e\u62a5\u5957\u63a5\u5b57&#xff0c;\u7528\u6765\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e<\/p>\n<\/li>\n<li>\n<p>DatagramPacket&#xff1a;\u6570\u636e\u62a5\u5305&#xff0c;\u5c31\u662f\u5177\u4f53\u8981\u53d1\u9001\u6216\u63a5\u6536\u7684\u6570\u636e<\/p>\n<\/li>\n<\/ul>\n<p>\u4e00\u6b21\u5b8c\u6574\u7684UDP\u8bf7\u6c42-\u54cd\u5e94\u6d41\u7a0b\u662f\u8fd9\u6837\u7684&#xff1a;<\/p>\n<li>\n<p>\u63a5\u6536\u7aef\u5148\u521b\u5efaDatagramSocket&#xff0c;\u7ed1\u5b9a\u7aef\u53e3&#xff0c;\u8c03\u7528receive()\u65b9\u6cd5\u963b\u585e\u7b49\u5f85\u6570\u636e<\/p>\n<\/li>\n<li>\n<p>\u53d1\u9001\u7aef\u521b\u5efaDatagramSocket&#xff0c;\u6784\u9020DatagramPacket&#xff08;\u5305\u542b\u6570\u636e&#043;\u76ee\u6807IP\u548c\u7aef\u53e3&#xff09;&#xff0c;\u8c03\u7528send()\u53d1\u9001<\/p>\n<\/li>\n<li>\n<p>\u63a5\u6536\u7aef\u6536\u5230\u6570\u636e&#xff0c;receive()\u65b9\u6cd5\u8fd4\u56de&#xff0c;\u4eceDatagramPacket\u4e2d\u53d6\u51fa\u6570\u636e<\/p>\n<\/li>\n<li>\n<p>\u63a5\u6536\u7aef\u5904\u7406\u4e1a\u52a1&#xff0c;\u6784\u9020\u54cd\u5e94\u7684DatagramPacket&#xff0c;send()\u53d1\u56de\u7ed9\u53d1\u9001\u7aef<\/p>\n<\/li>\n<li>\n<p>\u53d1\u9001\u7aef\u8c03\u7528receive()\u7b49\u5f85\u54cd\u5e94&#xff0c;\u6536\u5230\u540e\u5904\u7406<\/p>\n<\/li>\n<h3 id=\"3.2%20%E6%A0%B8%E5%BF%83API%E4%BB%8B%E7%BB%8D\">3.2 \u6838\u5fc3API\u4ecb\u7ecd<\/h3>\n<h4 id=\"DatagramSocket\">DatagramSocket<\/h4>\n<p>DatagramSocket\u5c31\u662fUDP\u7684Socket&#xff0c;\u7528\u4e8e\u53d1\u9001\u548c\u63a5\u6536UDP\u6570\u636e\u62a5\u3002<\/p>\n<p>\u6784\u9020\u65b9\u6cd5&#xff1a;<\/p>\n<table>\n<tr>\n<p>\u65b9\u6cd5\u7b7e\u540d<\/p>\n<p>\u8bf4\u660e<\/p>\n<\/tr>\n<tbody>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>DatagramSocket()<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u521b\u5efaUDP\u5957\u63a5\u5b57&#xff0c;\u7ed1\u5b9a\u672c\u673a\u968f\u673a\u7aef\u53e3&#xff08;\u4e00\u822c\u5ba2\u6237\u7aef\u7528&#xff09;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>DatagramSocket(int port)<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u521b\u5efaUDP\u5957\u63a5\u5b57&#xff0c;\u7ed1\u5b9a\u6307\u5b9a\u7aef\u53e3&#xff08;\u4e00\u822c\u670d\u52a1\u7aef\u7528&#xff09;<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5e38\u7528\u65b9\u6cd5&#xff1a;<\/p>\n<table>\n<tr>\n<p>\u65b9\u6cd5\u7b7e\u540d<\/p>\n<p>\u8bf4\u660e<\/p>\n<\/tr>\n<tbody>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>void receive(DatagramPacket p)<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u63a5\u6536\u6570\u636e\u62a5&#xff0c;\u6ca1\u6570\u636e\u5c31\u963b\u585e\u7b49\u5f85<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>void send(DatagramPacket p)<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u53d1\u9001\u6570\u636e\u62a5&#xff0c;\u76f4\u63a5\u53d1\u4e0d\u963b\u585e<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>void close()<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u5173\u95ed\u5957\u63a5\u5b57&#xff0c;\u91ca\u653e\u8d44\u6e90<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4 id=\"DatagramPacket\">DatagramPacket<\/h4>\n<p>DatagramPacket\u5c31\u662fUDP\u53d1\u9001\u548c\u63a5\u6536\u7684\u6570\u636e\u62a5\u5305\u3002<\/p>\n<p>\u6784\u9020\u65b9\u6cd5&#xff1a;<\/p>\n<table>\n<tr>\n<p>\u65b9\u6cd5\u7b7e\u540d<\/p>\n<p>\u8bf4\u660e<\/p>\n<\/tr>\n<tbody>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>DatagramPacket(byte[] buf, int length)<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u6784\u9020\u63a5\u6536\u7528\u7684\u6570\u636e\u62a5&#xff0c;\u6570\u636e\u5b58buf\u91cc&#xff0c;\u6307\u5b9a\u63a5\u6536\u957f\u5ea6<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>DatagramPacket(byte[] buf, int offset, int length, SocketAddress address)<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u6784\u9020\u53d1\u9001\u7528\u7684\u6570\u636e\u62a5&#xff0c;\u6307\u5b9a\u6570\u636e&#043;\u76ee\u6807\u5730\u5740<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5e38\u7528\u65b9\u6cd5&#xff1a;<\/p>\n<table>\n<tr>\n<p>\u65b9\u6cd5\u7b7e\u540d<\/p>\n<p>\u8bf4\u660e<\/p>\n<\/tr>\n<tbody>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>InetAddress getAddress()<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u83b7\u53d6\u5bf9\u65b9\u7684IP\u5730\u5740<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>int getPort()<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u83b7\u53d6\u5bf9\u65b9\u7684\u7aef\u53e3\u53f7<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>byte[] getData()<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u83b7\u53d6\u6570\u636e\u62a5\u4e2d\u7684\u6570\u636e<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3 id=\"3.3%20%E5%AE%9E%E6%88%98%E4%B8%80%EF%BC%9AEcho%E5%9B%9E%E6%98%BE%E6%9C%8D%E5%8A%A1%E5%99%A8\">3.3 \u5b9e\u6218\u4e00&#xff1a;Echo\u56de\u663e\u670d\u52a1\u5668<\/h3>\n<p>\u6211\u4eec\u5148\u6765\u5199\u4e00\u4e2a\u6700\u7b80\u5355\u7684\u56de\u663e\u670d\u52a1\u5668&#xff1a;\u5ba2\u6237\u7aef\u53d1\u4ec0\u4e48&#xff0c;\u670d\u52a1\u5668\u5c31\u539f\u6837\u8fd4\u56de\u4ec0\u4e48\u3002\u8fd9\u662f\u7f51\u7edc\u7f16\u7a0b\u7684&#034;Hello World&#034;\u3002<\/p>\n<h4 id=\"UDP%20Echo%20%E6%9C%8D%E5%8A%A1%E7%AB%AF\">UDP Echo \u670d\u52a1\u7aef<\/h4>\n<p>\u670d\u52a1\u5668\u7684\u6838\u5fc3\u903b\u8f91\u5c31\u662f\u4e09\u6b65\u8d70&#xff1a;<\/p>\n<li>\n<p>\u8bfb\u53d6\u8bf7\u6c42\u5e76\u89e3\u6790&#xff1a;receive()\u63a5\u6536\u5ba2\u6237\u7aef\u53d1\u6765\u7684\u6570\u636e\u62a5<\/p>\n<\/li>\n<li>\n<p>\u6839\u636e\u8bf7\u6c42\u8ba1\u7b97\u54cd\u5e94&#xff1a;\u56de\u663e\u670d\u52a1\u5c31\u662f\u8bf7\u6c42\u7b49\u4e8e\u54cd\u5e94<\/p>\n<\/li>\n<li>\n<p>\u628a\u54cd\u5e94\u5199\u56de\u5ba2\u6237\u7aef&#xff1a;\u6784\u9020\u54cd\u5e94\u6570\u636e\u62a5&#xff0c;send()\u53d1\u56de<\/p>\n<\/li>\n<p>import java.io.IOException;<br \/>\nimport java.net.DatagramPacket;<br \/>\nimport java.net.DatagramSocket;<br \/>\nimport java.net.SocketException;<\/p>\n<p>public class UdpEchoServer {<br \/>\n    private DatagramSocket socket &#061; null;<\/p>\n<p>    \/\/ \u53c2\u6570\u662f\u670d\u52a1\u5668\u8981\u7ed1\u5b9a\u7684\u7aef\u53e3<br \/>\n    public UdpEchoServer(int port) throws SocketException {<br \/>\n        socket &#061; new DatagramSocket(port);<br \/>\n    }<\/p>\n<p>    \/\/ \u542f\u52a8\u670d\u52a1\u5668<br \/>\n    public void start() throws IOException {<br \/>\n        System.out.println(&#034;\u670d\u52a1\u5668\u542f\u52a8!&#034;);<br \/>\n        while (true) {<br \/>\n            \/\/ \u53cd\u590d\u6267\u884c\u9488\u5bf9\u5ba2\u6237\u7aef\u8bf7\u6c42\u7684\u5904\u7406\u903b\u8f91<br \/>\n            \/\/ 1. \u8bfb\u53d6\u8bf7\u6c42\u5e76\u89e3\u6790<br \/>\n            DatagramPacket requestPacket &#061; new DatagramPacket(new byte[4096], 4096);<br \/>\n            socket.receive(requestPacket);<br \/>\n            \/\/ \u8f6c\u6210\u5b57\u7b26\u4e32&#xff0c;\u7ea6\u5b9a\u5ba2\u6237\u7aef\u53d1\u7684\u662f\u6587\u672c<br \/>\n            String request &#061; new String(requestPacket.getData(), 0, requestPacket.getLength());<\/p>\n<p>            \/\/ 2. \u6839\u636e\u8bf7\u6c42\u8ba1\u7b97\u54cd\u5e94<br \/>\n            String response &#061; process(request);<\/p>\n<p>            \/\/ 3. \u628a\u54cd\u5e94\u5199\u56de\u7ed9\u5ba2\u6237\u7aef<br \/>\n            DatagramPacket responsePacket &#061; new DatagramPacket(response.getBytes(),<br \/>\n                    response.getBytes().length,<br \/>\n                    requestPacket.getSocketAddress());<br \/>\n            socket.send(responsePacket);<\/p>\n<p>            \/\/ \u6253\u5370\u65e5\u5fd7&#xff0c;\u65b9\u4fbf\u89c2\u5bdf<br \/>\n            System.out.printf(&#034;[%s:%d] req: %s, resp: %s\\\\n&#034;,<br \/>\n                    requestPacket.getAddress().toString(),<br \/>\n                    requestPacket.getPort(),<br \/>\n                    request, response);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u6839\u636e\u8bf7\u6c42\u8ba1\u7b97\u54cd\u5e94&#xff0c;\u56de\u663e\u5c31\u662f\u539f\u6837\u8fd4\u56de<br \/>\n    public String process(String request) {<br \/>\n        return request;<br \/>\n    }<\/p>\n<p>    public static void main(String[] args) throws IOException {<br \/>\n        UdpEchoServer server &#061; new UdpEchoServer(9090);<br \/>\n        server.start();<br \/>\n    }<br \/>\n}<\/p>\n<h4 id=\"UDP%20Echo%20%E5%AE%A2%E6%88%B7%E7%AB%AF\">UDP Echo \u5ba2\u6237\u7aef<\/h4>\n<p>\u5ba2\u6237\u7aef\u7684\u903b\u8f91\u4e5f\u662f\u56db\u6b65\u8d70&#xff1a;<\/p>\n<li>\n<p>\u4ece\u63a7\u5236\u53f0\u8bfb\u53d6\u7528\u6237\u8f93\u5165<\/p>\n<\/li>\n<li>\n<p>\u6784\u9020\u8bf7\u6c42\u5bf9\u8c61&#xff0c;\u53d1\u7ed9\u670d\u52a1\u5668<\/p>\n<\/li>\n<li>\n<p>\u8bfb\u53d6\u670d\u52a1\u5668\u7684\u54cd\u5e94<\/p>\n<\/li>\n<li>\n<p>\u663e\u793a\u5230\u5c4f\u5e55\u4e0a<\/p>\n<\/li>\n<p>import java.io.IOException;<br \/>\nimport java.net.*;<br \/>\nimport java.util.Scanner;<\/p>\n<p>public class UdpEchoClient {<br \/>\n    private DatagramSocket socket &#061; null;<br \/>\n    private String serverIp;<br \/>\n    private int serverPort;<\/p>\n<p>    public UdpEchoClient(String ip, int port) throws SocketException {<br \/>\n        serverIp &#061; ip;<br \/>\n        serverPort &#061; port;<br \/>\n        \/\/ \u5ba2\u6237\u7aef\u4e0d\u6307\u5b9a\u7aef\u53e3&#xff0c;\u7cfb\u7edf\u81ea\u52a8\u5206\u914d\u7a7a\u95f2\u7aef\u53e3<br \/>\n        socket &#061; new DatagramSocket();<br \/>\n    }<\/p>\n<p>    public void start() throws IOException {<br \/>\n        Scanner scanner &#061; new Scanner(System.in);<br \/>\n        System.out.println(&#034;\u5ba2\u6237\u7aef\u542f\u52a8!&#034;);<br \/>\n        while (true) {<br \/>\n            \/\/ 1. \u4ece\u63a7\u5236\u53f0\u8bfb\u53d6\u7528\u6237\u8f93\u5165<br \/>\n            System.out.print(&#034;-&gt; &#034;);<br \/>\n            String request &#061; scanner.next();<\/p>\n<p>            \/\/ 2. \u6784\u9020\u8bf7\u6c42\u5bf9\u8c61&#xff0c;\u53d1\u7ed9\u670d\u52a1\u5668<br \/>\n            DatagramPacket requestPacket &#061; new DatagramPacket(request.getBytes(),<br \/>\n                    request.getBytes().length,<br \/>\n                    InetAddress.getByName(serverIp), serverPort);<br \/>\n            socket.send(requestPacket);<\/p>\n<p>            \/\/ 3. \u8bfb\u53d6\u670d\u52a1\u5668\u54cd\u5e94<br \/>\n            DatagramPacket responsePacket &#061; new DatagramPacket(new byte[4096], 4096);<br \/>\n            socket.receive(responsePacket);<br \/>\n            String response &#061; new String(responsePacket.getData(), 0, responsePacket.getLength());<\/p>\n<p>            \/\/ 4. \u663e\u793a\u5230\u5c4f\u5e55<br \/>\n            System.out.println(response);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    public static void main(String[] args) throws IOException {<br \/>\n        UdpEchoClient client &#061; new UdpEchoClient(&#034;127.0.0.1&#034;, 9090);<br \/>\n        client.start();<br \/>\n    }<br \/>\n}<\/p>\n<p>\u8fd0\u884c\u65b9\u5f0f&#xff1a;\u5148\u542f\u52a8\u670d\u52a1\u7aef&#xff0c;\u518d\u542f\u52a8\u5ba2\u6237\u7aef&#xff0c;\u5728\u5ba2\u6237\u7aef\u63a7\u5236\u53f0\u8f93\u5165\u6587\u5b57&#xff0c;\u56de\u8f66\u5c31\u80fd\u770b\u5230\u670d\u52a1\u5668\u56de\u663e\u56de\u6765\u7684\u5185\u5bb9\u3002\u6ce8\u610f127.0.0.1\u662f\u672c\u673a\u56de\u73af\u5730\u5740&#xff0c;\u672c\u5730\u8c03\u8bd5\u7528\u3002<\/p>\n<h3 id=\"3.4%20%E5%AE%9E%E6%88%98%E4%BA%8C%EF%BC%9A%E8%8B%B1%E8%AF%91%E6%B1%89%E8%AF%8D%E5%85%B8%E6%9C%8D%E5%8A%A1%E5%99%A8\">3.4 \u5b9e\u6218\u4e8c&#xff1a;\u82f1\u8bd1\u6c49\u8bcd\u5178\u670d\u52a1\u5668<\/h3>\n<p>\u5b66\u4f1a\u4e86\u56de\u663e\u670d\u52a1\u5668&#xff0c;\u6211\u4eec\u6765\u5347\u7ea7\u4e00\u4e0b&#xff0c;\u505a\u4e00\u4e2a\u82f1\u8bd1\u6c49\u8bcd\u5178\u670d\u52a1\u5668\u3002\u5ba2\u6237\u7aef\u8f93\u5165\u82f1\u6587\u5355\u8bcd&#xff0c;\u670d\u52a1\u5668\u8fd4\u56de\u5bf9\u5e94\u7684\u4e2d\u6587\u7ffb\u8bd1\u3002<\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u7528\u7ee7\u627f\u7684\u65b9\u5f0f&#xff0c;\u76f4\u63a5\u590d\u7528UdpEchoServer\u7684\u4ee3\u7801&#xff0c;\u53ea\u9700\u8981\u91cd\u5199process\u65b9\u6cd5\u5c31\u884c&#xff0c;\u975e\u5e38\u65b9\u4fbf&#xff01;<\/p>\n<p>import java.net.SocketException;<br \/>\nimport java.util.HashMap;<br \/>\nimport java.util.Map;<\/p>\n<p>public class UdpDictServer extends UdpEchoServer {<br \/>\n    private Map&lt;String, String&gt; dict &#061; new HashMap&lt;&gt;();<\/p>\n<p>    public UdpDictServer(int port) throws SocketException {<br \/>\n        super(port);<br \/>\n        \/\/ \u521d\u59cb\u5316\u8bcd\u5178&#xff0c;\u8fd9\u91cc\u7b80\u5355\u653e\u51e0\u4e2a\u5355\u8bcd<br \/>\n        dict.put(&#034;cat&#034;, &#034;\u5c0f\u732b&#034;);<br \/>\n        dict.put(&#034;dog&#034;, &#034;\u5c0f\u72d7&#034;);<br \/>\n        dict.put(&#034;hello&#034;, &#034;\u4f60\u597d&#034;);<br \/>\n        dict.put(&#034;world&#034;, &#034;\u4e16\u754c&#034;);<br \/>\n        dict.put(&#034;java&#034;, &#034;\u722a\u54c7\u7f16\u7a0b\u8bed\u8a00&#034;);<br \/>\n        \/\/ \u53ef\u4ee5\u7ee7\u7eed\u52a0\u5343\u5343\u4e07\u4e07\u4e2a\u5355\u8bcd&#8230;<br \/>\n    }<\/p>\n<p>    \/\/ \u91cd\u5199process\u65b9\u6cd5&#xff0c;\u5b9e\u73b0\u67e5\u8bcd\u5178\u7684\u903b\u8f91<br \/>\n    &#064;Override<br \/>\n    public String process(String request) {<br \/>\n        \/\/ \u67e5\u5230\u5c31\u8fd4\u56de\u7ffb\u8bd1&#xff0c;\u67e5\u4e0d\u5230\u8fd4\u56de\u63d0\u793a<br \/>\n        return dict.getOrDefault(request, &#034;\u8be5\u8bcd\u6ca1\u6709\u67e5\u8be2\u5230!&#034;);<br \/>\n    }<\/p>\n<p>    public static void main(String[] args) throws Exception {<br \/>\n        UdpDictServer server &#061; new UdpDictServer(9090);<br \/>\n        server.start();<br \/>\n    }<br \/>\n} <\/p>\n<p>\u5ba2\u6237\u7aef\u4e0d\u7528\u6539&#xff0c;\u76f4\u63a5\u7528\u4e4b\u524d\u7684UdpEchoClient\u5c31\u884c&#xff0c;\u8f93\u5165\u82f1\u6587\u5355\u8bcd\u5c31\u80fd\u62ff\u5230\u7ffb\u8bd1\u4e86\u3002<\/p>\n<p>\u8bbe\u8ba1\u601d\u60f3&#xff1a;\u4f60\u770b&#xff0c;\u901a\u8fc7\u7ee7\u627f&#043;\u91cd\u5199&#xff0c;\u6211\u4eec\u628a\u7f51\u7edc\u901a\u4fe1\u7684\u901a\u7528\u903b\u8f91\u548c\u4e1a\u52a1\u903b\u8f91\u5206\u5f00\u4e86\u3002\u4ee5\u540e\u4e0d\u7ba1\u505a\u4ec0\u4e48\u4e1a\u52a1&#xff0c;\u53ea\u8981\u91cd\u5199process\u65b9\u6cd5\u5c31\u884c&#xff0c;\u7f51\u7edc\u90e8\u5206\u5b8c\u5168\u4e0d\u7528\u52a8\u3002\u8fd9\u5c31\u662f\u4ee3\u7801\u590d\u7528\u7684\u9b45\u529b&#xff01;<\/p>\n<h2 id=\"%E5%9B%9B%E3%80%81TCP%E6%B5%81%E5%A5%97%E6%8E%A5%E5%AD%97%E7%BC%96%E7%A8%8B\">\u56db\u3001TCP\u6d41\u5957\u63a5\u5b57\u7f16\u7a0b<\/h2>\n<h3 id=\"4.1%20TCP%E9%80%9A%E4%BF%A1%E6%A8%A1%E5%9E%8B\">4.1 TCP\u901a\u4fe1\u6a21\u578b<\/h3>\n<p>TCP\u662f\u6709\u8fde\u63a5\u3001\u9762\u5411\u5b57\u8282\u6d41\u7684\u534f\u8bae\u3002\u548cUDP\u4e0d\u540c&#xff0c;TCP\u901a\u4fe1\u524d\u5fc5\u987b\u5148\u5efa\u7acb\u8fde\u63a5&#xff0c;\u5c31\u50cf\u6253\u7535\u8bdd\u5148\u62e8\u53f7\u63a5\u901a\u4e86\u624d\u80fd\u8bf4\u8bdd\u3002<\/p>\n<p>Java\u4e2dTCP\u901a\u4fe1\u4e3b\u8981\u7528\u4e24\u4e2a\u7c7b&#xff1a;<\/p>\n<ul>\n<li>\n<p>ServerSocket&#xff1a;\u670d\u52a1\u7aef\u7528\u7684&#xff0c;\u8d1f\u8d23\u76d1\u542c\u7aef\u53e3&#xff0c;\u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5<\/p>\n<\/li>\n<li>\n<p>Socket&#xff1a;\u53cc\u65b9\u5efa\u7acb\u8fde\u63a5\u540e&#xff0c;\u7528Socket\u6765\u6536\u53d1\u6570\u636e&#xff0c;\u901a\u8fc7\u8f93\u5165\u8f93\u51fa\u6d41\u64cd\u4f5c<\/p>\n<\/li>\n<\/ul>\n<p>TCP\u901a\u4fe1\u7684\u57fa\u672c\u6d41\u7a0b&#xff1a;<\/p>\n<li>\n<p>\u670d\u52a1\u7aef\u521b\u5efaServerSocket&#xff0c;\u7ed1\u5b9a\u7aef\u53e3&#xff0c;\u8c03\u7528accept()\u963b\u585e\u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5<\/p>\n<\/li>\n<li>\n<p>\u5ba2\u6237\u7aef\u521b\u5efaSocket&#xff0c;\u6307\u5b9a\u670d\u52a1\u7aefIP\u548c\u7aef\u53e3&#xff0c;\u53d1\u8d77\u8fde\u63a5\u8bf7\u6c42<\/p>\n<\/li>\n<li>\n<p>\u8fde\u63a5\u5efa\u7acb\u6210\u529f\u540e&#xff0c;\u670d\u52a1\u7aefaccept()\u8fd4\u56de\u4e00\u4e2aSocket\u5bf9\u8c61<\/p>\n<\/li>\n<li>\n<p>\u53cc\u65b9\u901a\u8fc7Socket\u83b7\u53d6InputStream\u548cOutputStream&#xff0c;\u50cf\u8bfb\u5199\u6587\u4ef6\u4e00\u6837\u6536\u53d1\u6570\u636e<\/p>\n<\/li>\n<li>\n<p>\u901a\u4fe1\u7ed3\u675f&#xff0c;\u5173\u95edSocket\u91ca\u653e\u8d44\u6e90<\/p>\n<\/li>\n<h3 id=\"4.2%20%E6%A0%B8%E5%BF%83API%E4%BB%8B%E7%BB%8D\">4.2 \u6838\u5fc3API\u4ecb\u7ecd<\/h3>\n<h4 id=\"ServerSocket\">ServerSocket<\/h4>\n<p>ServerSocket\u662f\u521b\u5efaTCP\u670d\u52a1\u7aefSocket\u7684API\u3002<\/p>\n<table>\n<tr>\n<p>\u65b9\u6cd5\u7b7e\u540d<\/p>\n<p>\u8bf4\u660e<\/p>\n<\/tr>\n<tbody>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>ServerSocket(int port)<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u521b\u5efa\u670d\u52a1\u7aefSocket&#xff0c;\u7ed1\u5b9a\u6307\u5b9a\u7aef\u53e3<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>Socket accept()<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u76d1\u542c\u7aef\u53e3&#xff0c;\u6709\u5ba2\u6237\u7aef\u8fde\u63a5\u5c31\u8fd4\u56deSocket&#xff0c;\u5426\u5219\u963b\u585e\u7b49\u5f85<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>void close()<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u5173\u95ed\u5957\u63a5\u5b57<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4 id=\"Socket\">Socket<\/h4>\n<p>Socket\u5c31\u662f\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u5efa\u7acb\u8fde\u63a5\u540e&#xff0c;\u7528\u6765\u6536\u53d1\u6570\u636e\u7684\u5bf9\u8c61\u3002\u4e0d\u7ba1\u5ba2\u6237\u7aef\u8fd8\u662f\u670d\u52a1\u7aef&#xff0c;\u8fde\u63a5\u5efa\u7acb\u540e\u90fd\u7528Socket\u6765\u901a\u4fe1\u3002<\/p>\n<table>\n<tr>\n<p>\u65b9\u6cd5\u7b7e\u540d<\/p>\n<p>\u8bf4\u660e<\/p>\n<\/tr>\n<tbody>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>Socket(String host, int port)<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u521b\u5efa\u5ba2\u6237\u7aefSocket&#xff0c;\u8fde\u63a5\u6307\u5b9a\u4e3b\u673a\u7684\u6307\u5b9a\u7aef\u53e3<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>InetAddress getInetAddress()<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u8fd4\u56de\u8fde\u63a5\u7684\u5bf9\u7aef\u5730\u5740<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>InputStream getInputStream()<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u83b7\u53d6\u8f93\u5165\u6d41&#xff0c;\u7528\u6765\u8bfb\u6570\u636e<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>OutputStream getOutputStream()<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u83b7\u53d6\u8f93\u51fa\u6d41&#xff0c;\u7528\u6765\u5199\u6570\u636e<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6ce8\u610f&#xff1a;TCP\u662f\u9762\u5411\u5b57\u8282\u6d41\u7684&#xff0c;\u6240\u4ee5\u6211\u4eec\u662f\u901a\u8fc7InputStream\u548cOutputStream\u6765\u8bfb\u5199\u6570\u636e&#xff0c;\u5c31\u548c\u64cd\u4f5c\u672c\u5730\u6587\u4ef6\u7684\u6d41\u4e00\u6a21\u4e00\u6837&#xff0c;\u975e\u5e38\u597d\u4e0a\u624b&#xff01;<\/p>\n<h3 id=\"4.3%20%E5%AE%9E%E6%88%98%EF%BC%9AEcho%E5%9B%9E%E6%98%BE%E6%9C%8D%E5%8A%A1%E5%99%A8\">4.3 \u5b9e\u6218&#xff1a;Echo\u56de\u663e\u670d\u52a1\u5668<\/h3>\n<p>\u6211\u4eec\u540c\u6837\u6765\u5199\u4e00\u4e2aTCP\u7248\u672c\u7684Echo\u56de\u663e\u670d\u52a1\u5668&#xff0c;\u5bf9\u6bd4\u4e00\u4e0b\u548cUDP\u7248\u672c\u6709\u4ec0\u4e48\u4e0d\u540c\u3002<\/p>\n<h4 id=\"TCP%20Echo%20%E6%9C%8D%E5%8A%A1%E7%AB%AF\">TCP Echo \u670d\u52a1\u7aef<\/h4>\n<p>\u670d\u52a1\u7aef\u7684\u601d\u8def&#xff1a;<\/p>\n<li>\n<p>\u521b\u5efaServerSocket\u7ed1\u5b9a\u7aef\u53e3<\/p>\n<\/li>\n<li>\n<p>\u8c03\u7528accept()\u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5<\/p>\n<\/li>\n<li>\n<p>\u8fde\u63a5\u5efa\u7acb\u540e&#xff0c;\u901a\u8fc7Socket\u83b7\u53d6\u8f93\u5165\u8f93\u51fa\u6d41<\/p>\n<\/li>\n<li>\n<p>\u5faa\u73af\u8bfb\u53d6\u8bf7\u6c42\u3001\u8ba1\u7b97\u54cd\u5e94\u3001\u8fd4\u56de\u54cd\u5e94<\/p>\n<\/li>\n<li>\n<p>\u5ba2\u6237\u7aef\u65ad\u5f00\u540e&#xff0c;\u5173\u95edSocket<\/p>\n<\/li>\n<p>import java.io.*;<br \/>\nimport java.net.ServerSocket;<br \/>\nimport java.net.Socket;<\/p>\n<p>public class TcpEchoServer {<br \/>\n    private ServerSocket serverSocket &#061; null;<\/p>\n<p>    \/\/ \u7ed1\u5b9a\u7aef\u53e3\u53f7<br \/>\n    public TcpEchoServer(int port) throws IOException {<br \/>\n        serverSocket &#061; new ServerSocket(port);<br \/>\n    }<\/p>\n<p>    \/\/ \u542f\u52a8\u670d\u52a1\u5668<br \/>\n    public void start() throws IOException {<br \/>\n        System.out.println(&#034;\u670d\u52a1\u5668\u542f\u52a8!&#034;);<br \/>\n        while (true) {<br \/>\n            \/\/ \u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5<br \/>\n            Socket clientSocket &#061; serverSocket.accept();<br \/>\n            processConnection(clientSocket);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u5904\u7406\u4e00\u4e2a\u8fde\u63a5\u7684\u903b\u8f91<br \/>\n    private void processConnection(Socket clientSocket) {<br \/>\n        System.out.printf(&#034;[%s:%d] \u5ba2\u6237\u7aef\u4e0a\u7ebf!\\\\n&#034;,<br \/>\n                clientSocket.getInetAddress().toString(),<br \/>\n                clientSocket.getPort());<\/p>\n<p>        \/\/ \u83b7\u53d6\u8f93\u5165\u8f93\u51fa\u6d41<br \/>\n        try (InputStream inputStream &#061; clientSocket.getInputStream();<br \/>\n             OutputStream outputStream &#061; clientSocket.getOutputStream()) {<\/p>\n<p>            \/\/ \u4e00\u6b21\u8fde\u63a5\u4e2d\u53ef\u80fd\u6709\u591a\u6b21\u8bf7\u6c42\u54cd\u5e94<br \/>\n            while (true) {<br \/>\n                \/\/ 1. \u8bfb\u53d6\u8bf7\u6c42&#xff0c;\u7528Scanner\u65b9\u4fbf\u8bfb\u53d6\u6587\u672c<br \/>\n                Scanner scanner &#061; new Scanner(inputStream);<br \/>\n                if (!scanner.hasNext()) {<br \/>\n                    \/\/ \u8bfb\u53d6\u5b8c\u6bd5&#xff0c;\u5ba2\u6237\u7aef\u4e0b\u7ebf<br \/>\n                    System.out.printf(&#034;[%s:%d] \u5ba2\u6237\u7aef\u4e0b\u7ebf!\\\\n&#034;,<br \/>\n                            clientSocket.getInetAddress().toString(),<br \/>\n                            clientSocket.getPort());<br \/>\n                    break;<br \/>\n                }<br \/>\n                String request &#061; scanner.next();<\/p>\n<p>                \/\/ 2. \u6839\u636e\u8bf7\u6c42\u8ba1\u7b97\u54cd\u5e94<br \/>\n                String response &#061; process(request);<\/p>\n<p>                \/\/ 3. \u628a\u54cd\u5e94\u5199\u56de\u5ba2\u6237\u7aef&#xff0c;\u7528PrintWriter\u65b9\u4fbf\u5199\u6587\u672c<br \/>\n                PrintWriter writer &#061; new PrintWriter(outputStream);<br \/>\n                \/\/ \u7528println\u5e26\u4e0a\u6362\u884c&#xff0c;\u65b9\u4fbf\u5ba2\u6237\u7aefscanner\u8bfb\u53d6<br \/>\n                writer.println(response);<br \/>\n                writer.flush(); \/\/ \u5237\u65b0\u7f13\u51b2\u533a&#xff0c;\u786e\u4fdd\u6570\u636e\u771f\u7684\u53d1\u51fa\u53bb<\/p>\n<p>                \/\/ \u6253\u5370\u65e5\u5fd7<br \/>\n                System.out.printf(&#034;[%s:%d] req: %s, resp: %s\\\\n&#034;,<br \/>\n                        clientSocket.getInetAddress().toString(),<br \/>\n                        clientSocket.getPort(),<br \/>\n                        request, response);<br \/>\n            }<br \/>\n        } catch (IOException e) {<br \/>\n            e.printStackTrace();<br \/>\n        } finally {<br \/>\n            \/\/ \u786e\u4fdd\u5173\u95edsocket\u91ca\u653e\u8d44\u6e90<br \/>\n            try {<br \/>\n                clientSocket.close();<br \/>\n            } catch (IOException e) {<br \/>\n                e.printStackTrace();<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    public String process(String request) {<br \/>\n        return request;<br \/>\n    }<\/p>\n<p>    public static void main(String[] args) throws IOException {<br \/>\n        TcpEchoServer server &#061; new TcpEchoServer(9090);<br \/>\n        server.start();<br \/>\n    }<br \/>\n}<\/p>\n<h4 id=\"TCP%20Echo%20%E5%AE%A2%E6%88%B7%E7%AB%AF\">TCP Echo \u5ba2\u6237\u7aef<\/h4>\n<p>import java.io.*;<br \/>\nimport java.net.Socket;<br \/>\nimport java.util.Scanner;<\/p>\n<p>public class TcpEchoClient {<br \/>\n    private Socket socket &#061; null;<\/p>\n<p>    \/\/ \u521b\u5efaSocket\u65f6\u5c31\u4f1a\u5efa\u7acbTCP\u8fde\u63a5<br \/>\n    public TcpEchoClient(String serverIp, int serverPort) throws IOException {<br \/>\n        socket &#061; new Socket(serverIp, serverPort);<br \/>\n    }<\/p>\n<p>    public void start() {<br \/>\n        System.out.println(&#034;\u5ba2\u6237\u7aef\u542f\u52a8&#034;);<br \/>\n        Scanner scannerConsole &#061; new Scanner(System.in);<\/p>\n<p>        try (InputStream inputStream &#061; socket.getInputStream();<br \/>\n             OutputStream outputStream &#061; socket.getOutputStream()) {<\/p>\n<p>            while (true) {<br \/>\n                \/\/ 1. \u4ece\u63a7\u5236\u53f0\u8f93\u5165<br \/>\n                System.out.print(&#034;-&gt; &#034;);<br \/>\n                String request &#061; scannerConsole.next();<\/p>\n<p>                \/\/ 2. \u628a\u8bf7\u6c42\u53d1\u7ed9\u670d\u52a1\u5668<br \/>\n                PrintWriter printWriter &#061; new PrintWriter(outputStream);<br \/>\n                printWriter.println(request);<br \/>\n                printWriter.flush(); \/\/ \u522b\u5fd8\u4e86flush&#xff01;<\/p>\n<p>                \/\/ 3. \u8bfb\u53d6\u670d\u52a1\u5668\u54cd\u5e94<br \/>\n                Scanner scannerNetwork &#061; new Scanner(inputStream);<br \/>\n                String response &#061; scannerNetwork.next();<\/p>\n<p>                \/\/ 4. \u6253\u5370\u7ed3\u679c<br \/>\n                System.out.println(response);<br \/>\n            }<br \/>\n        } catch (IOException e) {<br \/>\n            e.printStackTrace();<br \/>\n        }<br \/>\n    }<\/p>\n<p>    public static void main(String[] args) throws IOException {<br \/>\n        TcpEchoClient client &#061; new TcpEchoClient(&#034;127.0.0.1&#034;, 9090);<br \/>\n        client.start();<br \/>\n    }<br \/>\n} <\/p>\n<p>\u6ce8\u610f\u4e24\u4e2a\u5751&#xff1a;<\/p>\n<p>1. \u4e00\u5b9a\u8981\u7528println\u800c\u4e0d\u662fprint&#xff0c;\u672b\u5c3e\u5e26\u6362\u884c&#xff0c;\u8fd9\u6837\u5bf9\u65b9\u7684scanner.next()\u624d\u80fd\u6b63\u786e\u8bfb\u53d6<\/p>\n<p>2. \u5199\u5b8c\u4e00\u5b9a\u8981flush()&#xff01;\u8f93\u51fa\u6d41\u6709\u7f13\u51b2\u533a&#xff0c;\u4e0d\u5237\u65b0\u6570\u636e\u53ef\u80fd\u8fd8\u5728\u7f13\u51b2\u533a\u91cc\u6ca1\u53d1\u51fa\u53bb<\/p>\n<h2 id=\"%E4%BA%94%E3%80%81%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96\">\u4e94\u3001\u670d\u52a1\u5668\u6027\u80fd\u4f18\u5316<\/h2>\n<h3 id=\"5.1%20%E4%B8%BA%E4%BB%80%E4%B9%88%E9%9C%80%E8%A6%81%E5%A4%9A%E7%BA%BF%E7%A8%8B%EF%BC%9F\">5.1 \u4e3a\u4ec0\u4e48\u9700\u8981\u591a\u7ebf\u7a0b&#xff1f;<\/h3>\n<p>\u521a\u624d\u5199\u7684TCP\u670d\u52a1\u5668\u6709\u4e2a\u5927\u95ee\u9898&#xff1a;\u5355\u7ebf\u7a0b\u7684&#xff0c;\u4e00\u6b21\u53ea\u80fd\u5904\u7406\u4e00\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u3002<\/p>\n<p>\u56e0\u4e3aaccept()\u63a5\u6536\u5230\u4e00\u4e2a\u8fde\u63a5\u540e&#xff0c;\u5c31\u8fdb\u5165processConnection()\u65b9\u6cd5\u5faa\u73af\u5904\u7406\u8fd9\u4e2a\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u4e86\u3002\u5728\u8fd9\u4e2a\u5ba2\u6237\u7aef\u65ad\u5f00\u4e4b\u524d&#xff0c;accept()\u4e0d\u4f1a\u518d\u88ab\u8c03\u7528&#xff0c;\u65b0\u7684\u5ba2\u6237\u7aef\u5c31\u8fde\u4e0d\u4e0a\u4e86\u3002<\/p>\n<p>\u8fd9\u80af\u5b9a\u4e0d\u884c&#xff0c;\u771f\u5b9e\u7684\u670d\u52a1\u5668\u8981\u540c\u65f6\u670d\u52a1\u6210\u767e\u4e0a\u5343\u4e2a\u5ba2\u6237\u7aef\u3002\u600e\u4e48\u89e3\u51b3&#xff1f;\u5f88\u7b80\u5355&#xff0c;\u6bcf\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u5206\u914d\u4e00\u4e2a\u7ebf\u7a0b&#xff0c;\u8ba9\u5b83\u4eec\u5e76\u53d1\u5904\u7406\u3002<\/p>\n<h3 id=\"5.2%20%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%89%88%E6%9C%AC%E6%9C%8D%E5%8A%A1%E5%99%A8\">5.2 \u591a\u7ebf\u7a0b\u7248\u672c\u670d\u52a1\u5668<\/h3>\n<p>\u6539\u9020\u5f88\u7b80\u5355&#xff0c;\u53ea\u9700\u8981\u4fee\u6539start\u65b9\u6cd5&#xff0c;\u6bcf\u6b21accept\u5230\u4e00\u4e2a\u8fde\u63a5&#xff0c;\u5c31\u5f00\u4e00\u4e2a\u65b0\u7ebf\u7a0b\u53bb\u5904\u7406&#xff1a;<\/p>\n<p>public void start() throws IOException {<br \/>\n    System.out.println(&#034;\u670d\u52a1\u5668\u542f\u52a8!&#034;);<br \/>\n    while (true) {<br \/>\n        Socket clientSocket &#061; serverSocket.accept();<br \/>\n        \/\/ \u6bcf\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u5f00\u4e00\u4e2a\u7ebf\u7a0b\u5904\u7406<br \/>\n        Thread t &#061; new Thread(() -&gt; {<br \/>\n            processConnection(clientSocket);<br \/>\n        });<br \/>\n        t.start();<br \/>\n    }<br \/>\n}<\/p>\n<p>\u5c31\u6539\u8fd9\u4e48\u51e0\u884c&#xff0c;\u670d\u52a1\u5668\u5c31\u80fd\u540c\u65f6\u5904\u7406\u591a\u4e2a\u5ba2\u6237\u7aef\u4e86&#xff01;\u4e3b\u7ebf\u7a0b\u53ea\u8d1f\u8d23\u63a5\u5ba2&#xff08;accept&#xff09;&#xff0c;\u63a5\u5230\u4e86\u5c31\u4ea4\u7ed9\u5b50\u7ebf\u7a0b\u53bb\u670d\u52a1&#xff0c;\u4e3b\u7ebf\u7a0b\u7ee7\u7eed\u63a5\u4e0b\u4e00\u4e2a\u3002<\/p>\n<h3 id=\"5.3%20%E7%BA%BF%E7%A8%8B%E6%B1%A0%E7%89%88%E6%9C%AC%E6%9C%8D%E5%8A%A1%E5%99%A8\">5.3 \u7ebf\u7a0b\u6c60\u7248\u672c\u670d\u52a1\u5668<\/h3>\n<p>\u591a\u7ebf\u7a0b\u7248\u672c\u867d\u7136\u80fd\u7528&#xff0c;\u4f46\u5982\u679c\u5ba2\u6237\u7aef\u7279\u522b\u591a&#xff0c;\u9891\u7e41\u521b\u5efa\u9500\u6bc1\u7ebf\u7a0b\u5f00\u9500\u5f88\u5927\u3002\u8fd9\u65f6\u5019\u5c31\u53ef\u4ee5\u7528\u7ebf\u7a0b\u6c60\u6765\u4f18\u5316&#xff0c;\u590d\u7528\u7ebf\u7a0b\u51cf\u5c11\u5f00\u9500\u3002<\/p>\n<p>public void start() throws IOException {<br \/>\n    System.out.println(&#034;\u670d\u52a1\u5668\u542f\u52a8!&#034;);<br \/>\n    \/\/ \u521b\u5efa\u4e00\u4e2a\u7f13\u5b58\u7ebf\u7a0b\u6c60<br \/>\n    ExecutorService service &#061; Executors.newCachedThreadPool();<br \/>\n    while (true) {<br \/>\n        Socket clientSocket &#061; serverSocket.accept();<br \/>\n        \/\/ \u63d0\u4ea4\u4efb\u52a1\u5230\u7ebf\u7a0b\u6c60<br \/>\n        service.submit(new Runnable() {<br \/>\n            &#064;Override<br \/>\n            public void run() {<br \/>\n                processConnection(clientSocket);<br \/>\n            }<br \/>\n        });<br \/>\n    }<br \/>\n} <\/p>\n<p>\u5c0f\u77e5\u8bc6&#xff1a;newCachedThreadPool\u4f1a\u81ea\u52a8\u521b\u5efa\u7ebf\u7a0b&#xff0c;\u7a7a\u95f2\u7ebf\u7a0b\u4f1a\u88ab\u56de\u6536&#xff0c;\u9002\u5408\u77ed\u4efb\u52a1\u591a\u7684\u573a\u666f\u3002\u5982\u679c\u662f\u957f\u8fde\u63a5\u573a\u666f&#xff0c;\u53ef\u80fd\u9700\u8981\u81ea\u5b9a\u4e49\u7ebf\u7a0b\u6c60\u53c2\u6570\u66f4\u5408\u9002\u3002<\/p>\n<h2 id=\"%E5%85%AD%E3%80%81%E9%95%BF%E7%9F%AD%E8%BF%9E%E6%8E%A5%E8%AF%A6%E8%A7%A3\">\u516d\u3001\u957f\u77ed\u8fde\u63a5\u8be6\u89e3<\/h2>\n<p>TCP\u662f\u6709\u8fde\u63a5\u7684&#xff0c;\u90a3\u8fde\u63a5\u4ec0\u4e48\u65f6\u5019\u5173\u5462&#xff1f;\u8fd9\u5c31\u5f15\u51fa\u4e86\u77ed\u8fde\u63a5\u548c\u957f\u8fde\u63a5\u7684\u6982\u5ff5\u3002<\/p>\n<h4 id=\"%E7%9F%AD%E8%BF%9E%E6%8E%A5\">\u77ed\u8fde\u63a5<\/h4>\n<p>\u6bcf\u6b21\u6536\u53d1\u5b8c\u4e00\u6b21\u6570\u636e\u5c31\u5173\u95ed\u8fde\u63a5&#xff0c;\u5c31\u662f\u77ed\u8fde\u63a5\u3002\u4e5f\u5c31\u662f\u8bf4&#xff0c;\u77ed\u8fde\u63a5\u4e00\u6b21\u53ea\u80fd\u6536\u53d1\u4e00\u6b21\u6570\u636e\u3002\u4e0b\u6b21\u8981\u53d1\u6570\u636e&#xff0c;\u5f97\u91cd\u65b0\u5efa\u7acb\u8fde\u63a5\u3002<\/p>\n<h4 id=\"%E9%95%BF%E8%BF%9E%E6%8E%A5\" style=\"background-color:transparent\">\u957f\u8fde\u63a5<\/h4>\n<p>\u4e0d\u5173\u95ed\u8fde\u63a5&#xff0c;\u4e00\u76f4\u4fdd\u6301\u8fde\u63a5\u72b6\u6001&#xff0c;\u53cc\u65b9\u4e0d\u505c\u7684\u6536\u53d1\u6570\u636e&#xff0c;\u5c31\u662f\u957f\u8fde\u63a5\u3002\u957f\u8fde\u63a5\u53ef\u4ee5\u591a\u6b21\u6536\u53d1\u6570\u636e\u3002<\/p>\n<h4 id=\"%E4%B8%A4%E8%80%85%E5%AF%B9%E6%AF%94\">\u4e24\u8005\u5bf9\u6bd4<\/h4>\n<table>\n<tr>\n<p>\u5bf9\u6bd4\u9879<\/p>\n<p>\u77ed\u8fde\u63a5<\/p>\n<p>\u957f\u8fde\u63a5<\/p>\n<\/tr>\n<tbody>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u5efa\u8fde\u5173\u8fde\u5f00\u9500<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u6bcf\u6b21\u8bf7\u6c42\u90fd\u8981\u5efa\u8fde\u5173\u8fde&#xff0c;\u5f00\u9500\u5927<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u53ea\u5efa\u4e00\u6b21\u8fde\u63a5&#xff0c;\u540e\u7eed\u76f4\u63a5\u4f20\u8f93&#xff0c;\u5f00\u9500\u5c0f<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u4e3b\u52a8\u53d1\u8bf7\u6c42<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u4e00\u822c\u662f\u5ba2\u6237\u7aef\u4e3b\u52a8\u53d1<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u90fd\u53ef\u4ee5\u4e3b\u52a8\u53d1<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u9002\u7528\u573a\u666f<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u8bf7\u6c42\u9891\u7387\u4e0d\u9ad8&#xff0c;\u5982\u6d4f\u89c8\u7f51\u9875<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u901a\u4fe1\u9891\u7e41&#xff0c;\u5982\u804a\u5929\u5ba4\u3001\u5b9e\u65f6\u6e38\u620f<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6269\u5c55\u4e86\u89e3&#xff1a;\u6211\u4eec\u4e0a\u9762\u5199\u7684Echo\u670d\u52a1\u5668\u7528\u7684\u5c31\u662f\u957f\u8fde\u63a5\u6a21\u5f0f&#xff0c;\u5ba2\u6237\u7aef\u8fde\u4e0a\u540e\u53ef\u4ee5\u4e00\u76f4\u53d1\u6d88\u606f\u3002\u800cHTTP\u534f\u8bae\u65e9\u671f\u7528\u7684\u662f\u77ed\u8fde\u63a5&#xff0c;\u73b0\u5728HTTP\/1.1\u9ed8\u8ba4\u4e5f\u662f\u957f\u8fde\u63a5\u4e86\u3002<\/p>\n<p>\u53e6\u5916&#xff0c;\u57fa\u4e8eBIO&#xff08;\u540c\u6b65\u963b\u585eIO&#xff09;\u7684\u957f\u8fde\u63a5\u6bcf\u4e2a\u8fde\u63a5\u5360\u4e00\u4e2a\u7ebf\u7a0b&#xff0c;\u5e76\u53d1\u9ad8\u4e86\u625b\u4e0d\u4f4f\u3002\u771f\u6b63\u7684\u9ad8\u5e76\u53d1\u670d\u52a1\u4e00\u822c\u7528NIO&#xff08;\u540c\u6b65\u975e\u963b\u585eIO&#xff09;\u6765\u5b9e\u73b0\u957f\u8fde\u63a5&#xff0c;\u6027\u80fd\u4f1a\u597d\u5f88\u591a\u3002<\/p>\n<h2 id=\"%E4%B8%83%E3%80%81%E5%B8%B8%E8%A7%81%E5%9D%91%E7%82%B9%EF%BC%9A%E7%AB%AF%E5%8F%A3%E8%A2%AB%E5%8D%A0%E7%94%A8%E6%80%8E%E4%B9%88%E5%8A%9E%EF%BC%9F\">\u4e03\u3001\u5e38\u89c1\u5751\u70b9&#xff1a;\u7aef\u53e3\u88ab\u5360\u7528\u600e\u4e48\u529e&#xff1f;<\/h2>\n<p>\u521d\u5b66Socket\u7f16\u7a0b&#xff0c;\u6700\u5bb9\u6613\u9047\u5230\u7684\u62a5\u9519\u5c31\u662f\u7aef\u53e3\u88ab\u5360\u7528\u3002\u5982\u679c\u4f60\u770b\u5230\u8fd9\u4e2a\u5f02\u5e38&#xff1a;<\/p>\n<p>java.net.BindException: Address already in use: JVM_Bind <\/p>\n<p>\u4e0d\u7528\u614c&#xff0c;\u5c31\u662f\u4f60\u8981\u7ed1\u5b9a\u7684\u7aef\u53e3\u5df2\u7ecf\u88ab\u522b\u7684\u8fdb\u7a0b\u5360\u4e86\u3002\u4e00\u4e2a\u7aef\u53e3\u540c\u4e00\u65f6\u95f4\u53ea\u80fd\u88ab\u4e00\u4e2a\u8fdb\u7a0b\u7ed1\u5b9a\u3002<\/p>\n<h4 id=\"%E6%80%8E%E4%B9%88%E6%9F%A5%E5%93%AA%E4%B8%AA%E8%BF%9B%E7%A8%8B%E5%8D%A0%E4%BA%86%E7%AB%AF%E5%8F%A3%EF%BC%9F\">\u600e\u4e48\u67e5\u54ea\u4e2a\u8fdb\u7a0b\u5360\u4e86\u7aef\u53e3&#xff1f;<\/h4>\n<p>Windows\u4e0b\u6253\u5f00cmd&#xff0c;\u8f93\u5165\u547d\u4ee4&#xff1a;<\/p>\n<p>netstat -ano | findstr 8888 <\/p>\n<p>\u4f1a\u8f93\u51fa\u7c7b\u4f3c\u8fd9\u6837\u7684\u7ed3\u679c&#xff0c;\u6700\u540e\u4e00\u5217\u5c31\u662f\u8fdb\u7a0bPID&#xff1a;<\/p>\n<p>TCP 0.0.0.0:8888 0.0.0.0:0 LISTENING 4588 <\/p>\n<p>\u7136\u540e\u6253\u5f00\u4efb\u52a1\u7ba1\u7406\u5668&#xff0c;\u5728\u8be6\u7ec6\u4fe1\u606f\u91cc\u6309PID\u6392\u5e8f&#xff0c;\u627e\u5230PID\u4e3a4588\u7684\u8fdb\u7a0b&#xff0c;\u5c31\u77e5\u9053\u662f\u8c01\u5360\u4e86\u7aef\u53e3\u3002<\/p>\n<h4 id=\"%E6%80%8E%E4%B9%88%E8%A7%A3%E5%86%B3%EF%BC%9F\">\u600e\u4e48\u89e3\u51b3&#xff1f;<\/h4>\n<li>\n<p>\u5360\u7aef\u53e3\u7684\u8fdb\u7a0b\u4e0d\u9700\u8981\u4e86&#xff1a;\u76f4\u63a5\u7ed3\u675f\u90a3\u4e2a\u8fdb\u7a0b&#xff0c;\u518d\u542f\u52a8\u4f60\u7684\u7a0b\u5e8f\u5c31\u884c<\/p>\n<\/li>\n<li>\n<p>\u5360\u7aef\u53e3\u7684\u8fdb\u7a0b\u8fd8\u8981\u7528&#xff1a;\u6539\u4f60\u81ea\u5df1\u7a0b\u5e8f\u7684\u7aef\u53e3\u53f7&#xff0c;\u6362\u4e00\u4e2a\u6ca1\u88ab\u5360\u7528\u7684\u7aef\u53e3<\/p>\n<\/li>\n<p>\u5c0f\u6280\u5de7&#xff1a;\u4e00\u822c\u5f00\u53d1\u8c03\u8bd5\u75288080\u30019090\u30018888\u8fd9\u4e9b\u7aef\u53e3\u6bd4\u8f83\u591a&#xff0c;\u88ab\u5360\u7528\u4e86\u5c31\u6362\u4e2a\u6570\u5b57\u8bd5\u8bd5&#xff0c;\u6bd4\u59829091\u30019092\u3002<\/p>\n<p>\u6700\u540e&#xff0c;\u6211\u4eec\u6765\u68b3\u7406\u4e00\u4e0b\u8fd9\u7bc7\u6587\u7ae0\u7684\u6838\u5fc3\u77e5\u8bc6\u70b9&#xff0c;\u5e2e\u5927\u5bb6\u5de9\u56fa\u8bb0\u5fc6&#xff1a;<\/p>\n<li>\n<p>\u7f51\u7edc\u7f16\u7a0b\u57fa\u7840&#xff1a;\u7f51\u7edc\u7f16\u7a0b\u5c31\u662f\u4e0d\u540c\u4e3b\u673a\u7684\u8fdb\u7a0b\u901a\u8fc7\u7f51\u7edc\u4f20\u8f93\u6570\u636e\u3002\u6838\u5fc3\u6982\u5ff5\u6709\u53d1\u9001\u7aef\/\u63a5\u6536\u7aef\u3001\u8bf7\u6c42\/\u54cd\u5e94\u3001\u5ba2\u6237\u7aef\/\u670d\u52a1\u7aef\u3002<\/p>\n<\/li>\n<li>\n<p>Socket\u5957\u63a5\u5b57&#xff1a;\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u7684\u7f51\u7edc\u901a\u4fe1API&#xff0c;\u4e3b\u8981\u5206\u4e24\u7c7b&#xff1a;<\/p>\n<li>\n<p>UDP\u6570\u636e\u62a5\u5957\u63a5\u5b57&#xff1a;\u65e0\u8fde\u63a5\u3001\u4e0d\u53ef\u9760\u3001\u9762\u5411\u6570\u636e\u62a5\u3001\u901f\u5ea6\u5feb<\/p>\n<\/li>\n<li>\n<p>TCP\u6d41\u5957\u63a5\u5b57&#xff1a;\u6709\u8fde\u63a5\u3001\u53ef\u9760\u4f20\u8f93\u3001\u9762\u5411\u5b57\u8282\u6d41\u3001\u7a33\u5b9a\u53ef\u9760<\/p>\n<\/li>\n<\/li>\n<li>\n<p>UDP\u7f16\u7a0b&#xff1a;\u6838\u5fc3\u7c7b\u662fDatagramSocket\u548cDatagramPacket\u3002\u670d\u52a1\u7aef\u5faa\u73afreceive\u63a5\u6536\u8bf7\u6c42\u2192process\u5904\u7406\u2192send\u8fd4\u56de\u54cd\u5e94\u3002<\/p>\n<\/li>\n<li>\n<p>TCP\u7f16\u7a0b&#xff1a;\u6838\u5fc3\u7c7b\u662fServerSocket\u548cSocket\u3002\u670d\u52a1\u7aefaccept\u7b49\u5f85\u8fde\u63a5\u2192\u5efa\u7acb\u8fde\u63a5\u540e\u901a\u8fc7InputStream\/OutputStream\u8bfb\u5199\u6570\u636e&#xff0c;\u548c\u64cd\u4f5c\u6587\u4ef6\u6d41\u4e00\u6a21\u4e00\u6837\u3002<\/p>\n<\/li>\n<li>\n<p>\u670d\u52a1\u5668\u4f18\u5316&#xff1a;\u5355\u7ebf\u7a0b\u670d\u52a1\u5668\u4e00\u6b21\u53ea\u80fd\u5904\u7406\u4e00\u4e2a\u5ba2\u6237\u7aef&#xff0c;\u7528\u591a\u7ebf\u7a0b\u6216\u7ebf\u7a0b\u6c60\u53ef\u4ee5\u5b9e\u73b0\u5e76\u53d1\u5904\u7406\u3002<\/p>\n<\/li>\n<li>\n<p>\u957f\u77ed\u8fde\u63a5&#xff1a;\u77ed\u8fde\u63a5\u4e00\u6b21\u8bf7\u6c42\u4e00\u5173\u8fde\u63a5&#xff0c;\u957f\u8fde\u63a5\u4fdd\u6301\u8fde\u63a5\u591a\u6b21\u901a\u4fe1\u3002\u8bf7\u6c42\u9891\u7387\u4f4e\u7528\u77ed\u8fde\u63a5&#xff0c;\u901a\u4fe1\u9891\u7e41\u7528\u957f\u8fde\u63a5\u3002<\/p>\n<\/li>\n<li>\n<p>\u5e38\u89c1\u5751&#xff1a;\u7aef\u53e3\u88ab\u5360\u7528\u62a5BindException&#xff0c;\u7528netstat\u67e5PID\u7ed3\u675f\u8fdb\u7a0b\u6216\u6362\u7aef\u53e3\u3002<\/p>\n<\/li>\n<hr \/>\n<p>\u7f51\u7edc\u7f16\u7a0b\u662fJava\u540e\u7aef\u7684\u57fa\u7840\u4e2d\u7684\u57fa\u7840&#xff0c;\u4e0d\u7ba1\u662f\u505aWeb\u5f00\u53d1\u3001\u5fae\u670d\u52a1\u8fd8\u662f\u5206\u5e03\u5f0f\u7cfb\u7edf&#xff0c;\u5e95\u5c42\u90fd\u79bb\u4e0d\u5f00Socket\u3002\u5efa\u8bae\u5927\u5bb6\u628a\u6587\u4e2d\u7684\u4ee3\u7801\u90fd\u4eb2\u624b\u6572\u4e00\u904d\u8dd1\u8d77\u6765&#xff0c;\u771f\u6b63\u7406\u89e3\u6570\u636e\u662f\u600e\u4e48\u6536\u53d1\u7684\u3002<\/p>\n<p>\u5982\u679c\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u5e2e\u52a9&#xff0c;\u6b22\u8fce\u70b9\u8d5e\u6536\u85cf\u5173\u6ce8&#xff0c;\u540e\u7eed\u8fd8\u4f1a\u66f4\u65b0\u66f4\u591a\u7f51\u7edc\u7f16\u7a0b\u8fdb\u9636\u5185\u5bb9&#xff01;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u76ee\u5f55 \u524d\u8a00&#xff1a;\u4e3a\u4ec0\u4e48\u8981\u5b66\u7f51\u7edc\u7f16\u7a0b&#xff1f;<br \/>\n\u4e00\u3001\u7f51\u7edc\u7f16\u7a0b\u57fa\u7840\u6982\u5ff5<br \/>\n1.1 \u4ec0\u4e48\u662f\u7f51\u7edc\u7f16\u7a0b<br \/>\n1.2 \u53d1\u9001\u7aef\u4e0e\u63a5\u6536\u7aef<br \/>\n1.4 \u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u7aef<br \/>\n\u4e8c\u3001Socket\u5957\u63a5\u5b57\u662f\u4ec0\u4e48&#xff1f;<br \/>\n2.1 \u5957\u63a5\u5b57\u7684\u6982\u5ff5<br \/>\n2.3 TCP vs UDP \u6838\u5fc3\u5bf9\u6bd4<br \/>\n\u4e09\u3001UDP\u6570\u636e\u62a5\u5957\u63a5\u5b57\u7f16\u7a0b<br \/>\n3.1 UDP\u901a\u4fe1\u6a21\u578b<br \/>\n3.2 \u6838\u5fc3API\u4ecb\u7ecd<br \/>\nDatagramSocket<br \/>\nDatagramPacket<br \/>\n3.3 \u5b9e\u6218\u4e00&#xff1a;Echo\u56de\u663e\u670d\u52a1\u5668<br \/>\nU<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[151,78,191],"topic":[],"class_list":["post-81375","post","type-post","status-publish","format-standard","hentry","category-server","tag-http","tag-78","tag-191"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Socket\u7f51\u7edc\u7f16\u7a0b\u5165\u95e8\uff1aUDP+TCP\u53cc\u6848\u4f8b\u624b\u628a\u624b\u6559\u4f60\u5199\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/81375.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Socket\u7f51\u7edc\u7f16\u7a0b\u5165\u95e8\uff1aUDP+TCP\u53cc\u6848\u4f8b\u624b\u628a\u624b\u6559\u4f60\u5199\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u76ee\u5f55 \u524d\u8a00&#xff1a;\u4e3a\u4ec0\u4e48\u8981\u5b66\u7f51\u7edc\u7f16\u7a0b&#xff1f; \u4e00\u3001\u7f51\u7edc\u7f16\u7a0b\u57fa\u7840\u6982\u5ff5 1.1 \u4ec0\u4e48\u662f\u7f51\u7edc\u7f16\u7a0b 1.2 \u53d1\u9001\u7aef\u4e0e\u63a5\u6536\u7aef 1.4 \u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u7aef \u4e8c\u3001Socket\u5957\u63a5\u5b57\u662f\u4ec0\u4e48&#xff1f; 2.1 \u5957\u63a5\u5b57\u7684\u6982\u5ff5 2.3 TCP vs UDP \u6838\u5fc3\u5bf9\u6bd4 \u4e09\u3001UDP\u6570\u636e\u62a5\u5957\u63a5\u5b57\u7f16\u7a0b 3.1 UDP\u901a\u4fe1\u6a21\u578b 3.2 \u6838\u5fc3API\u4ecb\u7ecd DatagramSocket DatagramPacket 3.3 \u5b9e\u6218\u4e00&#xff1a;Echo\u56de\u663e\u670d\u52a1\u5668 U\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/81375.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-07-24T17:21:47+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/81375.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/81375.html\",\"name\":\"Socket\u7f51\u7edc\u7f16\u7a0b\u5165\u95e8\uff1aUDP+TCP\u53cc\u6848\u4f8b\u624b\u628a\u624b\u6559\u4f60\u5199\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-07-24T17:21:47+00:00\",\"dateModified\":\"2026-07-24T17:21:47+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/81375.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/81375.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/81375.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Socket\u7f51\u7edc\u7f16\u7a0b\u5165\u95e8\uff1aUDP+TCP\u53cc\u6848\u4f8b\u624b\u628a\u624b\u6559\u4f60\u5199\u670d\u52a1\u5668\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Socket\u7f51\u7edc\u7f16\u7a0b\u5165\u95e8\uff1aUDP+TCP\u53cc\u6848\u4f8b\u624b\u628a\u624b\u6559\u4f60\u5199\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/81375.html","og_locale":"zh_CN","og_type":"article","og_title":"Socket\u7f51\u7edc\u7f16\u7a0b\u5165\u95e8\uff1aUDP+TCP\u53cc\u6848\u4f8b\u624b\u628a\u624b\u6559\u4f60\u5199\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u76ee\u5f55 \u524d\u8a00&#xff1a;\u4e3a\u4ec0\u4e48\u8981\u5b66\u7f51\u7edc\u7f16\u7a0b&#xff1f; \u4e00\u3001\u7f51\u7edc\u7f16\u7a0b\u57fa\u7840\u6982\u5ff5 1.1 \u4ec0\u4e48\u662f\u7f51\u7edc\u7f16\u7a0b 1.2 \u53d1\u9001\u7aef\u4e0e\u63a5\u6536\u7aef 1.4 \u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u7aef \u4e8c\u3001Socket\u5957\u63a5\u5b57\u662f\u4ec0\u4e48&#xff1f; 2.1 \u5957\u63a5\u5b57\u7684\u6982\u5ff5 2.3 TCP vs UDP \u6838\u5fc3\u5bf9\u6bd4 \u4e09\u3001UDP\u6570\u636e\u62a5\u5957\u63a5\u5b57\u7f16\u7a0b 3.1 UDP\u901a\u4fe1\u6a21\u578b 3.2 \u6838\u5fc3API\u4ecb\u7ecd DatagramSocket DatagramPacket 3.3 \u5b9e\u6218\u4e00&#xff1a;Echo\u56de\u663e\u670d\u52a1\u5668 U","og_url":"https:\/\/www.wsisp.com\/helps\/81375.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-07-24T17:21:47+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"7 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/81375.html","url":"https:\/\/www.wsisp.com\/helps\/81375.html","name":"Socket\u7f51\u7edc\u7f16\u7a0b\u5165\u95e8\uff1aUDP+TCP\u53cc\u6848\u4f8b\u624b\u628a\u624b\u6559\u4f60\u5199\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-07-24T17:21:47+00:00","dateModified":"2026-07-24T17:21:47+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/81375.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/81375.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/81375.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Socket\u7f51\u7edc\u7f16\u7a0b\u5165\u95e8\uff1aUDP+TCP\u53cc\u6848\u4f8b\u624b\u628a\u624b\u6559\u4f60\u5199\u670d\u52a1\u5668"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/81375","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=81375"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/81375\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=81375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=81375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=81375"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=81375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}