使用telnet连接netty:发送定长字符串
//1、启动Server,输入telnet命令:telnet localhost 8088
public class NettyServer {
private int port=8088;
public static void main(String[] args) {
NettyServer server = new NettyServer();
server.run();
}
void run() {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
public void initChannel(SocketChannel ch) {
ChannelPipeline p = ch.pipeline();
//客户端发送给服务端:hello server,im a client这符串,
//该字符串占用24字节,所以在服务端channelpipeline里面添加一个长度
//为24的定长解码器和二进制转换为string的解码器
p.addLast(new FixedLengthFrameDecoder(24));
p.addLast(new StringDecoder()); //与SimpleChannelInboundHandler<String> 泛型对应
p.addLast(new ServerHandler2());
}}
).option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = b.bind(port).sync();
f.channel().closeFuture().sync();
}catch(Exception e) {
e.printStackTrace();
}finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
public class ServerHandler2 extends SimpleChannelInboundHandler<String> {
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println("from client: "+msg + msg.getClass());
String sendContent="hello client,im server";
ByteBuf sendMsg = Unpooled.buffer(sendContent.length());
sendMsg.writeBytes(sendContent.getBytes());
ctx.writeAndFlush(Unpooled.copiedBuffer(sendMsg));
}
}
客户端:
public class NettyClient {
private String host="localhost";
private int port = 8088;
public static void main(String[] args) {
new NettyClient().run();
}
public void run() {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
//.remoteAddress(new InetSocketAddress(host, port))
.handler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel ch)throws Exception {
ChannelPipeline p=ch.pipeline();
//服务端发送:hello client,im server字符串,占用22字节
p.addLast(new FixedLengthFrameDecoder(22)); //缓冲区达到22字节,才写入通道
p.addLast(new StringDecoder());
p.addLast("handler",new ClientHandler2());
}
});
ChannelFuture f = b.connect(host,port).sync();
f.channel().closeFuture().sync();
}catch(Exception e) {
e.printStackTrace();
}
finally {
try {
group.shutdownGracefully().sync();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class ClientHandler2 extends SimpleChannelInboundHandler<String>{
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println("client channelRead0.."+msg);
}
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("client channelActive..");
ctx.writeAndFlush(Unpooled.copiedBuffer("hello server,im a client".getBytes()));
}
}
分享到:
相关推荐
netty分隔符和定长解码器的应用
Netty发送protoBuf格式数据 Netty发送protoBuf格式数据 Netty发送protoBuf格式数据 Netty发送protoBuf格式数据
通过Netty4 获取串口数据并且下发数据到串口,是一个封装不错的框架
java netty接收串口数据 开启windows串口工具 发送串口数据调试助手
使用netty实现TCP长链接消息写入kafka以及kafka批量消费数据,数据可以批量进行操作
netty服务器解析16进制数据
netty案例,netty4.1基础入门篇三《NettyServer字符串解码器》源码 https://mp.weixin.qq.com/s?__biz=MzIxMDAwMDAxMw==&mid=2650724760&idx=1&sn=4e5faf45de87e2c7346e9053628b51b0&scene=19#wechat_redirect
添加io.netty的maven库, 在com.zhao的包下,可以自行修改 有较多的注释,可以学习使用
物联网通讯协议,基于netty框架,支持COM(串口)和TCP协议,同时支持设备组多台设备高并发通讯。设计上采用工厂设计模式,代码采用继承和重写的方式实现高度封装,可作为SDK提供封装的接口,让具体的业务开发人员...
NULL 博文链接:https://gjp014.iteye.com/blog/2390925
随着物联网的发展,随之出现了各种传感器监测数据的实时发送,需要和netty服务器通讯,netty和传感器之间需要保持长连接(换句话说,netty和gateway之间都会主动给对方发送消息) 碰到的问题: netty作为服务器端如何...
Netty中使用WebSocket实现服务端与客户端的长连接通信发送消息示例代码;Netty中使用WebSocket实现服务端与客户端的长连接通信发送消息示例代码;Netty中使用WebSocket实现服务端与客户端的长连接通信发送消息示例代码
netty案例,netty4.1中级拓展篇十《Netty接收发送多种协议消息类型的通信处理方案》源码 ...
Netty4长连接、断开重连、心跳检测、Msgpack编码解码 http://blog.csdn.net/giousa/article/details/72846303#t2
这个小程序使用netty5进行udp网络通讯,客户端有两种,1:用netty5类库发送DatagramPacket和接收 2:直接使用DatagramSocket发送接收DatagramPacket 先运行netty_server的QuoteOfTheMomentServer, 在运行netty_...
Netty将java对象作为数据的发送与接收 使用:先运行UserServer,在运行UserClient。成功连接后,服务器发送一个User对象给客户端。输出到客户端控制台 5.第四个示例 com.user_1 Netty将java对象作为数据的发送与...
netty的长连接.. 连接断开之后 自动重连demo
实现Java服务端和C#客户端联通 Java使用Netty 开发环境为IDEA C#使用DotNetty 开发环境为VS2017 运行时先开启Java服务端 再开启客户端
0、maven项目 1、需要手动加入依赖RXTXcomm.jar 2、将rxtxParallel.dll,rxtxSerial.dll放到<JAVA_HOME>\jre\bin下 3、运行App.java主程序
在公司做项目的时候发现用Netty进行TCP/IP通信的Netty客户端接收到的数据进制乱码,经过摸索,终于成功解决了这个鸡肋的问题