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

本文档介绍如何基于Netty构建TCP服务器,实现简单的通讯功能。通过编码器和解码器增强通信能力,并展示了如何在Linux环境下使用telnet连接服务器并发送消息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

参考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());

    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值