Netty实现tcp通讯(telent发送消息)

前言

参考Netty官网文档的例子,加了编码器、解码器。
实现了简单的tcp通讯。
代码很简单,最后我们会用Linux下的Telnet命令连接到服务端。并且给服务端发送消息。

代码

tcp服务端

package netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import sun.rmi.runtime.Log;

import java.util.logging.Logger;

public class DiscardServer {
   
    private static final Logger LOG = Logger.getLogger(DiscardServerHandler.class.getName());

    
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 Spring Boot 整合 Netty 实现 TCP 发送消息的完整代码: 首先,需要在 pom.xml 中添加对 Netty 的依赖: ```xml <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.53.Final</version> </dependency> ``` 然后,创建一个 Netty 服务端: ```java @Component public class NettyServer { private final EventLoopGroup bossGroup = new NioEventLoopGroup(); private final EventLoopGroup workerGroup = new NioEventLoopGroup(); @Value("${netty.port}") private int port; @PostConstruct public void start() throws Exception { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) { ch.pipeline().addLast(new StringDecoder()); ch.pipeline().addLast(new StringEncoder()); ch.pipeline().addLast(new NettyServerHandler()); } }); ChannelFuture future = bootstrap.bind(port).sync(); if (future.isSuccess()) { System.out.println("Netty server started on port " + port); } } @PreDestroy public void stop() throws Exception { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } ``` 其中,NettyServerHandler 是自定义的处理器,用于处理接收到的消息。 接下来,创建一个 Netty 客户端: ```java @Component public class NettyClient { private final EventLoopGroup group = new NioEventLoopGroup(); @Value("${netty.host}") private String host; @Value("${netty.port}") private int port; public void send(String message) throws Exception { Bootstrap bootstrap = new Bootstrap(); bootstrap.group(group) .channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) { ch.pipeline().addLast(new StringEncoder()); } }); ChannelFuture future = bootstrap.connect(host, port).sync(); if (future.isSuccess()) { Channel channel = future.channel(); channel.writeAndFlush(message); channel.closeFuture().sync(); } } @PreDestroy public void stop() throws Exception { group.shutdownGracefully(); } } ``` 最后,在 application.yml 中配置 Netty 的端口和主机: ```yaml netty: port: 8080 host: localhost ``` 然后就可以在需要发送消息的地方注入 NettyClient,并调用 send 方法发送消息了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值