Java 的网络编程涉及使用 Java 的 API 来创建和管理网络应用程序,例如客户端和服务器之间的通信。Java 提供了一系列的类和接口来支持网络编程,主要包括 java.net
包中的功能。网络编程通常包括以下几个方面:
Java 网络编程的基本概念
-
协议:
- TCP/IP(传输控制协议/网际协议):一种面向连接的协议,提供可靠的数据传输。Java 提供了对 TCP/IP 协议的支持。
- UDP(用户数据报协议):一种无连接的协议,不保证数据的可靠传输。适用于对实时性要求较高但可以容忍数据丢失的场景。
-
Socket 编程:
- Socket 是网络通信的端点,可以是客户端或服务器的一部分。Java 的
java.net
包提供了对 Socket 的支持,包括 TCP 和 UDP Socket。
- Socket 是网络通信的端点,可以是客户端或服务器的一部分。Java 的
关键类和接口
Socket
类:
- 用于 TCP 连接的客户端。通过
Socket
可以连接到服务器,进行数据的发送和接收。import java.io.*; import java.net.*; public class Client { public static void main(String[] args) { try (Socket socket = new Socket("localhost", 8080); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) { out.println("Hello, Server"); String response = in.readLine(); System.out.println("Server response: " + response); } catch (IOException e) { e.printStackTrace(); } } }
ServerSocket
类:
- 用于 TCP 连接的服务器端。通过
ServerSocket
创建一个监听指定端口的服务器,等待客户端连接。import java.io.*; import java.net.*; public class Server { public static void main(String[] args) { try (ServerSocket serverSocket = new ServerSocket(8080)) { System.out.println("Server started. Waiting for connection..."); try (Socket clientSocket = serverSocket.accept(); PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()))) { String message = in.readLine(); System.out.println("Client says: " + message); out.println("Hello, Client"); } catch (IOException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } } }
DatagramSocket
类:
- 用于 UDP 连接。提供无连接的数据报服务,可以发送和接收数据报。
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class UDPClient { public static void main(String[] args) { try (DatagramSocket socket = new DatagramSocket()) { String message = "Hello, UDP Server"; byte[] buffer = message.getBytes(); DatagramPacket packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName("localhost"), 9876); socket.send(packet); byte[] receiveBuffer = new byte[1024]; DatagramPacket receivePacket = new DatagramPacket(receiveBuffer, receiveBuffer.length); socket.receive(receivePacket); String response = new String(receivePacket.getData(), 0, receivePacket.getLength()); System.out.println("Server response: " + response); } catch (IOException e) { e.printStackTrace(); } } }
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class UDPServer { public static void main(String[] args) { try (DatagramSocket socket = new DatagramSocket(9876)) { byte[] receiveBuffer = new byte[1024]; DatagramPacket receivePacket = new DatagramPacket(receiveBuffer, receiveBuffer.length); socket.receive(receivePacket); String message = new String(receivePacket.getData(), 0, receivePacket.getLength()); System.out.println("Client says: " + message); String response = "Hello, UDP Client"; byte[] responseBuffer = response.getBytes(); DatagramPacket responsePacket = new DatagramPacket(responseBuffer, responseBuffer.length, receivePacket.getAddress(), receivePacket.getPort()); socket.send(responsePacket); } catch (IOException e) { e.printStackTrace(); } } }
URL
和 URLConnection
类:
- 用于处理 HTTP/HTTPS 协议的网络请求和响应。
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class URLExample { public static void main(String[] args) { try { URL url = new URL("https://api.github.com"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; StringBuilder content = new StringBuilder(); while ((inputLine = in.readLine()) != null) { content.append(inputLine); } in.close(); connection.disconnect(); System.out.println("Response: " + content.toString()); } catch (IOException e) { e.printStackTrace(); } } }
总结
- Java 网络编程 允许你创建客户端和服务器之间的通信,通过 TCP 和 UDP 协议进行数据传输。
- Socket 编程:用于创建客户端和服务器端之间的连接。
- DatagramSocket:用于无连接的 UDP 通信。
- URL 和 URLConnection:用于 HTTP/HTTPS 请求处理。
Java 的网络编程功能强大且灵活,能够支持各种网络应用程序的开发。通过这些 API,你可以实现复杂的网络交互和数据处理。
标签:Java,String,java,编程,网络,import,new,net,Socket From: https://blog.csdn.net/m0_57083961/article/details/140938217