1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public class MyClientHandler extends SimpleChannelInboundHandler<String> { @Override protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { System.out.println(ctx.channel().remoteAddress()); System.out.println("client ouput: " + msg); ctx.channel().writeAndFlush("from client:"+ LocalDateTime.now()); }
@Override public void channelActive(ChannelHandlerContext ctx) throws Exception { ctx.writeAndFlush("来自于客户端的问候!"); }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); ctx.close(); } }
|