{"id":2272,"date":"2025-03-03T23:14:25","date_gmt":"2025-03-03T15:14:25","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2272"},"modified":"2025-03-03T23:14:25","modified_gmt":"2025-03-03T15:14:25","slug":"java-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/03\/java-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e8%af%a6%e8%a7%a3\/","title":{"rendered":"Java \u7f51\u7edc\u7f16\u7a0b\u8be6\u89e3"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Java <strong>\u7f51\u7edc\u7f16\u7a0b<\/strong>\uff08Network Programming\uff09\u4e3b\u8981\u7528\u4e8e\u5904\u7406 <strong>TCP\/IP<\/strong>\u3001<strong>UDP<\/strong>\u3001<strong>Socket<\/strong> \u901a\u4fe1\uff0c\u652f\u6301 <strong>\u5ba2\u6237\u7aef-\u670d\u52a1\u5668\uff08C\/S\uff09\u6a21\u578b<\/strong> \u4ee5\u53ca <strong>HTTP \u8bf7\u6c42<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Java \u7f51\u7edc\u7f16\u7a0b\u57fa\u7840<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>1.1 Java \u7f51\u7edc\u7f16\u7a0b\u6838\u5fc3 API<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Java \u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7f51\u7edc\u76f8\u5173\u7684 API\uff0c\u4e3b\u8981\u5305\u62ec\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7c7b<\/th><th>\u4f5c\u7528<\/th><\/tr><\/thead><tbody><tr><td><code>InetAddress<\/code><\/td><td>\u5904\u7406 IP \u5730\u5740\uff08IPv4 \/ IPv6\uff09<\/td><\/tr><tr><td><code>Socket<\/code><\/td><td>\u5b9e\u73b0 TCP \u5ba2\u6237\u7aef<\/td><\/tr><tr><td><code>ServerSocket<\/code><\/td><td>\u5b9e\u73b0 TCP \u670d\u52a1\u5668\u7aef<\/td><\/tr><tr><td><code>DatagramSocket<\/code><\/td><td>\u5b9e\u73b0 UDP \u901a\u4fe1<\/td><\/tr><tr><td><code>DatagramPacket<\/code><\/td><td>\u5904\u7406 UDP \u6570\u636e\u5305<\/td><\/tr><tr><td><code>URL<\/code> \/ <code>URLConnection<\/code><\/td><td>\u5904\u7406 HTTP \/ FTP<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Java <code>InetAddress<\/code>\uff08IP \u5730\u5740\u7c7b\uff09<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>InetAddress<\/code> \u4ee3\u8868 <strong>IP \u5730\u5740<\/strong>\uff0c\u7528\u4e8e\u5904\u7406\u4e3b\u673a\u540d\u548c IP \u4e4b\u95f4\u7684\u8f6c\u6362\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.1 \u83b7\u53d6\u672c\u673a\u548c\u8fdc\u7a0b IP<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.net.*;\n\npublic class InetAddressDemo {\n    public static void main(String&#91;] args) throws UnknownHostException {\n        \/\/ \u83b7\u53d6\u672c\u673a IP\n        InetAddress localHost = InetAddress.getLocalHost();\n        System.out.println(\"\u672c\u673a IP: \" + localHost.getHostAddress());\n\n        \/\/ \u83b7\u53d6\u8fdc\u7a0b\u4e3b\u673a IP\n        InetAddress remoteHost = InetAddress.getByName(\"www.google.com\");\n        System.out.println(\"Google IP: \" + remoteHost.getHostAddress());\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Java TCP \u901a\u4fe1<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">TCP\uff08\u4f20\u8f93\u63a7\u5236\u534f\u8bae\uff09\u662f<strong>\u9762\u5411\u8fde\u63a5<\/strong>\u7684\u53ef\u9760\u901a\u4fe1\u534f\u8bae\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3.1 TCP \u670d\u52a1\u5668\u7aef<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.*;\nimport java.net.*;\n\npublic class TCPServer {\n    public static void main(String&#91;] args) throws IOException {\n        ServerSocket serverSocket = new ServerSocket(9999); \/\/ \u76d1\u542c 9999 \u7aef\u53e3\n        System.out.println(\"\u670d\u52a1\u5668\u542f\u52a8\uff0c\u7b49\u5f85\u8fde\u63a5...\");\n\n        Socket socket = serverSocket.accept(); \/\/ \u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5\n        System.out.println(\"\u5ba2\u6237\u7aef\u5df2\u8fde\u63a5\uff1a\" + socket.getInetAddress());\n\n        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));\n        String message = in.readLine();\n        System.out.println(\"\u6536\u5230\u6d88\u606f\uff1a\" + message);\n\n        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);\n        out.println(\"Hello, Client!\");\n\n        in.close();\n        out.close();\n        socket.close();\n        serverSocket.close();\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3.2 TCP \u5ba2\u6237\u7aef<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.*;\nimport java.net.*;\n\npublic class TCPClient {\n    public static void main(String&#91;] args) throws IOException {\n        Socket socket = new Socket(\"localhost\", 9999); \/\/ \u8fde\u63a5\u670d\u52a1\u5668\n\n        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);\n        out.println(\"Hello, Server!\");\n\n        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));\n        System.out.println(\"\u670d\u52a1\u5668\u56de\u590d\uff1a\" + in.readLine());\n\n        in.close();\n        out.close();\n        socket.close();\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Java UDP \u901a\u4fe1<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">UDP\uff08\u7528\u6237\u6570\u636e\u62a5\u534f\u8bae\uff09\u662f<strong>\u65e0\u8fde\u63a5<\/strong>\u7684\u901a\u4fe1\u534f\u8bae\uff0c\u9002\u7528\u4e8e\u9ad8\u901f\u4f20\u8f93\u4f46\u4e0d\u4fdd\u8bc1\u53ef\u9760\u6027\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>4.1 UDP \u670d\u52a1\u5668\u7aef<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.net.*;\n\npublic class UDPServer {\n    public static void main(String&#91;] args) throws Exception {\n        DatagramSocket socket = new DatagramSocket(8888);\n        byte&#91;] buffer = new byte&#91;1024];\n\n        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);\n        System.out.println(\"UDP \u670d\u52a1\u5668\u5df2\u542f\u52a8\uff0c\u7b49\u5f85\u6570\u636e...\");\n\n        socket.receive(packet); \/\/ \u63a5\u6536\u6570\u636e\n        String message = new String(packet.getData(), 0, packet.getLength());\n        System.out.println(\"\u6536\u5230\u6d88\u606f\uff1a\" + message);\n\n        socket.close();\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>4.2 UDP \u5ba2\u6237\u7aef<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.net.*;\n\npublic class UDPClient {\n    public static void main(String&#91;] args) throws Exception {\n        DatagramSocket socket = new DatagramSocket();\n        InetAddress address = InetAddress.getByName(\"localhost\");\n\n        String message = \"Hello, UDP Server!\";\n        byte&#91;] buffer = message.getBytes();\n\n        DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, 8888);\n        socket.send(packet); \/\/ \u53d1\u9001\u6570\u636e\n\n        System.out.println(\"\u6570\u636e\u5df2\u53d1\u9001\uff01\");\n        socket.close();\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. Java \u5904\u7406 HTTP \u8bf7\u6c42<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Java \u63d0\u4f9b <code>URL<\/code> \u548c <code>URLConnection<\/code> \u5904\u7406 HTTP \u8bbf\u95ee\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>5.1 \u53d1\u9001 HTTP GET \u8bf7\u6c42<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.*;\nimport java.net.*;\n\npublic class HttpGetExample {\n    public static void main(String&#91;] args) throws Exception {\n        URL url = new URL(\"https:\/\/www.google.com\");\n        HttpURLConnection connection = (HttpURLConnection) url.openConnection();\n\n        connection.setRequestMethod(\"GET\"); \/\/ \u53d1\u9001 GET \u8bf7\u6c42\n        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));\n\n        String line;\n        while ((line = in.readLine()) != null) {\n            System.out.println(line);\n        }\n\n        in.close();\n        connection.disconnect();\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. Java \u591a\u7ebf\u7a0b Socket \u670d\u52a1\u5668<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>6.1 \u591a\u7ebf\u7a0b\u5904\u7406\u591a\u4e2a\u5ba2\u6237\u7aef<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.*;\nimport java.net.*;\n\nclass ClientHandler extends Thread {\n    private Socket clientSocket;\n\n    public ClientHandler(Socket socket) {\n        this.clientSocket = socket;\n    }\n\n    public void run() {\n        try {\n            BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));\n            PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);\n\n            String message = in.readLine();\n            System.out.println(\"\u6536\u5230\u5ba2\u6237\u7aef\u6d88\u606f\uff1a\" + message);\n            out.println(\"\u670d\u52a1\u5668\u6536\u5230\uff1a\" + message);\n\n            in.close();\n            out.close();\n            clientSocket.close();\n        } catch (IOException e) {\n            e.printStackTrace();\n        }\n    }\n}\n\npublic class MultiThreadedServer {\n    public static void main(String&#91;] args) throws IOException {\n        ServerSocket serverSocket = new ServerSocket(9999);\n        System.out.println(\"\u591a\u7ebf\u7a0b\u670d\u52a1\u5668\u542f\u52a8\uff0c\u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5...\");\n\n        while (true) {\n            Socket clientSocket = serverSocket.accept();\n            System.out.println(\"\u5ba2\u6237\u7aef\u5df2\u8fde\u63a5\uff1a\" + clientSocket.getInetAddress());\n            new ClientHandler(clientSocket).start();\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7. Java NIO\uff08\u975e\u963b\u585e IO\uff09<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Java NIO\uff08New IO\uff09\u63d0\u4f9b\u4e86\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u901a\u4fe1\u80fd\u529b\uff0c\u9002\u7528\u4e8e<strong>\u9ad8\u5e76\u53d1\u573a\u666f<\/strong>\uff08\u5982\u804a\u5929\u5ba4\u3001Web \u670d\u52a1\u5668\uff09\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>7.1 NIO \u670d\u52a1\u5668<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.IOException;\nimport java.net.InetSocketAddress;\nimport java.nio.ByteBuffer;\nimport java.nio.channels.*;\n\npublic class NioServer {\n    public static void main(String&#91;] args) throws IOException {\n        Selector selector = Selector.open();\n        ServerSocketChannel serverChannel = ServerSocketChannel.open();\n        serverChannel.bind(new InetSocketAddress(9999));\n        serverChannel.configureBlocking(false);\n        serverChannel.register(selector, SelectionKey.OP_ACCEPT);\n\n        System.out.println(\"NIO \u670d\u52a1\u5668\u542f\u52a8...\");\n\n        while (true) {\n            selector.select();\n            for (SelectionKey key : selector.selectedKeys()) {\n                if (key.isAcceptable()) {\n                    SocketChannel clientChannel = serverChannel.accept();\n                    clientChannel.configureBlocking(false);\n                    clientChannel.register(selector, SelectionKey.OP_READ);\n                } else if (key.isReadable()) {\n                    SocketChannel clientChannel = (SocketChannel) key.channel();\n                    ByteBuffer buffer = ByteBuffer.allocate(1024);\n                    int bytesRead = clientChannel.read(buffer);\n                    if (bytesRead &gt; 0) {\n                        System.out.println(\"\u6536\u5230\uff1a\" + new String(buffer.array()).trim());\n                    }\n                }\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 <code>Socket<\/code> \u9002\u7528\u4e8e TCP \u8fde\u63a5<br>\u2705 <code>DatagramSocket<\/code> \u9002\u7528\u4e8e UDP<br>\u2705 <code>URLConnection<\/code> \u9002\u7528\u4e8e HTTP \u8bbf\u95ee<br>\u2705 <code>NIO<\/code> \u9002\u7528\u4e8e\u9ad8\u5e76\u53d1\u7f51\u7edc\u901a\u4fe1<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java \u7f51\u7edc\u7f16\u7a0b\uff08Network Programming\uff09\u4e3b\u8981\u7528\u4e8e\u5904\u7406 TCP\/IP\u3001UDP\u3001Socket  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68],"tags":[],"class_list":["post-2272","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2272"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2272\/revisions"}],"predecessor-version":[{"id":2273,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2272\/revisions\/2273"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}