{"id":1352,"date":"2022-10-31T22:51:45","date_gmt":"2022-10-31T14:51:45","guid":{"rendered":"https:\/\/qaqaq.top\/?p=1352"},"modified":"2022-11-27T12:39:46","modified_gmt":"2022-11-27T04:39:46","slug":"io%e6%b5%81%e4%b8%8e%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b-tcp%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e4%be%8b%e9%a2%981","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=1352","title":{"rendered":"IO\u6d41\u4e0e\u7f51\u7edc\u7f16\u7a0b-TCP\u7f51\u7edc\u7f16\u7a0b\u4f8b\u98981"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P624;\r\n\r\nimport org.junit.jupiter.api.Test;\r\n\r\nimport java.io.ByteArrayOutputStream;\r\nimport java.io.IOException;\r\nimport java.io.InputStream;\r\nimport java.io.OutputStream;\r\nimport java.net.InetAddress;\r\nimport java.net.ServerSocket;\r\nimport java.net.Socket;\r\n\r\n\/**\r\n * \u5b9e\u73b0TCP\u7684\u7f51\u7edc\u7f16\u7a0b\r\n * \u4f8b\u5b501\uff1a\u5ba2\u6237\u7aef\u53d1\u9001\u4fe1\u606f\u7ed9\u670d\u52a1\u7aef\uff0c\u670d\u52a1\u7aef\u5c06\u6570\u636e\u663e\u793a\u5728\u63a7\u5236\u53f0\u4e0a\r\n *\r\n *\r\n * @author RichieZhang\r\n * @create 2022-10-31 \u4e0b\u5348 9:40\r\n *\/\r\npublic class TCPTest1 {\r\n\r\n    \/\/\u5ba2\u6237\u7aef\r\n    @Test\r\n    public void client() {\r\n\r\n        Socket socket = null;\r\n        OutputStream os = null;\r\n        try {\r\n            \/\/1. \u521b\u5efaSocket\u5bf9\u8c61\uff0c\u6307\u660e\u670d\u52a1\u5668\u7aef\u7684ip\u548c\u7aef\u53e3\u53f7\r\n            InetAddress inet = InetAddress.getByName(\"192.168.31.190\");\r\n            socket = new Socket(inet,8899);\r\n            \/\/ 2. \u83b7\u53d6\u4e00\u4e2a\u8f93\u51fa\u6d41\uff0c\u7528\u4e8e\u8f93\u51fa\u6570\u636e\r\n            os = socket.getOutputStream();\r\n            \/\/ 3. \u5199\u51fa\u6570\u636e\u7684\u64cd\u4f5c\r\n            os.write(\"\u4f60\u597d\uff0c\u6211\u662f\u5ba2\u6237\u7aefmm\".getBytes());\r\n        } catch (IOException e) {\r\n            e.printStackTrace();\r\n        } finally {\r\n            \/\/ 4. \u8d44\u6e90\u7684\u5173\u95ed\r\n            if (os != null){\r\n                try {\r\n                    os.close();\r\n                } catch (IOException e) {\r\n                    e.printStackTrace();\r\n                }\r\n\r\n            }\r\n\r\n            if (os != null){\r\n                try {\r\n                    socket.close();\r\n                } catch (IOException e) {\r\n                    e.printStackTrace();\r\n                }\r\n\r\n            }\r\n        }\r\n\r\n    }\r\n\r\n    \/\/\u670d\u52a1\u7aef\r\n    @Test\r\n    public void server() {\r\n\r\n        ServerSocket ss = null;\r\n        Socket socket = null;\r\n        InputStream is = null;\r\n        ByteArrayOutputStream baos = null;\r\n        try {\r\n            \/\/ 1. \u521b\u5efa\u670d\u52a1\u5668\u7aef\u7684ServerSocket\uff0c\u6307\u660e\u81ea\u5df1\u7684\u7aef\u53e3\u53f7\r\n            ss = new ServerSocket(8899);\r\n            \/\/ 2. \u8c03\u7528accept()\u8868\u793a\u63a5\u53d7\u6765\u81ea\u4e8e\u5ba2\u6237\u7aef\u7684socket\r\n            socket = ss.accept();\r\n            \/\/ 3. \u83b7\u53d6\u8f93\u5165\u6d41\r\n            is = socket.getInputStream();\r\n\r\n            \/\/\u4e0d\u5efa\u8bae\u8fd9\u6837\u5199\uff0c\u53ef\u80fd\u4f1a\u6709\u4e71\u7801\r\n\/\/        byte&#91;] buffer = new byte&#91;1024];\r\n\/\/        int len;\r\n\/\/        while ((len = is.read(buffer)) != -1){\r\n\/\/            String str = new String(buffer, 0, len);\r\n\/\/            System.out.println(str);\r\n\/\/        }\r\n\r\n            \/\/ 4. \u8bfb\u53d6\u8f93\u5165\u6d41\u4e2d\u7684\u6570\u636e\r\n            baos = new ByteArrayOutputStream();\r\n            byte&#91;] buffer = new byte&#91;5];\r\n            int len;\r\n            while ((len = is.read(buffer)) != -1){\r\n                baos.write(buffer,0,len);\r\n            }\r\n\r\n            System.out.println(baos.toString());\r\n\r\n            System.out.println(\"\u6536\u5230\u4e86\u6765\u81ea\u4e8e\uff1a\" + socket.getInetAddress().getHostAddress() + \"\u7684\u6570\u636e\");\r\n        } catch (IOException e) {\r\n            e.printStackTrace();\r\n        } finally {\r\n            if (baos != null){\r\n                \/\/ 5. \u5173\u95ed\u8d44\u6e90\r\n                try {\r\n                    baos.close();\r\n                } catch (IOException e) {\r\n                    e.printStackTrace();\r\n                }\r\n            }\r\n\r\n            if (is != null){\r\n                try {\r\n                    is.close();\r\n                } catch (IOException e) {\r\n                    e.printStackTrace();\r\n                }\r\n            }\r\n\r\n            if (socket != null){\r\n                try {\r\n                    socket.close();\r\n                } catch (IOException e) {\r\n                    e.printStackTrace();\r\n                }\r\n            }\r\n\r\n            if (ss != null){\r\n                try {\r\n                    ss.close();\r\n                } catch (IOException e) {\r\n                    e.printStackTrace();\r\n                }\r\n            }\r\n        }\r\n\r\n    }\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[46],"class_list":["post-1352","post","type-post","status-publish","format-standard","hentry","category-java-code","tag-java"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1352"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1352"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1352\/revisions"}],"predecessor-version":[{"id":1353,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1352\/revisions\/1353"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}