Netty: Socket: a demo
DTO
TransportObject.java
package com.me.dto;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class TransportObject implements Serializable {
private String name;
private int id;
private List<String> list = new ArrayList<String>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
@Override
public String toString() {
return "{" +
"name='" + name + '\'' +
", id=" + id +
", list=" + list +
'}';
}
}
First Server
FirstClient.java
package com.me.socket.client;
import com.me.dto.TransportObject;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.util.CharsetUtil;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
public class FirstClient {
private final String host;
private final int port;
public FirstClientHandler cl=new FirstClientHandler();
public FirstClient() {
this(0);
}
public FirstClient(int port) {
this("localhost", port);
}
public FirstClient(String host, int port) {
this.host = host;
this.port = port;
}
public void connect(String msg) throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.remoteAddress(new InetSocketAddress(this.host, this.port))
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
System.out.println("--------------------client: channel log begin--------------------");
System.out.println("正在连接中...");
ch.pipeline().addLast(new StringEncoder(Charset.forName("GBK")));
ch.pipeline().addLast(cl);
ch.pipeline().addLast(new ByteArrayEncoder());
ch.pipeline().addLast(new ChunkedWriteHandler());
}
});
ChannelFuture cf = b.connect().sync();
System.out.println("服务端连接成功...");
cl.myctx.writeAndFlush(Unpooled.copiedBuffer(msg, CharsetUtil.UTF_8));
cf.channel().closeFuture().sync();
System.out.println("连接已关闭..");
} finally {
group.shutdownGracefully().sync();
}
}
public static void main(String[] args) throws Exception {
TransportObject data = new TransportObject();
data.setId(999);
data.setName("xyz");
List<String> list = new ArrayList<String>();
list.add("111");
list.add("222");
list.add("333");
data.setList(list);
FirstClient firstClient = new FirstClient("127.0.0.1", 8882);
firstClient.connect(data.toString());
System.out.println("=========================");
}
}
FirstClientHandler.java
package com.me.socket.client;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import java.nio.charset.Charset;
public class FirstClientHandler extends SimpleChannelInboundHandler<ByteBuf> {
public ChannelHandlerContext myctx;
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
myctx=ctx;
System.out.println("客户端与服务端通道-开启:" + ctx.channel().localAddress() + "channelActive");
}
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println("客户端与服务端通道-关闭:" + ctx.channel().localAddress() + "channelInactive");
System.out.println("--------------------client: channel log end--------------------");
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
System.out.println("读取客户端通道信息..");
ByteBuf buf = msg.readBytes(msg.readableBytes());
System.out.println(
"客户端接收到的服务端信息:" + ByteBufUtil.hexDump(buf) + "; 数据包为:" + buf.toString(Charset.forName("utf-8")));
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
System.out.println("异常退出:" + cause.getMessage());
}
}
FirstServer.java
package com.me.socket.server;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture