How to use network programming functions in Java for network communication
In today's information age, network communication is a very important part. As a cross-platform programming language, Java provides powerful network programming functions, allowing developers to easily implement network communication functions in programs. This article will introduce how to use network programming functions in Java for network communication and provide specific code examples.
import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) throws Exception { ServerSocket serverSocket = new ServerSocket(8888); System.out.println("服务器已启动,等待客户端连接..."); while (true) { Socket socket = serverSocket.accept(); System.out.println("客户端连接成功,IP地址:" + socket.getInetAddress() + ",端口号:" + socket.getPort()); // 处理客户端请求 // ... socket.close(); } } }
In the above code, a ServerSocket object is created, and the parameter 8888 represents the port number that the server listens on. By calling the accept() method, you can accept the client's connection request and create the corresponding Socket object. Then, the client request can be processed in a while loop. After completing the processing, the socket.close() method needs to be called to close the connection with the client.
import java.net.Socket; public class Client { public static void main(String[] args) throws Exception { String serverIP = "127.0.0.1"; // 服务器IP地址 int serverPort = 8888; // 服务器端口号 Socket socket = new Socket(serverIP, serverPort); System.out.println("连接服务器成功,IP地址:" + socket.getInetAddress() + ",端口号:" + socket.getPort()); // 发送请求给服务器 // ... socket.close(); } }
In the above code, a Socket object is created and the IP address and port number of the server are passed in as parameters. By calling the connect() method, a connection can be established with the server. After the connection is successful, you can communicate with the server's Socket object. After completing the communication, you need to call the socket.close() method to close the connection with the server.
Server-side code:
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) throws Exception { ServerSocket serverSocket = new ServerSocket(8888); System.out.println("服务器已启动,等待客户端连接..."); while (true) { Socket socket = serverSocket.accept(); System.out.println("客户端连接成功,IP地址:" + socket.getInetAddress() + ",端口号:" + socket.getPort()); // 接收客户端消息 BufferedReader reader = new BufferedReader( new InputStreamReader(socket.getInputStream())); String clientMsg = reader.readLine(); System.out.println("接收到客户端消息:" + clientMsg); // 发送响应给客户端 PrintWriter writer = new PrintWriter(socket.getOutputStream()); writer.println("服务器已收到消息:" + clientMsg); writer.flush(); reader.close(); writer.close(); socket.close(); } } }
Client-side code:
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; public class Client { public static void main(String[] args) throws Exception { String serverIP = "127.0.0.1"; // 服务器IP地址 int serverPort = 8888; // 服务器端口号 Socket socket = new Socket(serverIP, serverPort); System.out.println("连接服务器成功,IP地址:" + socket.getInetAddress() + ",端口号:" + socket.getPort()); // 发送请求给服务器 PrintWriter writer = new PrintWriter(socket.getOutputStream()); writer.println("Hello Server"); writer.flush(); // 接收服务器响应 BufferedReader reader = new BufferedReader( new InputStreamReader(socket.getInputStream())); String serverMsg = reader.readLine(); System.out.println("接收到服务器响应:" + serverMsg); reader.close(); writer.close(); socket.close(); } }
In the above code, After the server receives the client's message, it sends the server's response to the client through the println() method of the PrintWriter class. The client receives the server's response message through the readLine() method of the BufferedReader class. After sending and receiving messages, you need to close the input and output streams and Socket objects respectively.
Through the above code examples, we can easily implement network communication using network programming functions in Java. Whether creating a server or a client, it can be done through the classes and methods provided by Java. In actual development, network communication functions can be further expanded and optimized according to specific needs. Network communication is used in a wide range of applications, such as chat applications, remote control, etc., and plays an important role in various fields. I hope this article can help you learn and use Java network programming functions!
The above is the detailed content of How to use network programming functions in Java for network communication. For more information, please follow other related articles on the PHP Chinese website!