{"id":2132,"date":"2017-08-25T11:48:49","date_gmt":"2017-08-25T03:48:49","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=2132"},"modified":"2026-08-18T12:09:38","modified_gmt":"2026-08-18T04:09:38","slug":"socket%e9%80%9a%e8%ae%af%e8%af%bb%e5%8f%96%e6%95%b0%e6%8d%ae%e6%97%b6%e9%98%bb%e5%a1%9e%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=2132","title":{"rendered":"Socket\u901a\u8baf\u8bfb\u53d6\u6570\u636e\u65f6\u963b\u585e\u89e3\u51b3\u65b9\u6848"},"content":{"rendered":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<p style=\"margin: 1em 0px;\">\u8bfb\u6587\u4ef6\u65f6 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">read<\/code> \u8bfb\u5230\u672b\u5c3e\u4f1a\u8fd4\u56de <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">-1<\/code>\u3002\u4f46 <strong style=\"color: #4870ac;\">Socket<\/strong> \u901a\u8baf\u91cc\u4ece <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">InputStream<\/code> \u8bfb\u6570\u636e\uff0c\u5bf9\u7aef\u8fd8\u6ca1\u5173\u6d41\u3001\u6682\u65f6\u53c8\u6ca1\u6709\u6570\u636e\u65f6\uff0c<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">read<\/code> \u4f1a\u4e00\u76f4\u963b\u585e\uff0c\u53ea\u6709\u5173\u95ed\u6d41\u624d\u4f1a\u8fd4\u56de <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">-1<\/code>\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u6240\u4ee5\u4e0b\u9762\u8fd9\u79cd\u300c\u8bfb\u5230 -1 \u624d\u505c\u300d\u7684\u5199\u6cd5\uff0c\u5728 Socket \u4e0a\u5f88\u5bb9\u6613\u5361\u4f4f\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">while ((len = in.read(bytes)) != -1) {\nos.write(bytes, 0, len);\nos.flush();\n}<\/code><\/pre>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e00\u3001\u6bcf\u53d1\u4e00\u6b21\u5c31\u5f00\u5173\u8fde\u63a5<\/h2>\n<p style=\"margin: 1em 0px;\">\u6bcf\u53d1\u9001\u4e00\u6b21\u6d88\u606f\u5f00\u542f Socket \u8fde\u63a5\uff0c\u53d1\u5b8c\u5173\u95ed\u3002\u5173\u6d41\u540e\u670d\u52a1\u7aef <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">read<\/code> \u5c31\u80fd\u62ff\u5230 -1\uff0c\u5faa\u73af\u7ed3\u675f\u3002<\/p>\n<p style=\"margin: 1em 0px;\">Socket \u670d\u52a1\u7aef\u63a5\u6536\u6d88\u606f\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">ServerSocket serverSocket = new ServerSocket(port);\nwhile(true){\nsocket = serverSocket.accept();\nreceiveMessage();\/\/\u63a5\u6536\u6d88\u606f\n}\n\nSocket\u5ba2\u6237\u7aef\u53d1\u9001\u6d88\u606f:\nsocket = new Socket(host, port);  \/\/\u53d1\u8d77\u8fde\u63a5\nclient.sendPicture(fileDir); \/\/\u53d1\u9001\u6d88\u606f<\/code><\/pre>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e8c\u3001\u5148\u53d1\u6587\u4ef6\u5927\u5c0f<\/h2>\n<p style=\"margin: 1em 0px;\">\u5ba2\u6237\u7aef\u5148\u628a\u6587\u4ef6\u5b57\u8282\u5927\u5c0f\u5199\u8fc7\u53bb\uff0c\u670d\u52a1\u7aef\u7d2f\u8ba1\u8bfb\u5230\u7684\u5b57\u8282\u6570\uff0c\u591f\u4e86\u5c31 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">break<\/code>\uff0c\u4e0d\u5fc5\u7b49\u6d41\u5173\u95ed\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">Socket\u5ba2\u6237\u7aef:\ndos.writeLong(file.length()); \/\/\u6587\u4ef6\u5b57\u8282\u5927\u5c0f\nSokcet\u670d\u52a1\u7aef:\n\/\/\u8bfb\u53d6\u56fe\u7247\u5927\u5c0f\nlong picLength = dos.readLong();\n\/\/\u56fe\u7247\u6587\u4ef6\u5185\u5bb9\nbyte[] bytes = new byte[128];\nint len = 0;\nint count = 0; \/\/\u7edf\u8ba1\u8bfb\u53d6\u6d41\u603b\u5b57\u8282\u6570\nwhile ((len = in.read(bytes)) != -1) {\nos.write(bytes, 0, len);\nos.flush();\ncount = count + len;\nif(count == picLength){ \/\/\u63a5\u6536\u6570\u636e\u5927\u5c0f\u4e0e\u6587\u4ef6\u5927\u5c0f\u6bd4\u8f83\nbreak;\n}\n}<\/code><\/pre>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e09\u3001\u8bbe\u7f6e\u8bfb\u8d85\u65f6<\/h2>\n<p style=\"margin: 1em 0px;\">\u7528 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">socket.setSoTimeout<\/code> \u8bbe\u8bfb\u8d85\u65f6\u3002\u4e00\u6bb5\u65f6\u95f4\u6ca1\u6570\u636e\u5c31\u629b <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">IOException<\/code>\uff0c\u5faa\u73af\u88ab\u6253\u65ad\uff1b\u5904\u7406\u5b8c\u518d\u628a\u8d85\u65f6\u8bbe\u56de 0\uff08\u65e0\u7a77\u5927\uff09\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">Socket\u670d\u52a1\u7aef:\ntry {\nbyte[] bytes = new byte[128];\nint len = 0;\nsocket.setSoTimeout(10); \/\/\u8bbe\u7f6esocket\u8bfb\u8d85\u65f6\u65f6\u95f4\u4e3a10\u6beb\u79d2\nwhile ((len = in.read(bytes)) != -1) {\nos.write(bytes, 0, len);\nos.flush();\n}\n} catch (IOException e) {\nSystem.out.println(\"\u63a5\u6536\u56fe\u7247\u6210\u529f!\");\nsocket.setSoTimeout(0);\/\/\u8bbe\u7f6esocket\u8bfb\u8d85\u65f6\u65f6\u95f4\u4e3a\u65e0\u7a77\u5927\n}<\/code><\/pre>\n<table style=\"display: table; text-align: left; overflow: auto; border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; overflow-wrap: break-word; word-break: break-all;\" width=\"800\">\n<thead>\n<tr>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u65b9\u6848<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u505a\u6cd5<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u4f55\u65f6\u7ed3\u675f\u8bfb\u5faa\u73af<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5f00\u5173\u8fde\u63a5<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u53d1\u5b8c\u5c31\u5173 Socket<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5bf9\u7aef\u5173\u6d41\uff0cread \u8fd4\u56de -1<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5148\u53d1\u957f\u5ea6<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">writeLong(file.length())<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u7d2f\u8ba1\u5b57\u8282\u6570\u7b49\u4e8e\u6587\u4ef6\u5927\u5c0f\u5219 break<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u8bfb\u8d85\u65f6<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">setSoTimeout(10)<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u8d85\u65f6\u629b IOException<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<blockquote style=\"display: block; font-size: 0.9em; border-left: 3px solid #4870ac; padding: 0.5em 1em; margin: 0px; background: #f6f8fa; color: #40464f;\">\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1aSocket \u7684 read \u6ca1\u6570\u636e\u4f1a\u963b\u585e\uff0c\u522b\u53ea\u7b49 -1\uff1b\u7528\u5173\u8fde\u63a5\u3001\u5148\u4f20\u957f\u5ea6\u6216 setSoTimeout \u624d\u80fd\u505c\u4e0b\u6765\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u8bfb\u6587\u4ef6\u65f6 read \u8bfb\u5230\u672b\u5c3e\u4f1a\u8fd4\u56de -1\u3002\u4f46 Socket \u901a\u8baf\u91cc\u4ece InputStream \u8bfb\u6570\u636e\uff0c\u5bf9\u7aef\u8fd8\u6ca1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20],"tags":[41,26],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2132"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2132"}],"version-history":[{"count":3,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2132\/revisions"}],"predecessor-version":[{"id":3737,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2132\/revisions\/3737"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}