1对于服务端,
private void bindPort(int port){
EventLoopGroup workGroup = new NioEventLoopGroup();
EventLoopGroup bossGroup = new NioEventLoopGroup();
try{
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workGroup);
b.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 128)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ObjectDecoder(1024*1024,
Cla***esolvers.weakCachingConcurrentResolver(
this.getClass().getClassLoader())));
ch.pipeline().addLast(new ObjectEncoder());
ch.pipeline().addLast(new ServerReqHandler());
}
});
ChannelFuture channelFuture = b.bind(port).sync();
channelFuture.channel().closeFuture().sync();
}catch(Exception e){
}finally{
workGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
class ServerReqHandler extends ChannelInboundHandlerAdapter{
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
cause.printStackTrace();
ctx.close();
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("channel active");
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)throws Exception {
ServerMsg serverMsg = (ServerMsg) msg;
System.out.println(serverMsg);
}
}
在添加自己的Handler(ServerReqHandler)之前,要添加ObjectDecoder和ObjectEncoder,
自己的Handler在接受到类之后可直接强转,这里注意,被传输的类(ServerMsg)一定要实现Serializable接口(序列化)
2.对于客户端
private void connect(int port){
EventLoopGroup workGrop = new NioEventLoopGroup();
try{
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(workGrop);
bootstrap.channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(new ObjectDecoder(1024*1024,
Cla***esolvers.weakCachingConcurrentResolver(
this.getClass().getClassLoader())));
ch.pipeline().addLast(new ObjectEncoder());
ch.pipeline().addLast(new ClientReqHandler());
}
});
ChannelFuture channelFuture = bootstrap.connect("localhost", port).sync();
channelFuture.channel().closeFuture().sync();
}catch(Exception e){
e.printStackTrace();
}finally{
workGrop.shutdownGracefully();
}
}
class ClientReqHandler extends ChannelInboundHandlerAdapter{
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
for(int i = 0; i < 10; i++){
ServerMsg msg = parseServerMsg(i);
ctx.write(msg);
}
ctx.flush();
}
private ServerMsg parseServerMsg(int id){
ServerMsg msg = new ServerMsg();
msg.setId(id);
msg.setName(id + "");
return msg;
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)throws Exception {
cause.printStackTrace();
ctx.close();
}
}
要添加的Handler与服务端一样,
在自己的Handler(ClientReqHandler)中,发送Msg时直接用ctx
附ServerMsg
public class ServerMsg implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "id: " + id + " name: " + name;
}
}
转载于:https://blog.51cto.com/chpn208/1630524