{"id":28555,"date":"2024-11-25T13:28:13","date_gmt":"2024-11-25T05:28:13","guid":{"rendered":"https:\/\/fwq.ai\/blog\/28555\/"},"modified":"2024-11-25T13:28:13","modified_gmt":"2024-11-25T05:28:13","slug":"%e5%8f%8c%e5%90%91%e5%90%8c%e6%ad%a5%e8%81%8a%e5%a4%a9%e5%b0%8f%e7%a8%8b%e5%ba%8f%ef%bc%bbbyjavaonlinux%ef%bc%bd%e5%ae%9e%e7%8e%b0%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/28555\/","title":{"rendered":"\u53cc\u5411\u540c\u6b65\u804a\u5929\u5c0f\u7a0b\u5e8f\uff3bByJavaOnLinux\uff3d\u5b9e\u73b0\u4ee3\u7801"},"content":{"rendered":"<p style=\"text-align: left\">\u4e00\u4e2a\u5f88\u7b80\u5355\u7684\u7f51\u7edc\u804a\u5929\u5c0f\u5de5\u5177\uff0cjava\u5b9e\u73b0\uff0c\u53cc\u5411\u540c\u6b65\u53d1\u9001\u4fe1\u606f\uff0c\u529f\u80fd\u589e\u52a0\u4e2d<br \/>\u5c40\u57df\u7f51\u5185ip\u8bf7\u81ea\u884c\u66f4\u6539\uff0c\u53ea\u8981\u53d8\u66f4client\u7aef\u7684localhost ip\u4e3a\u53e6\u4e00\u53f0pc\u7684ip\u5c31\u884c<\/p>\n<pre>import java.io.*;\r\nimport java.net.Socket;\r\nimport java.net.ServerSocket;\r\nimport java.net.SocketException;\r\npublic class TestServer {\r\n        public static void main(String[] args) {\r\n                try {\r\n                        \/\/open the communication port for messenge-transfer\r\n                        \/\/server socket id:8888\r\n                        ServerSocket s = new ServerSocket(8888);\r\n                        \/\/create socket instance and set it be waiting state to accept data\r\n                        Socket s1 = s.accept();\r\n                        \/\/original data stream\r\n                        InputStream is = s1.getInputStream();\r\n                        OutputStream os = s1.getOutputStream();\r\n                        \r\n                        DataOutputStream dos = new DataOutputStream(os);\r\n                        DataInputStream dis = new DataInputStream(is);\r\n                        System.out.println(\"Server started!\");\r\n                        new MyServerReader(dis).start();\r\n                        new MyServerWriter(dos).start();\r\n                } catch (IOException ioe) {\r\n                        ioe.printStackTrace();\r\n                }\r\n        }\r\n}\r\nclass MyServerReader extends Thread {\r\n        private DataInputStream dis;\r\n        public MyServerReader(DataInputStream dis) {\r\n                this.dis = dis;\r\n        }\r\n        public void run() {\r\n                String info;\r\n                try {\r\n                        while (true) {\r\n                                info = dis.readUTF();\r\n                                System.out.println(\"Ta said:\" + info);\r\n                                if (info.equals(\"bye\") || info.equals(\"88\")) {\r\n                                        System.out.println(\"Ta offline, connection's out!\");\r\n                                        System.exit(0);\r\n                                }\r\n                        }\r\n                } catch (IOException e) {\r\n                        e.printStackTrace();\r\n                }\r\n        }\r\n}\r\nclass MyServerWriter extends Thread {\r\n        private DataOutputStream dos;\r\n        public MyServerWriter (DataOutputStream dos) {\r\n                this.dos = dos;\r\n        }\r\n        public void run() {\r\n                String info;\r\n                InputStreamReader isr = new InputStreamReader(System.in);\r\n                BufferedReader br = new BufferedReader(isr);\r\n                try {\r\n                        while (true) {\r\n                                info = br.readLine();\r\n                                dos.writeUTF(info);\r\n                                if (info.equals(\"bye\") || info.equals(\"88\")) {\r\n                                        System.out.println(\"Local machine Offline, application exit!\");\r\n                                        System.exit(0);\r\n                                }\r\n                        }\r\n                } catch (IOException e) {\r\n                        e.printStackTrace();\r\n                }\r\n        }\r\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p style=\"text-align: left\">client\u7aef\u7684\uff1a<\/p>\n<pre>import java.io.*;\r\nimport java.net.Socket;\r\nimport java.net.SocketException;\r\npublic class TestClient {\r\n        public static void main (String[] args) {\r\n                try {\r\n                        Socket s1 = new Socket(\"127.0.0.1\", 8888);\r\n                        InputStream is = s1.getInputStream();\r\n                        OutputStream os = s1.getOutputStream();\r\n                        DataInputStream dis = new DataInputStream(is);\r\n                        DataOutputStream dos = new DataOutputStream(os);\r\n                        new MyClientReader(dis).start();\r\n                        new MyClientWriter(dos).start();\r\n                } catch (IOException e) {\r\n                        e.printStackTrace();\r\n                }\r\n        }\r\n}\r\nclass MyClientReader extends Thread {\r\n        private DataInputStream dis;\r\n        public MyClientReader(DataInputStream dis) {\r\n                this.dis = dis;\r\n        }\r\n        public void run() {\r\n                String info;\r\n                try {\r\n                        while (true) {\r\n                                info = dis.readUTF();\r\n                                System.out.println(\"Ta said:\" + info);\r\n                                if (info.equals(\"bye\") || info.equals(\"88\")) {\r\n                                        System.out.println(\"Ta offline, connection's out!\");\r\n                                        System.exit(0);\r\n                                }\r\n                        }\r\n                } catch (IOException e) {\r\n                        e.printStackTrace();\r\n                }\r\n        }\r\n}\r\nclass MyClientWriter extends Thread {\r\n        private DataOutputStream dos;\r\n        public MyClientWriter (DataOutputStream dos) {\r\n                this.dos = dos;\r\n        }\r\n        public void run() {\r\n                String info;\r\n                InputStreamReader isr = new InputStreamReader(System.in);\r\n                BufferedReader br = new BufferedReader(isr);\r\n                try {\r\n                        while (true) {\r\n                                info = br.readLine();\r\n                                dos.writeUTF(info);\r\n                                if (info.equals(\"bye\") || info.equals(\"88\")) {\r\n                                        System.out.println(\"Local machine Offline, application exit!\");\r\n                                        System.exit(0);\r\n                                }\r\n                        }\r\n                } catch (IOException e) {\r\n                        e.printStackTrace();\r\n                }\r\n        }\r\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u53cc\u5411\u540c\u6b65\u804a\u5929\u5c0f\u7a0b\u5e8f\uff3bByJavaOnLinux\uff3d\u5b9e\u73b0\u4ee3\u7801\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u4e2a\u5f88\u7b80\u5355\u7684\u7f51\u7edc\u804a\u5929\u5c0f\u5de5\u5177\uff0cjava\u5b9e\u73b0\uff0c\u53cc\u5411\u540c\u6b65\u53d1\u9001\u4fe1\u606f\uff0c\u529f\u80fd\u589e\u52a0\u4e2d\u5c40\u57df\u7f51\u5185ip\u8bf7\u81ea\u884c\u66f4\u6539\uff0c\u53ea\u8981\u53d8\u66f4client\u7aef\u7684localhost ip\u4e3a\u53e6\u4e00\u53f0pc\u7684ip\u5c31\u884c import java.io.*; import java.net.Socket; import java.net.ServerSocket; import java.net.SocketException; public class TestServer { public static void main(String[] args) { try { \/\/open the communication port for messenge-transfer \/\/server socket id:8888 ServerSocket s = new ServerSocket(8888); \/\/create socket instance and set it be waiting state to accept data Socket s1 = s.accept(); \/\/original data stream [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-28555","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/28555","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=28555"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/28555\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=28555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=28555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=28555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}