1、pom.xml
注:最好引用最高版本,不然会导致ChannelHandlerAdapter的Override方法无效
<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>5.0.0.Alpha2</version>
</dependency>
</dependencies>
2、客户端
TimeClient、TimeClientHandler
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;
public class TimeClientHandler extends ChannelHandlerAdapter {
//private byte[] req;
private String req;
/*public TimeClientHandler(){
req="$tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00$".getBytes();
}*/
public TimeClientHandler(String sendMsg){
//req=sendMsg.getBytes();
req=sendMsg;
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception{
ByteBuf message=null;
for(int i=0;i<100;i++){
message=Unpooled.buffer(req.getBytes().length);
message.writeBytes(("第"+String.valueOf(i)+" 次:"+req).getBytes());
ctx.writeAndFlush(message);
}
}
@Override
public void channelRead(ChannelHandlerContext ctx,Object msg) {
try{
ByteBuf in=(ByteBuf)msg;
System.out.println(in.toString(CharsetUtil.UTF_8));
}finally {
ReferenceCountUtil.release(msg);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx,Throwable cause) throws Exception{
cause.printStackTrace();
ctx.close();
}
}
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
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.NioSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
public class TimeClient {
public void connetc(int port,String host) throws Exception{
System.out.println(port+"---"+port);
EventLoopGroup eventLoopGroup=new NioEventLoopGroup();
try {
Bootstrap b=new Bootstrap();
b=b.group(eventLoopGroup).channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY,true).
handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception{
ByteBuf byteBuf=Unpooled.copiedBuffer("$".getBytes());
socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,byteBuf));
socketChannel.pipeline().addLast(new TimeClientHandler(""));
}
});
ChannelFuture f=b.connect(host,port).sync();
f.channel().closeFuture().sync();
}finally {
eventLoopGroup.shutdownGracefully();
}
}
public void connetc(int port,String host,String sendMsg) throws Exception{
System.out.println(port+"---"+port);
EventLoopGroup eventLoopGroup=new NioEventLoopGroup();
try {
Bootstrap b=new Bootstrap();
b=b.group(eventLoopGroup).channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY,true).
handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception{
ByteBuf byteBuf=Unpooled.copiedBuffer("$".getBytes());
socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,byteBuf));
socketChannel.pipeline().addLast(new TimeClientHandler(sendMsg));
}
});
ChannelFuture f=b.connect(host,port).sync();
f.channel().closeFuture().sync();
}finally {
eventLoopGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception{
//new TimeClient().connetc(8089,"localhost");
String content="我客户端向服务端发送的信息【年低准备过春节了 年低准备过春节了】";
String sendMsg="$ from client port 8090 "+content+" $";
new TimeClient().connetc(8090,"localhost",sendMsg);
}
}
3、服务端
DiscardServer 、ChildChannelHandler DiscardServerHandler
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public class DiscardServer {
public void run(int port) throws Exception{
EventLoopGroup bossGroup=new NioEventLoopGroup();
EventLoopGroup workerGroup=new NioEventLoopGroup();
System.out.println("准备运行端口:"+port);
try {
ServerBootstrap b=new ServerBootstrap();
b=b.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG,128).
childHandler(new ChildChannelHandler());
ChannelFuture f=b.bind(port).sync();
f.channel().closeFuture().sync();
}finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception{
new DiscardServer().run(8090);
}
}
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
public class ChildChannelHandler extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ByteBuf byteBuf=Unpooled.copiedBuffer("$".getBytes());
socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,byteBuf));
socketChannel.pipeline().addLast(new DiscardServerHandler());
}
}
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;
public class DiscardServerHandler extends ChannelHandlerAdapter {
int i=0;
@Override
public void channelRead(ChannelHandlerContext ctx,Object msg){
try{
ByteBuf in=(ByteBuf)msg;
System.out.println("收到你的客户端内容是");
System.out.println(in.toString(CharsetUtil.UTF_8));
ByteBuf resp=Unpooled.copiedBuffer(("【"+String.valueOf(i)+"】已收到你的客户端信息,谢谢$").getBytes());
ctx.writeAndFlush(resp);
}finally {
ReferenceCountUtil.release(msg);
i++;
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx,Throwable cause){
cause.printStackTrace();
ctx.close();
}
}
4、传输内容
转:https://www.cnblogs.com/guoyuchuan/p/9549672.html