├── pom.xml
├── tcp_client
├── .gitignore
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── tcp
│ │ ├── TcpClientApplication.java
│ │ ├── controller
│ │ └── HandController.java
│ │ └── server
│ │ ├── TcpClient.java
│ │ └── TcpClientHandler.java
│ └── resources
│ └── application.yml
└── tcp_server
├── .gitignore
├── pom.xml
└── src
└── main
├── java
└── com
│ └── tcp
│ ├── TcpServerApplication.java
│ └── server
│ ├── TcpServer.java
│ └── TcpServerHandler.java
└── resources
└── application.yml
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.springframework.boot
7 | spring-boot-starter-parent
8 | 2.1.5.RELEASE
9 |
10 |
11 | com.tcp
12 | tcp_project
13 | 0.0.1-SNAPSHOT
14 | tcp_project
15 | Demo project for Spring Boot
16 | pom
17 |
18 |
19 | tcp_server
20 | tcp_client
21 |
22 |
23 |
24 | UTF-8
25 | UTF-8
26 | 1.8
27 |
28 |
29 |
30 |
31 | org.springframework.boot
32 | spring-boot-starter
33 |
34 |
35 |
36 | org.springframework.boot
37 | spring-boot-starter-web
38 |
39 |
40 |
41 | io.netty
42 | netty-all
43 | 4.1.4.Final
44 |
45 |
46 |
47 | org.springframework.boot
48 | spring-boot-starter-amqp
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 | org.springframework.boot
58 | spring-boot-maven-plugin
59 |
60 |
61 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/tcp_client/.gitignore:
--------------------------------------------------------------------------------
1 | HELP.md
2 | /target/
3 | !.mvn/wrapper/maven-wrapper.jar
4 |
5 | ### STS ###
6 | .apt_generated
7 | .classpath
8 | .factorypath
9 | .project
10 | .settings
11 | .springBeans
12 | .sts4-cache
13 |
14 | ### IntelliJ IDEA ###
15 | .idea
16 | *.iws
17 | *.iml
18 | *.ipr
19 |
20 | ### NetBeans ###
21 | /nbproject/private/
22 | /nbbuild/
23 | /dist/
24 | /nbdist/
25 | /.nb-gradle/
26 | /build/
27 |
28 | ### VS Code ###
29 | .vscode/
30 |
--------------------------------------------------------------------------------
/tcp_client/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | com.tcp
7 | tcp_project
8 | 0.0.1-SNAPSHOT
9 |
10 |
11 | com.tcp
12 | tcp_client
13 | 0.0.1-SNAPSHOT
14 | tcp_client
15 | Demo project for Spring Boot
16 | jar
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 | org.springframework.boot
25 | spring-boot-maven-plugin
26 |
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/tcp_client/src/main/java/com/tcp/TcpClientApplication.java:
--------------------------------------------------------------------------------
1 | package com.tcp;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class TcpClientApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(TcpClientApplication.class, args);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/tcp_client/src/main/java/com/tcp/controller/HandController.java:
--------------------------------------------------------------------------------
1 | package com.tcp.controller;
2 |
3 | import com.tcp.server.TcpClient;
4 | import org.springframework.beans.factory.annotation.Autowired;
5 | import org.springframework.web.bind.annotation.GetMapping;
6 | import org.springframework.web.bind.annotation.RestController;
7 |
8 | @RestController
9 | public class HandController {
10 |
11 | @Autowired
12 | private TcpClient tcpClient;
13 |
14 | @GetMapping("/sent")
15 | public String sentMsg(){
16 | try {
17 | tcpClient.sendMsg("1111");
18 | } catch (Exception e) {
19 | return e.toString();
20 | }
21 | return "1111";
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/tcp_client/src/main/java/com/tcp/server/TcpClient.java:
--------------------------------------------------------------------------------
1 | package com.tcp.server;
2 |
3 | import io.netty.bootstrap.Bootstrap;
4 | import io.netty.channel.*;
5 | import io.netty.channel.nio.NioEventLoopGroup;
6 | import io.netty.channel.socket.nio.NioSocketChannel;
7 | import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
8 | import io.netty.handler.codec.LengthFieldPrepender;
9 | import io.netty.handler.codec.string.StringDecoder;
10 | import io.netty.handler.codec.string.StringEncoder;
11 | import io.netty.util.CharsetUtil;
12 | import org.springframework.stereotype.Component;
13 |
14 | @Component
15 | public class TcpClient {
16 |
17 | public static String HOST = "127.0.0.1";
18 | public static int PORT = 9999;
19 |
20 | public static Bootstrap bootstrap = getBootstrap();
21 | public static Channel channel = getChannel(HOST, PORT);
22 |
23 | /**
24 | * 初始化Bootstrap
25 | */
26 | public static final Bootstrap getBootstrap() {
27 | EventLoopGroup group = new NioEventLoopGroup();
28 | Bootstrap b = new Bootstrap();
29 | b.group(group).channel(NioSocketChannel.class);
30 | b.handler(new ChannelInitializer() {
31 | @Override
32 | protected void initChannel(Channel ch) throws Exception {
33 | ChannelPipeline pipeline = ch.pipeline();
34 | pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
35 | pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
36 | pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
37 | pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
38 | pipeline.addLast("handler", new TcpClientHandler());
39 | }
40 | });
41 |
42 | b.option(ChannelOption.SO_KEEPALIVE, true);
43 | return b;
44 | }
45 |
46 | public static final Channel getChannel(String host, int port) {
47 | Channel channel = null;
48 | try {
49 | channel = bootstrap.connect(host, port).sync().channel();
50 | System.out.println("TcpClient启动成功");
51 | } catch (Exception e) {
52 | System.out.println("连接Server失败" + e);
53 | return null;
54 | }
55 | return channel;
56 | }
57 |
58 | public void sendMsg(String msg) throws Exception {
59 | if (channel != null) {
60 | channel.writeAndFlush(msg).sync();
61 | } else {
62 | System.out.println("消息发送失败,连接尚未建立!");
63 | }
64 | }
65 |
66 | /*public static void main(String[] args) throws Exception {
67 | try {
68 | long t0 = System.nanoTime();
69 | for (int i = 0; i < 100; i++) {
70 | TcpClient.sendMsg(i + "你好1");
71 | }
72 | long t1 = System.nanoTime();
73 | System.out.println("time used:" + (t1 - t0));
74 | } catch (Exception e) {
75 | System.out.println("main err:" + e);
76 | }
77 | }*/
78 | }
79 |
--------------------------------------------------------------------------------
/tcp_client/src/main/java/com/tcp/server/TcpClientHandler.java:
--------------------------------------------------------------------------------
1 | package com.tcp.server;
2 |
3 | import io.netty.channel.ChannelHandlerContext;
4 | import io.netty.channel.SimpleChannelInboundHandler;
5 |
6 | public class TcpClientHandler extends SimpleChannelInboundHandler