实现TCP多次握手的Java Spring Boot示例涉及几个关键部分:服务端和客户端。以下是一个简单的示例,包括TCP连接的建立、数据传输和连接关闭。示例代码包括服务端和客户端两个部分。
1. 服务端实现
首先,我们创建一个简单的Spring Boot应用程序来实现TCP服务器。
Maven依赖
在pom.xml
中添加Spring Boot Starter依赖:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies>
服务端代码
创建一个Spring Boot应用程序类和一个TCP服务类:
package com.example.tcpserver; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; @SpringBootApplication public class TcpServerApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(TcpServerApplication.class, args); } @Override public void run(String... args) throws Exception { int port = 9090; try (ServerSocket serverSocket = new ServerSocket(port)) { System.out.println("Server is listening on port " + port); while (true) { Socket socket = serverSocket.accept(); new ServerThread(socket).start(); } } catch (IOException ex) { System.out.println("Server exception: " + ex.getMessage()); ex.printStackTrace(); } } }
ServerThread类
为了处理每个客户端连接,我们创建一个ServerThread类:
package com.example.tcpserver; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; public class ServerThread extends Thread { private Socket socket; public ServerThread(Socket socket) { this.socket = socket; } public void run() { try (BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter output = new PrintWriter(socket.getOutputStream(), true)) { String text; while ((text = input.readLine()) != null) { System.out.println("Received: " + text); output.println("Server: " + text); } } catch (IOException ex) { System.out.println("Server exception: " + ex.getMessage()); ex.printStackTrace(); } finally { try { socket.close(); } catch (IOException ex) { ex.printStackTrace(); } } } }
2. 客户端实现
现在我们实现一个简单的TCP客户端来连接到我们的服务器。
客户端代码
创建一个Spring Boot应用程序类和一个TCP客户端类:
package com.example.tcpclient; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.util.Scanner; @SpringBootApplication public class TcpClientApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(TcpClientApplication.class, args); } @Override public void run(String... args) throws Exception { String hostname = "localhost"; int port = 9090; try (Socket socket = new Socket(hostname, port)) { PrintWriter output = new PrintWriter(socket.getOutputStream(), true); BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); Scanner scanner = new Scanner(System.in); String text; do { System.out.print("Enter message: "); text = scanner.nextLine(); output.println(text); String response = input.readLine(); System.out.println(response); } while (!text.equals("bye")); } catch (IOException ex) { System.out.println("Client exception: " + ex.getMessage()); ex.printStackTrace(); } } }
3. 运行程序
编写好代码后,分别运行TcpServerApplication
和TcpClientApplication
类。客户端将连接到服务器并可以发送消息,服务器会回显接收到的消息。
结论
上述代码展示了如何使用Java Spring Boot实现一个简单的TCP服务器和客户端。客户端和服务器通过TCP连接进行通信,演示了TCP握手的基本原理。实际应用中,可以在此基础上进行扩展,如添加异常处理、日志记录、配置管理等功能,以满足特定需求。