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;
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.bytes.ByteArrayEncoder;
import io.netty.handler.codec.string.StringEncoder;
import java.nio.charset.Charset;
public class FirstServer {
private final int port;
public FirstServer(int port) {
this.port = port;
}
public void connect() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup group = new NioEventLoopGroup();
try {
ServerBootstrap sb = new ServerBootstrap();
sb.option(ChannelOption.SO_BACKLOG, 1024);
sb.group(group, bossGroup)
.channel(NioServerSocketChannel.class)
.localAddress(this.port)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
System.out.println("--------------------server: channel log begin--------------------");
System.out.println("报告");
System.out.println("信息:有一客户端链接到本服务端");
System.out.println("IP:" + ch.remoteAddress().getHostName());
System.out.println("Port:" + ch.remoteAddress().getPort());
System.out.println("报告完毕");
ch.pipeline().addLast(new StringEncoder(Charset.forName("GBK")));
ch.pipeline().addLast(new FirstServerHandler());
ch.pipeline().addLast(new ByteArrayEncoder());
}
});
ChannelFuture cf = sb.bind().sync();
System.out.println(FirstServer.class + " 启动正在监听: " + cf.channel().localAddress());
cf.channel().closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
bossGroup.shutdownGracefully().sync();
}
}
public static void main(String[] args) throws Exception {
new FirstServer(8881).connect();
}
}
FirstServerHandler.java
package com.me.socket.server;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.io.UnsupportedEncodingException;
public class FirstServerHandler extends ChannelInboundHandlerAdapter {
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println(ctx.channel().localAddress().toString() + " 通道已激活!");
}
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println(ctx.channel().localAddress().toString() + " 通道不活跃!");
System.out.println("--------------------server: channel log end--------------------");
}
private String getMessage(ByteBuf buf) {
byte[] con = new byte[buf.readableBytes()];
buf.readBytes(con);
try {
return new String(con, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
String rev = getMessage(buf);
System.out.println("客户端收到服务器数据:" + rev);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println("服务端接收数据完毕..");
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
System.out.println("异常信息:\r\n" + cause.getMessage());
}
}
Second Server
SecondClient.java
package com.me.socket.client;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
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.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;
public class SecondClient {
private final String host;
private final int port;
public SecondClientHandler cl=new SecondClientHandler();
public SecondClient() {
this(0);
}
public SecondClient(int port) {
this("localhost", port);
}
public SecondClient(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 {
new SecondClient("127.0.0.1", 8888).connect("second msg");
}
}
SecondClientHandler.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 SecondClientHandler 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());
}
}
SecondServer.java
package com.me.socket.server;
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.bytes.ByteArrayEncoder;
import io.netty.handler.codec.string.StringEncoder;
import java.nio.charset.Charset;
public class SecondServer {
private final int port;
public SecondServer(int port) {
this.port = port;
}
public void start() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup group = new NioEventLoopGroup();
try {
ServerBootstrap sb = new ServerBootstrap();
sb.option(ChannelOption.SO_BACKLOG, 1024);
sb.group(group, bossGroup)
.channel(NioServerSocketChannel.class)
.localAddress(this.port)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
System.out.println("--------------------server: channel log begin--------------------");
System.out.println("报告");
System.out.println("信息:有一客户端链接到本服务端");
System.out.println("IP:" + ch.remoteAddress().getHostName());
System.out.println("Port:" + ch.remoteAddress().getPort());
System.out.println("报告完毕");
ch.pipeline().addLast(new StringEncoder(Charset.forName("GBK")));
ch.pipeline().addLast(new SecondServerHandler());
ch.pipeline().addLast(new ByteArrayEncoder());
}
});
ChannelFuture cf = sb.bind().sync();
System.out.println(SecondServer.class + " 启动正在监听: " + cf.channel().localAddress());
cf.channel().closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
bossGroup.shutdownGracefully().sync();
}
}
public static void main(String[] args) throws Exception {
new SecondServer(8882).start();
}
}
SecondServerHandler.java
package com.me.socket.server;
import com.me.dto.TransportObject;
import com.me.socket.client.SecondClient;
import com.google.gson.Gson;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.io.UnsupportedEncodingException;
public class SecondServerHandler extends ChannelInboundHandlerAdapter {
private String rev;
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println(ctx.channel().localAddress().toString() + " 通道已激活!");
}
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println(ctx.channel().localAddress().toString() + " 通道不活跃!");
System.out.println("--------------------server: channel log end--------------------");
}
private String getMessage(ByteBuf buf) {
byte[] con = new byte[buf.readableBytes()];
buf.readBytes(con);
try {
return new String(con, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
rev = getMessage(buf);
System.out.println("second-server收到first-server发送的数据:" + rev);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println("服务端接收数据完毕..");
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
TransportObject transportObject = new Gson().fromJson(rev, TransportObject.class);
transportObject.getList().add("444");
transportObject.setName("当前位置second");
transportObject.setId(888);
SecondClient secondClient = new SecondClient("127.0.0.1", 8883);
secondClient.connect(transportObject.toString()+" | second msg");
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
System.out.println("异常信息:\r\n" + cause.getMessage());
}
}
Third Server
ThirdClient.java
package com.me.socket.client;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
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.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;
public class ThirdClient {
private final String host;
private final int port;
public ThirdClientHandler cl=new ThirdClientHandler();
public ThirdClient() {
this(0);
}
public ThirdClient(int port) {
this("localhost", port);
}
public ThirdClient(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 {
new ThirdClient("127.0.0.1", 8888).connect("aaa");
}
}
ThirdClientHandler.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 ThirdClientHandler 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());
}
}
ThirdServer.java
package com.me.socket.server;
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.bytes.ByteArrayEncoder;
import io.netty.handler.codec.string.StringEncoder;
import java.nio.charset.Charset;
public class ThirdServer {
private final int port;
public ThirdServer(int port) {
this.port = port;
}
public void start() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup group = new NioEventLoopGroup();
try {
ServerBootstrap sb = new ServerBootstrap();
sb.option(ChannelOption.SO_BACKLOG, 1024);
sb.group(group, bossGroup)
.channel(NioServerSocketChannel.class)
.localAddress(this.port)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
System.out.println("--------------------server: channel log begin--------------------");
System.out.println("报告");
System.out.println("信息:有一客户端链接到本服务端");
System.out.println("IP:" + ch.remoteAddress().getHostName());
System.out.println("Port:" + ch.remoteAddress().getPort());
System.out.println("报告完毕");
ch.pipeline().addLast(new StringEncoder(Charset.forName("GBK")));
ch.pipeline().addLast(new ThirdServerHandler());
ch.pipeline().addLast(new ByteArrayEncoder());
}
});
ChannelFuture cf = sb.bind().sync();
System.out.println(ThirdServer.class + " 启动正在监听: " + cf.channel().localAddress());
cf.channel().closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
bossGroup.shutdownGracefully().sync();
}
}
public static void main(String[] args) throws Exception {
new ThirdServer(8883).start();
}
}
ThirdServerHandler.java
package com.me.socket.server;
import com.me.socket.client.ThirdClient;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.io.UnsupportedEncodingException;
public class ThirdServerHandler extends ChannelInboundHandlerAdapter {
private String rev;
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println(ctx.channel().localAddress().toString() + " 通道已激活!");
}
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println(ctx.channel().localAddress().toString() + " 通道不活跃!");
System.out.println("--------------------server: channel log end--------------------");
}
private String getMessage(ByteBuf buf) {
byte[] con = new byte[buf.readableBytes()];
buf.readBytes(con);
try {
return new String(con, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
rev = getMessage(buf);
System.out.println("third-server收到second-server发送的数据:" + rev);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println("服务端接收数据完毕..");
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
System.out.println("异常信息:\r\n" + cause.getMessage());
}
}
Demo操作流程
1. 启动ThirdServer
2. 启动SecondServer
3. 启动FirstClient
输出结果
FirstClient
--------------------client: channel log begin--------------------
正在连接中...
服务端连接成功...
客户端与服务端通道-开启:/127.0.0.1:54088channelActive
连接已关闭..
客户端与服务端通道-关闭:/127.0.0.1:54088channelInactive
--------------------client: channel log end--------------------
=========================
SecondServer
class com.me.socket.server.SecondServer 启动正在监听: /0:0:0:0:0:0:0:0:8882
--------------------server: channel log begin--------------------
报告
信息:有一客户端链接到本服务端
IP:127.0.0.1
Port:54088
报告完毕
/127.0.0.1:8882 通道已激活!
second-server收到first-server发送的数据:{name='xyz', id=999, list=[111, 222, 333]}
服务端接收数据完毕..
--------------------client: channel log begin--------------------
正在连接中...
客户端与服务端通道-开启:/127.0.0.1:54113channelActive
服务端连接成功...
连接已关闭..
客户端与服务端通道-关闭:/127.0.0.1:54113channelInactive
--------------------client: channel log end--------------------
/127.0.0.1:8882 通道不活跃!
--------------------server: channel log end--------------------
ThirdServer
class com.me.socket.server.ThirdServer 启动正在监听: /0:0:0:0:0:0:0:0:8883
--------------------server: channel log begin--------------------
报告
信息:有一客户端链接到本服务端
IP:127.0.0.1
Port:54113
报告完毕
/127.0.0.1:8883 通道已激活!
third-server收到second-server发送的数据:{name='当前位置second', id=888, list=[111, 222, 333, 444]} | second msg
服务端接收数据完毕..
/127.0.0.1:8883 通道不活跃!
--------------------server: channel log end--------------------
源码地址
Netty socket demo 源码地址
GitHub 地址