{"id":9772,"date":"2025-04-18T17:37:23","date_gmt":"2025-04-18T09:37:23","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/9772.html"},"modified":"2025-04-18T17:37:23","modified_gmt":"2025-04-18T09:37:23","slug":"%e3%80%90%e7%bd%91%e7%bb%9c%e5%8e%9f%e7%90%86%e3%80%91%e4%bd%bf%e7%94%a8java%e5%9f%ba%e4%ba%8etcp%e6%90%ad%e5%bb%ba%e7%ae%80%e5%8d%95%e5%ae%a2%e6%88%b7%e7%ab%af%e4%b8%8e%e6%9c%8d%e5%8a%a1%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/9772.html","title":{"rendered":"\u3010\u7f51\u7edc\u539f\u7406\u3011\u4f7f\u7528Java\u57fa\u4e8eTCP\u642d\u5efa\u7b80\u5355\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u901a\u4fe1"},"content":{"rendered":"<\/p>\n<h4>\u76ee\u5f55<\/h4>\n<ul>\n<li>&#x1f384;API\u4ecb\u7ecd<\/li>\n<li>\n<ul>\n<li>&#x1f338;ServerSocket API<\/li>\n<li>&#x1f338;Socket API<\/li>\n<\/ul>\n<\/li>\n<li>&#x1f340;TCP\u4e2d\u7684\u957f\u77ed\u8fde\u63a5<\/li>\n<li>&#x1f333;\u5efa\u7acbTCP\u56de\u663e\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668<\/li>\n<li>\n<ul>\n<li>&#x1f338;TCP\u642d\u5efa\u670d\u52a1\u5668<\/li>\n<li>&#x1f338;TCP\u642d\u5efa\u5ba2\u6237\u7aef<\/li>\n<\/ul>\n<\/li>\n<li>\u2b55\u603b\u7ed3<\/li>\n<\/ul>\n<p> TCP\u670d\u52a1\u5668\u4e0e\u5ba2\u6237\u7aef\u7684\u642d\u5efa\u9700\u8981\u501f\u52a9\u4ee5\u4e0bAPI <\/p>\n<h2>&#x1f384;API\u4ecb\u7ecd<\/h2>\n<h3>&#x1f338;ServerSocket API<\/h3>\n<p>ServerSocket \u662f\u521b\u5efaTCP\u670d\u52a1\u7aefSocket\u7684API\u3002<\/p>\n<p>ServerSocket \u6784\u9020\u65b9\u6cd5&#xff1a;<\/p>\n<table>\n<tr>\u65b9\u6cd5\u7b7e\u540d\u65b9\u6cd5\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>ServerSocket(int port)<\/td>\n<td>\u521b\u5efa\u4e00\u4e2a\u670d\u52a1\u7aef\u6d41\u5957\u63a5\u5b57Socket&#xff0c;\u5e76\u7ed1\u5b9a\u5230\u6307\u5b9a\u7aef\u53e3<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>ServerSocket \u65b9\u6cd5&#xff1a;<\/p>\n<table>\n<tr>\u65b9\u6cd5\u7b7e\u540d\u65b9\u6cd5\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>Socket accept()<\/td>\n<td>\u5f00\u59cb\u76d1\u542c\u6307\u5b9a\u7aef\u53e3&#xff08;\u521b\u5efa\u65f6\u7ed1\u5b9a\u7684\u7aef\u53e3&#xff09;&#xff0c;\u6709\u5ba2\u6237\u7aef\u8fde\u63a5\u540e&#xff0c;\u8fd4\u56de\u4e00\u4e2a\u670d\u52a1\u7aefSocket\u5bf9\u8c61&#xff0c;\u5e76\u57fa\u4e8e\u8be5Socket\u5efa\u7acb\u4e0e\u5ba2\u6237\u7aef\u7684\u8fde\u63a5&#xff0c;\u5426\u5219\u963b\u585e\u7b49\u5f85<\/td>\n<\/tr>\n<tr>\n<td>void close()<\/td>\n<td>\u5173\u95ed\u6b64\u5957\u63a5\u5b57<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>&#x1f338;Socket API<\/h3>\n<p>Socket \u662f\u5ba2\u6237\u7aefSocket&#xff0c;\u6216\u670d\u52a1\u7aef\u4e2d\u63a5\u6536\u5230\u5ba2\u6237\u7aef\u5efa\u7acb\u8fde\u63a5&#xff08;accept\u65b9\u6cd5&#xff09;\u7684\u8bf7\u6c42\u540e&#xff0c;\u8fd4\u56de\u7684\u670d\u52a1\u7aefSocket\u3002<\/p>\n<p>\u4e0d\u7ba1\u662f\u5ba2\u6237\u7aef\u8fd8\u662f\u670d\u52a1\u7aefSocket&#xff0c;\u90fd\u662f\u53cc\u65b9\u5efa\u7acb\u8fde\u63a5\u4ee5\u540e&#xff0c;\u4fdd\u5b58\u7684\u5bf9\u7aef\u4fe1\u606f&#xff0c;\u53ca\u7528\u6765\u4e0e\u5bf9\u65b9\u6536\u53d1\u6570\u636e\u7684\u3002<\/p>\n<p>Socket \u6784\u9020\u65b9\u6cd5&#xff1a;<\/p>\n<table>\n<tr>\u65b9\u6cd5\u7b7e\u540d\u65b9\u6cd5\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>Socket(String host, intport)<\/td>\n<td>\u521b\u5efa\u4e00\u4e2a\u5ba2\u6237\u7aef\u6d41\u5957\u63a5\u5b57Socket&#xff0c;\u5e76\u4e0e\u5bf9\u5e94IP\u7684\u4e3b\u673a\u4e0a&#xff0c;\u5bf9\u5e94\u7aef\u53e3\u7684\u8fdb\u7a0b\u5efa\u7acb\u8fde\u63a5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Socket \u65b9\u6cd5&#xff1a;<\/p>\n<table>\n<tr>\u65b9\u6cd5\u7b7e\u540d\u65b9\u6cd5\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>InetAddress getInetAddress()<\/td>\n<td>\u8fd4\u56de\u5957\u63a5\u5b57\u6240\u8fde\u63a5\u7684\u5730\u5740<\/td>\n<\/tr>\n<tr>\n<td>InputStream getInputStream()<\/td>\n<td>\u8fd4\u56de\u6b64\u5957\u63a5\u5b57\u7684\u8f93\u5165\u6d41<\/td>\n<\/tr>\n<tr>\n<td>OutputStream getOutputStream()<\/td>\n<td>\u8fd4\u56de\u6b64\u5957\u63a5\u5b57\u7684\u8f93\u51fa\u6d41<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>&#x1f340;TCP\u4e2d\u7684\u957f\u77ed\u8fde\u63a5<\/h2>\n<p>\u535a\u4e3b\u5728\u524d\u9762\u7684\u535a\u6587\u91cc\u9762\u8bf4\u5230&#xff0c;TCP\u662f\u9762\u5411\u8fde\u63a5\u7684\u901a\u4fe1\u65b9\u5f0f&#xff0c;TCP\u53d1\u9001\u6570\u636e\u65f6&#xff0c;\u9700\u8981\u5148\u5efa\u7acb\u8fde\u63a5&#xff0c;\u800c\u8fd9\u4e2a\u8fde\u63a5\u53c8\u5206\u4e3a\u957f\u77ed\u8fde\u63a5&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u77ed\u8fde\u63a5&#xff1a;\u6bcf\u6b21\u63a5\u6536\u5230\u6570\u636e\u5e76\u8fd4\u56de\u54cd\u5e94\u540e&#xff0c;\u90fd\u5173\u95ed\u8fde\u63a5&#xff0c;\u5373\u662f\u77ed\u8fde\u63a5\u3002\u4e5f\u5c31\u662f\u8bf4&#xff0c;\u77ed\u8fde\u63a5\u53ea\u80fd\u4e00\u6b21\u6536\u53d1\u6570\u636e\u3002<\/p>\n<\/li>\n<li>\n<p>\u957f\u8fde\u63a5&#xff1a;\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;\u5373\u662f\u957f\u8fde\u63a5\u3002\u4e5f\u5c31\u662f\u8bf4&#xff0c;\u957f\u8fde\u63a5\u53ef\u4ee5\u591a\u6b21\u6536\u53d1\u6570\u636e<\/p>\n<\/li>\n<\/ul>\n<p>\u5bf9\u6bd4\u4ee5\u4e0a\u957f\u77ed\u8fde\u63a5&#xff0c;\u4e24\u8005\u533a\u522b\u5982\u4e0b&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5efa\u7acb\u8fde\u63a5\u3001\u5173\u95ed\u8fde\u63a5\u7684\u8017\u65f6&#xff1a;\u77ed\u8fde\u63a5\u6bcf\u6b21\u8bf7\u6c42\u3001\u54cd\u5e94\u90fd\u9700\u8981\u5efa\u7acb\u8fde\u63a5&#xff0c;\u5173\u95ed\u8fde\u63a5&#xff1b;\u800c\u957f\u8fde\u63a5\u53ea\u9700\u8981\u7b2c\u4e00\u6b21\u5efa\u7acb\u8fde\u63a5&#xff0c;\u4e4b\u540e\u7684\u8bf7\u6c42\u3001\u54cd\u5e94\u90fd\u53ef\u4ee5\u76f4\u63a5\u4f20\u8f93\u3002\u76f8\u5bf9\u6765\u8bf4\u5efa\u7acb\u8fde\u63a5&#xff0c;\u5173\u95ed\u8fde\u63a5\u4e5f\u662f\u8981\u8017\u65f6\u7684&#xff0c;\u957f\u8fde\u63a5\u6548\u7387\u66f4\u9ad8\u3002<\/p>\n<\/li>\n<li>\n<p>\u4e3b\u52a8\u53d1\u9001\u8bf7\u6c42\u4e0d\u540c&#xff1a;\u77ed\u8fde\u63a5\u4e00\u822c\u662f\u5ba2\u6237\u7aef\u4e3b\u52a8\u5411\u670d\u52a1\u7aef\u53d1\u9001\u8bf7\u6c42&#xff1b;\u800c\u957f\u8fde\u63a5\u53ef\u4ee5\u662f\u5ba2\u6237\u7aef\u4e3b\u52a8\u53d1\u9001\u8bf7\u6c42&#xff0c;\u4e5f\u53ef\u4ee5\u662f\u670d\u52a1\u7aef\u4e3b\u52a8\u53d1\u3002<\/p>\n<\/li>\n<li>\n<p>\u4e24\u8005\u7684\u4f7f\u7528\u573a\u666f\u6709\u4e0d\u540c&#xff1a;\u77ed\u8fde\u63a5\u9002\u7528\u4e8e\u5ba2\u6237\u7aef\u8bf7\u6c42\u9891\u7387\u4e0d\u9ad8\u7684\u573a\u666f&#xff0c;\u5982\u6d4f\u89c8\u7f51\u9875\u7b49\u3002\u957f\u8fde\u63a5\u9002\u7528\u4e8e\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u7aef\u901a\u4fe1\u9891\u7e41\u7684\u573a\u666f&#xff0c;\u5982\u804a\u5929\u5ba4&#xff0c;\u5b9e\u65f6\u6e38\u620f\u7b49<\/p>\n<\/li>\n<\/ul>\n<p>\u62d3\u5c55&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u57fa\u4e8eBIO&#xff08;\u540c\u6b65\u963b\u585eIO&#xff09;\u7684\u957f\u8fde\u63a5\u4f1a\u4e00\u76f4\u5360\u7528\u7cfb\u7edf\u8d44\u6e90\u3002\u5bf9\u4e8e\u5e76\u53d1\u8981\u6c42\u5f88\u9ad8\u7684\u670d\u52a1\u7aef\u7cfb\u7edf\u6765\u8bf4&#xff0c;\u8fd9\u6837\u7684\u6d88\u8017\u662f\u4e0d\u80fd\u627f\u53d7\u7684\u3002<\/p>\n<\/li>\n<li>\n<p>\u7531\u4e8e\u6bcf\u4e2a\u8fde\u63a5\u90fd\u9700\u8981\u4e0d\u505c\u7684\u963b\u585e\u7b49\u5f85\u63a5\u6536\u6570\u636e&#xff0c;\u6240\u4ee5\u6bcf\u4e2a\u8fde\u63a5\u90fd\u4f1a\u5728\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u8fd0\u884c\u3002\u4e00\u6b21\u963b\u585e\u7b49\u5f85\u5bf9\u5e94\u7740\u4e00\u6b21\u8bf7\u6c42\u3001\u54cd\u5e94&#xff0c;\u4e0d\u505c\u5904\u7406\u4e5f\u5c31\u662f\u957f\u8fde\u63a5\u7684\u7279\u6027&#xff1a;\u4e00\u76f4\u4e0d\u5173\u95ed\u8fde\u63a5&#xff0c;\u4e0d\u505c\u7684\u5904\u7406\u8bf7\u6c42<\/p>\n<\/li>\n<li>\n<p>\u5b9e\u9645\u5e94\u7528\u65f6&#xff0c;\u670d\u52a1\u7aef\u4e00\u822c\u662f\u57fa\u4e8eNIO&#xff08;\u5373\u540c\u6b65\u975e\u963b\u585eIO&#xff09;\u6765\u5b9e\u73b0\u957f\u8fde\u63a5&#xff0c;\u6027\u80fd\u53ef\u4ee5\u6781\u5927\u7684\u63d0\u5347\u3002<\/p>\n<\/li>\n<\/ul>\n<h2>&#x1f333;\u5efa\u7acbTCP\u56de\u663e\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668<\/h2>\n<h3>&#x1f338;TCP\u642d\u5efa\u670d\u52a1\u5668<\/h3>\n<p>\u6211\u4eec\u5206\u4e3a\u4ee5\u4e0b\u51e0\u6b65\u6765\u5b9e\u73b0&#xff1a;<\/p>\n<li>\u521b\u5efaTcpEchoServer\u7c7b\u6765\u8868\u793a\u6211\u4eec\u7684\u670d\u52a1\u5668&#xff0c;\u5e76\u521b\u5efaServerSocket\u5bf9\u8c61&#xff0c;\u521d\u59cb\u503c\u4e3anull<\/li>\n<li>\u5728TcpEchoServer\u7684\u6784\u9020\u65b9\u6cd5\u91cc\u8fdb\u884cServerSocket\u5bf9\u8c61\u7684\u5b9e\u4f8b\u5316<\/li>\n<li>\u7528\u4e00\u4e2astart()\u65b9\u6cd5\u8868\u793a\u542f\u52a8\u7a0b\u5e8f<\/li>\n<li>\u5728\u8be5\u65b9\u6cd5\u5185\u6211\u4eec\u9996\u5148\u8981\u4f7f\u7528accept()\u8fdb\u884c\u8fde\u63a5&#xff0c;\u5e76\u7528Socket\u5bf9\u8c61\u8fdb\u884c\u63a5\u6536<\/li>\n<li>\u6211\u4eec\u518d\u7528\u4e00\u4e2aprocessConnection(Socket clientSocket)\u65b9\u6cd5\u5904\u7406\u6211\u4eec\u7684\u8fde\u63a5<\/li>\n<p>\u7531\u4e8e\u6211\u4eec\u7684TCP\u4f20\u8f93\u662f\u4ee5\u6d41\u7684\u5f62\u5f0f\u4f20\u64ad\u7684&#xff0c;\u6240\u4ee5\u6211\u4eec\u8fd9\u91cc\u7528\u5230\u4e86\u8bfb\u5199\u6570\u636e\u6d41\u7684\u65b9\u6cd5\u6765\u8fdb\u884c\u4e66\u5199&#xff0c;\u4e0d\u4f1a\u8fd9\u4e00\u90e8\u5206\u7684\u5c0f\u4f19\u4f34&#xff0c;\u53ef\u4ee5\u53bb\u770b\u770b\u535a\u4e3b\u6240\u5199\u3010Java EE\u3011\u6587\u4ef6\u5185\u5bb9\u7684\u8bfb\u5199\u2e3a\u6570\u636e\u6d41\u8fdb\u884c\u67e5\u770b\u5b66\u4e60<\/p>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u4e66\u5199\u8fd9\u4e2aprocessConnection(Socket clientSocket)\u65b9\u6cd5<\/p>\n<li>\u8bfb\u53d6\u8bf7\u6c42&#xff0c;\u6784\u9020\u8f93\u5165\u6d41\u7684Scanner&#xff0c;\u5e76\u5224\u65ad\u540e\u9762\u5982\u679c\u6ca1\u6709\u6570\u636e\u5c31\u5173\u95ed\u8fde\u63a5<\/li>\n<li>\u7136\u540e\u6211\u4eec\u5c06\u8bfb\u53d6\u7684\u6570\u636e\u4ea4\u7ed9\u6211\u4eec\u7684 response()\u6784\u9020\u54cd\u5e94<\/li>\n<li>\u54cd\u5e94\u540e\u7684\u6570\u636e\u5199\u5165\u8be5\u5957\u63a5\u5b57\u7684\u8f93\u51fa\u6d41\u4e2d&#xff0c;\u6700\u540eflush()&#xff0c;\u8fdb\u884c\u5237\u65b0&#xff0c;\u786e\u4fdd\u5199\u5165<\/li>\n<p>\u4e3a\u4e86\u91ca\u653e\u8d44\u6e90&#xff0c;\u6211\u4eec\u6bcf\u4e00\u6b21\u4ea4\u4e92\u5b8c\u6bd5\u90fd\u9700\u8981\u5bf9\u6211\u4eec\u7684\u5957\u63a5\u5b57\u8fdb\u884c\u5173\u95ed&#xff0c;\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528fially\u6765\u8fdb\u884c\u5904\u7406<\/p>\n<p>\u4ee3\u7801\u5982\u4e0b&#xff1a;<\/p>\n<p><span class=\"token function\">import<\/span> java.io.IOException<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">import<\/span> java.io.InputStream<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">import<\/span> java.io.OutputStream<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">import<\/span> java.io.PrintWriter<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">import<\/span> java.net.ServerSocket<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">import<\/span> java.net.Socket<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">import<\/span> java.util.Scanner<span class=\"token punctuation\">;<\/span><\/p>\n<p>public class TcpEchoServer <span class=\"token punctuation\">{<\/span><br \/>\n    private ServerSocket serverSocket <span class=\"token operator\">&#061;<\/span> null<span class=\"token punctuation\">;<\/span><\/p>\n<p>    public TcpEchoServer<span class=\"token punctuation\">(<\/span>int port<span class=\"token punctuation\">)<\/span> throws IOException <span class=\"token punctuation\">{<\/span><br \/>\n        serverSocket <span class=\"token operator\">&#061;<\/span> new ServerSocket<span class=\"token punctuation\">(<\/span>port<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    public void start<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> throws IOException <span class=\"token punctuation\">{<\/span><br \/>\n        System.out.println<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u542f\u52a8\u670d\u52a1\u5668&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        Socket socket <span class=\"token operator\">&#061;<\/span> serverSocket.accept<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>        processConnection<span class=\"token punctuation\">(<\/span>socket<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    \/\/ \u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u6765\u5904\u7406\u4e00\u4e2a\u8fde\u63a5.<br \/>\n    \/\/ \u8fd9\u4e00\u4e2a\u8fde\u63a5\u5bf9\u5e94\u5230\u4e00\u4e2a\u5ba2\u6237\u7aef. \u4f46\u662f\u8fd9\u91cc\u53ef\u80fd\u4f1a\u6d89\u53ca\u5230\u591a\u6b21\u4ea4\u4e92.<br \/>\n    private void processConnection<span class=\"token punctuation\">(<\/span>Socket clientSocket<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        System.out.printf<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;[%s:%d] \u5ba2\u6237\u7aef\u4e0a\u7ebf!<span class=\"token entity\" title=\"\\\\n\">\\\\n<\/span>&#034;<\/span>, clientSocket.getInetAddress<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>.toString<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>, clientSocket.getPort<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">))<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        \/\/ \u57fa\u4e8e\u4e0a\u8ff0 socket \u5bf9\u8c61\u548c\u5ba2\u6237\u7aef\u8fdb\u884c\u901a\u4fe1<br \/>\n        try <span class=\"token punctuation\">(<\/span>InputStream inputStream <span class=\"token operator\">&#061;<\/span> clientSocket.getInputStream<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n             OutputStream outputStream <span class=\"token operator\">&#061;<\/span> clientSocket.getOutputStream<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">))<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            \/\/ \u7531\u4e8e\u8981\u5904\u7406\u591a\u4e2a\u8bf7\u6c42\u548c\u54cd\u5e94, \u4e5f\u662f\u4f7f\u7528\u5faa\u73af\u6765\u8fdb\u884c.<br \/>\n            <span class=\"token keyword\">while<\/span> <span class=\"token punctuation\">(<\/span>true<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                \/\/ <span class=\"token number\">1<\/span>. \u8bfb\u53d6\u8bf7\u6c42<br \/>\n                Scanner scanner <span class=\"token operator\">&#061;<\/span> new Scanner<span class=\"token punctuation\">(<\/span>inputStream<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n                <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token operator\">!<\/span>scanner.hasNext<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">))<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                    \/\/ \u6ca1\u6709\u4e0b\u4e2a\u6570\u636e, \u8bf4\u660e\u8bfb\u5b8c\u4e86. <span class=\"token punctuation\">(<\/span>\u5ba2\u6237\u7aef\u5173\u95ed\u4e86\u8fde\u63a5<span class=\"token punctuation\">)<\/span><br \/>\n                    System.out.printf<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;[%s:%d] \u5ba2\u6237\u7aef\u4e0b\u7ebf! <span class=\"token entity\" title=\"\\\\n\">\\\\n<\/span>&#034;<\/span>, clientSocket.getInetAddress<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>.toString<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>, clientSocket.getPort<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">))<\/span><span class=\"token punctuation\">;<\/span><br \/>\n                    <span class=\"token builtin class-name\">break<\/span><span class=\"token punctuation\">;<\/span><br \/>\n                <span class=\"token punctuation\">}<\/span><br \/>\n                \/\/ \u6ce8\u610f<span class=\"token operator\">!<\/span><span class=\"token operator\">!<\/span> \u6b64\u5904\u4f7f\u7528 next \u662f\u4e00\u76f4\u8bfb\u53d6\u5230\u6362\u884c\u7b26\/\u7a7a\u683c\/\u5176\u4ed6\u7a7a\u767d\u7b26\u7ed3\u675f, \u4f46\u662f\u6700\u7ec8\u8fd4\u56de\u7ed3\u679c\u91cc\u4e0d\u5305\u542b\u4e0a\u8ff0 \u7a7a\u767d\u7b26 <span class=\"token builtin class-name\">.<\/span><br \/>\n                String request <span class=\"token operator\">&#061;<\/span> scanner.next<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n                \/\/ <span class=\"token number\">2<\/span>. \u6839\u636e\u8bf7\u6c42\u6784\u9020\u54cd\u5e94<br \/>\n                String response <span class=\"token operator\">&#061;<\/span> process<span class=\"token punctuation\">(<\/span>request<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n                \/\/ <span class=\"token number\">3<\/span>. \u8fd4\u56de\u54cd\u5e94\u7ed3\u679c.<br \/>\n                \/\/    OutputStream \u6ca1\u6709 <span class=\"token function\">write<\/span> String \u8fd9\u6837\u7684\u529f\u80fd. \u53ef\u4ee5\u628a String \u91cc\u7684\u5b57\u8282\u6570\u7ec4\u62ff\u51fa\u6765, \u8fdb\u884c\u5199\u5165<span class=\"token punctuation\">;<\/span><br \/>\n                \/\/    \u4e5f\u53ef\u4ee5\u7528\u5b57\u7b26\u6d41\u6765\u8f6c\u6362\u4e00\u4e0b.<br \/>\n                PrintWriter printWriter <span class=\"token operator\">&#061;<\/span> new PrintWriter<span class=\"token punctuation\">(<\/span>outputStream<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n                \/\/ \u6b64\u5904\u4f7f\u7528 println \u6765\u5199\u5165. \u8ba9\u7ed3\u679c\u4e2d\u5e26\u6709\u4e00\u4e2a <span class=\"token punctuation\">\\\\<\/span>n \u6362\u884c. \u65b9\u4fbf\u5bf9\u7aef\u6765\u63a5\u6536\u89e3\u6790.<br \/>\n                printWriter.println<span class=\"token punctuation\">(<\/span>response<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n                \/\/ flush \u7528\u6765\u5237\u65b0\u7f13\u51b2\u533a, \u4fdd\u8bc1\u5f53\u524d\u5199\u5165\u7684\u6570\u636e, \u786e\u5b9e\u662f\u53d1\u9001\u51fa\u53bb\u4e86.<br \/>\n                printWriter.flush<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>                System.out.printf<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;[%s:%d] req: %s; resp: %s <span class=\"token entity\" title=\"\\\\n\">\\\\n<\/span>&#034;<\/span>, clientSocket.getInetAddress<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>.toString<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>, clientSocket.getPort<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>,<br \/>\n                        request, response<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span> catch <span class=\"token punctuation\">(<\/span>IOException e<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            e.printStackTrace<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span> finally <span class=\"token punctuation\">{<\/span><br \/>\n            \/\/ \u66f4\u5408\u9002\u7684\u505a\u6cd5, \u662f\u628a close \u653e\u5230 finally \u91cc\u9762, \u4fdd\u8bc1\u4e00\u5b9a\u80fd\u591f\u6267\u884c\u5230<span class=\"token operator\">!<\/span><span class=\"token operator\">!<\/span><br \/>\n            try <span class=\"token punctuation\">{<\/span><br \/>\n            clientSocket.close<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n                clientSocket.close<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span> catch <span class=\"token punctuation\">(<\/span>IOException e<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                e.printStackTrace<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    public String process<span class=\"token punctuation\">(<\/span>String request<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token builtin class-name\">return<\/span> request<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    public static void main<span class=\"token punctuation\">(<\/span>String<span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> args<span class=\"token punctuation\">)<\/span> throws IOException <span class=\"token punctuation\">{<\/span><br \/>\n        TcpEchoServer server <span class=\"token operator\">&#061;<\/span> new TcpEchoServer<span class=\"token punctuation\">(<\/span><span class=\"token number\">9090<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        server.start<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>&#x1f338;TCP\u642d\u5efa\u5ba2\u6237\u7aef<\/h3>\n<p>\u642d\u5efa\u5ba2\u6237\u7aef\u6211\u4eec\u4e5f\u53ef\u4ee5\u5206\u4e3a\u4ee5\u4e0b\u51e0\u6b65&#xff1a;<\/p>\n<li>\u521b\u5efaTcpEchoClient\u7c7b\u8868\u793a\u6211\u4eec\u7684\u5ba2\u6237\u7aef&#xff0c;\u521b\u5efaSoket\u5bf9\u8c61\u7528\u4e8e\u4e0e\u5ba2\u6237\u7aef\u901a\u4fe1\u00b7<\/li>\n<li>\u518dTcpEchoClient\u6784\u9020\u65b9\u6cd5\u91cc\u8fdb\u884c\u5b9e\u4f8b\u5316Socket\u7684\u5bf9\u8c61<\/li>\n<li>\u521b\u5efastart()\u65b9\u6cd5\u7528\u4e8e\u6211\u4eec\u7684\u64cd\u4f5c<\/li>\n<li>\u8bfb\u53d6\u952e\u76d8\u6240\u8981\u8f93\u5165\u7684\u6570\u636e<\/li>\n<li>\u5c06\u6240\u8bfb\u7684\u6570\u636e\u901a\u8fc7\u8f93\u51fa\u6d41\u8fdb\u884c\u5199\u5165<\/li>\n<li>\u8bfb\u53d6\u54cd\u5e94\u7684\u8f93\u5165\u6d41&#xff0c;\u8fdb\u884c\u6253\u5370<\/li>\n<li>main\u51fd\u6570\u4e2d\u8fdb\u884c\u542f\u52a8<\/li>\n<p><span class=\"token function\">import<\/span> java.io.IOException<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">import<\/span> java.io.InputStream<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">import<\/span> java.io.OutputStream<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">import<\/span> java.io.PrintWriter<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">import<\/span> java.net.Socket<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token function\">import<\/span> java.util.Scanner<span class=\"token punctuation\">;<\/span><\/p>\n<p>public class TcpEchoClient <span class=\"token punctuation\">{<\/span><br \/>\n    private Socket socket <span class=\"token operator\">&#061;<\/span> null<span class=\"token punctuation\">;<\/span><\/p>\n<p>    public TcpEchoClient<span class=\"token punctuation\">(<\/span>String serverIp, int serverPort<span class=\"token punctuation\">)<\/span> throws IOException <span class=\"token punctuation\">{<\/span><br \/>\n        \/\/ Socket \u6784\u9020\u65b9\u6cd5, \u80fd\u591f\u8bc6\u522b \u70b9\u5206\u5341\u8fdb\u5236\u683c\u5f0f\u7684 IP \u5730\u5740. \u6bd4 DatagramPacket \u66f4\u65b9\u4fbf.<br \/>\n        \/\/ new \u8fd9\u4e2a\u5bf9\u8c61\u7684\u540c\u65f6, \u5c31\u4f1a\u8fdb\u884c TCP \u8fde\u63a5\u64cd\u4f5c.<br \/>\n        socket <span class=\"token operator\">&#061;<\/span> new Socket<span class=\"token punctuation\">(<\/span>serverIp, serverPort<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    public void <span class=\"token function-name function\">start<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        System.out.println<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u5ba2\u6237\u7aef\u542f\u52a8!&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        Scanner scanner <span class=\"token operator\">&#061;<\/span> new Scanner<span class=\"token punctuation\">(<\/span>System.in<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        try <span class=\"token punctuation\">(<\/span>InputStream inputStream <span class=\"token operator\">&#061;<\/span> socket.getInputStream<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n             OutputStream outputStream <span class=\"token operator\">&#061;<\/span> socket.getOutputStream<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">))<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token keyword\">while<\/span> <span class=\"token punctuation\">(<\/span>true<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                \/\/ <span class=\"token number\">1<\/span>. \u5148\u4ece\u952e\u76d8\u4e0a\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9<br \/>\n                System.out.print<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;&gt; &#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n                String request <span class=\"token operator\">&#061;<\/span> scanner.next<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n                <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>request.equals<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;exit&#034;<\/span><span class=\"token punctuation\">))<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                    System.out.println<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;goodbye&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n                    <span class=\"token builtin class-name\">break<\/span><span class=\"token punctuation\">;<\/span><br \/>\n                <span class=\"token punctuation\">}<\/span><br \/>\n                \/\/ <span class=\"token number\">2<\/span>. \u628a\u8bfb\u5230\u7684\u5185\u5bb9\u6784\u9020\u6210\u8bf7\u6c42, \u53d1\u9001\u7ed9\u670d\u52a1\u5668.<br \/>\n                PrintWriter printWriter <span class=\"token operator\">&#061;<\/span> new PrintWriter<span class=\"token punctuation\">(<\/span>outputStream<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n                printWriter.println<span class=\"token punctuation\">(<\/span>request<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n                \/\/ \u6b64\u5904\u52a0\u4e0a flush \u4fdd\u8bc1\u6570\u636e\u786e\u5b9e\u53d1\u9001\u51fa\u53bb\u4e86.<br \/>\n                printWriter.flush<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n                \/\/ <span class=\"token number\">3<\/span>. \u8bfb\u53d6\u670d\u52a1\u5668\u7684\u54cd\u5e94<br \/>\n                Scanner respScanner <span class=\"token operator\">&#061;<\/span> new Scanner<span class=\"token punctuation\">(<\/span>inputStream<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n                String response <span class=\"token operator\">&#061;<\/span> respScanner.next<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n                \/\/ <span class=\"token number\">4<\/span>. \u628a\u54cd\u5e94\u5185\u5bb9\u663e\u793a\u5230\u754c\u9762\u4e0a<br \/>\n                System.out.println<span class=\"token punctuation\">(<\/span>response<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span> catch <span class=\"token punctuation\">(<\/span>IOException e<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            e.printStackTrace<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    public static void main<span class=\"token punctuation\">(<\/span>String<span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> args<span class=\"token punctuation\">)<\/span> throws IOException <span class=\"token punctuation\">{<\/span><br \/>\n        TcpEchoClient client <span class=\"token operator\">&#061;<\/span> new TcpEchoClient<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;127.0.0.1&#034;<\/span>, <span class=\"token number\">9090<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        client.start<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418093721-68021d51da014.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418093722-68021d5202007.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418093722-68021d5210ed9.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418093722-68021d521fcc5.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h2>\u2b55\u603b\u7ed3<\/h2>\n<p>\u5173\u4e8e\u300a\u3010\u7f51\u7edc\u539f\u7406\u3011\u4f7f\u7528Java\u57fa\u4e8eTCP\u5b9e\u73b0\u7b80\u5355\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u901a\u4fe1\u300b\u5c31\u8bb2\u89e3\u5230\u8fd9\u513f&#xff0c;\u611f\u8c22\u5927\u5bb6\u7684\u652f\u6301&#xff0c;\u6b22\u8fce\u5404\u4f4d\u7559\u8a00\u4ea4\u6d41\u4ee5\u53ca\u6279\u8bc4\u6307\u6b63&#xff0c;\u5982\u679c\u6587\u7ae0\u5bf9\u60a8\u6709\u5e2e\u52a9\u6216\u8005\u89c9\u5f97\u4f5c\u8005\u5199\u7684\u8fd8\u4e0d\u9519\u53ef\u4ee5\u70b9\u4e00\u4e0b\u5173\u6ce8&#xff0c;\u70b9\u8d5e&#xff0c;\u6536\u85cf\u652f\u6301\u4e00\u4e0b&#xff01;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb9.5k\u6b21\uff0c\u70b9\u8d5e88\u6b21\uff0c\u6536\u85cf103\u6b21\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86Java\u4e2dServerSocket\u548cSocketAPI\u5728TCP\u901a\u4fe1\u4e2d\u7684\u5e94\u7528\uff0c\u533a\u5206\u4e86\u77ed\u8fde\u63a5\u548c\u957f\u8fde\u63a5\u7684\u533a\u522b\uff0c\u4ee5\u53ca\u5982\u4f55\u5229\u7528\u5b83\u4eec\u5728TCP\u56de\u663e\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u4e2d\u5b9e\u73b0\u6570\u636e\u4ea4\u6362\u3002\u8ba8\u8bba\u4e86BIO\u548cNIO\u5728\u957f\u8fde\u63a5\u4f18\u5316\u4e2d\u7684\u4f5c\u7528\u3002<\/p>\n","protected":false},"author":2,"featured_media":9768,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[99,374,205,193,43,373,78,191,372],"topic":[],"class_list":["post-9772","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-java","tag-java-ee","tag-tcp-ip","tag-udp","tag-43","tag-373","tag-78","tag-191","tag-372"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010\u7f51\u7edc\u539f\u7406\u3011\u4f7f\u7528Java\u57fa\u4e8eTCP\u642d\u5efa\u7b80\u5355\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u901a\u4fe1 - \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\/9772.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010\u7f51\u7edc\u539f\u7406\u3011\u4f7f\u7528Java\u57fa\u4e8eTCP\u642d\u5efa\u7b80\u5355\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb9.5k\u6b21\uff0c\u70b9\u8d5e88\u6b21\uff0c\u6536\u85cf103\u6b21\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86Java\u4e2dServerSocket\u548cSocketAPI\u5728TCP\u901a\u4fe1\u4e2d\u7684\u5e94\u7528\uff0c\u533a\u5206\u4e86\u77ed\u8fde\u63a5\u548c\u957f\u8fde\u63a5\u7684\u533a\u522b\uff0c\u4ee5\u53ca\u5982\u4f55\u5229\u7528\u5b83\u4eec\u5728TCP\u56de\u663e\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u4e2d\u5b9e\u73b0\u6570\u636e\u4ea4\u6362\u3002\u8ba8\u8bba\u4e86BIO\u548cNIO\u5728\u957f\u8fde\u63a5\u4f18\u5316\u4e2d\u7684\u4f5c\u7528\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/9772.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-18T09:37:23+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418093721-68021d51da014.png\" \/>\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=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/9772.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/9772.html\",\"name\":\"\u3010\u7f51\u7edc\u539f\u7406\u3011\u4f7f\u7528Java\u57fa\u4e8eTCP\u642d\u5efa\u7b80\u5355\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-18T09:37:23+00:00\",\"dateModified\":\"2025-04-18T09:37:23+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/9772.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/9772.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/9772.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010\u7f51\u7edc\u539f\u7406\u3011\u4f7f\u7528Java\u57fa\u4e8eTCP\u642d\u5efa\u7b80\u5355\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u901a\u4fe1\"}]},{\"@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":"\u3010\u7f51\u7edc\u539f\u7406\u3011\u4f7f\u7528Java\u57fa\u4e8eTCP\u642d\u5efa\u7b80\u5355\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u901a\u4fe1 - \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\/9772.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010\u7f51\u7edc\u539f\u7406\u3011\u4f7f\u7528Java\u57fa\u4e8eTCP\u642d\u5efa\u7b80\u5355\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb9.5k\u6b21\uff0c\u70b9\u8d5e88\u6b21\uff0c\u6536\u85cf103\u6b21\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86Java\u4e2dServerSocket\u548cSocketAPI\u5728TCP\u901a\u4fe1\u4e2d\u7684\u5e94\u7528\uff0c\u533a\u5206\u4e86\u77ed\u8fde\u63a5\u548c\u957f\u8fde\u63a5\u7684\u533a\u522b\uff0c\u4ee5\u53ca\u5982\u4f55\u5229\u7528\u5b83\u4eec\u5728TCP\u56de\u663e\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u4e2d\u5b9e\u73b0\u6570\u636e\u4ea4\u6362\u3002\u8ba8\u8bba\u4e86BIO\u548cNIO\u5728\u957f\u8fde\u63a5\u4f18\u5316\u4e2d\u7684\u4f5c\u7528\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/9772.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-18T09:37:23+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250418093721-68021d51da014.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/9772.html","url":"https:\/\/www.wsisp.com\/helps\/9772.html","name":"\u3010\u7f51\u7edc\u539f\u7406\u3011\u4f7f\u7528Java\u57fa\u4e8eTCP\u642d\u5efa\u7b80\u5355\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-18T09:37:23+00:00","dateModified":"2025-04-18T09:37:23+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/9772.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/9772.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/9772.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010\u7f51\u7edc\u539f\u7406\u3011\u4f7f\u7528Java\u57fa\u4e8eTCP\u642d\u5efa\u7b80\u5355\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u901a\u4fe1"}]},{"@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\/9772","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=9772"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/9772\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/9768"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=9772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=9772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=9772"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=9772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}