服务端代码:
package com.my.test.netty;
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;
/**
* 服务端
*
* @author liugaofeng
* @date 2017年9月21日
*/
public class TimeServer {
public void bind(int port) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workGroup = new NioEventLoopGroup();
try {
ServerBootstrap sb = new ServerBootstrap();
sb.group(bossGroup, workGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 1024)
.childHandler(new ChildChannelHandler());
ChannelFuture f = sb.bind(port).sync();
f.channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
new TimeServer().bind(9000);
}
private class ChildChannelHandler extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel channel) throws Exception {
channel.pipeline().addLast(new TimeServerhandler());
}
}
}
服务端消息处理:
package com.my.test.netty;
import java.util.Date;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
/**
* 处理消息
* @author liugaofeng
* @date 2017年9月21日
*/
public class TimeServerhandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf bb = (ByteBuf) msg;
byte[] by = new byte[bb.readableBytes()];
bb.readBytes(by);
String str = new String(by, "UTF-8");
System.out.println("Received Order:" + str);
if ("TIME".equals(str)) {
String now = new Date(System.currentTimeMillis()).toString();
ByteBuf sendBuf = Unpooled.copiedBuffer(now.getBytes());
ctx.write(sendBuf);
} else {
String error = "ERROR ORDER!";
ByteBuf sendBuf = Unpooled.copiedBuffer(error.getBytes());
ctx.write(sendBuf);
}
super.channelRead(ctx, msg);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}
客户端代码:
package com.my.test.netty;
import io.netty.bootstrap.Bootstrap;
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;
public class TimeClient {
public void connect(String host, int port, String order) throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap boot = new Bootstrap();
boot.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel arg0) throws Exception {
arg0.pipeline().addLast(new TimeClientHandler(order));
}
});
ChannelFuture f = boot.connect(host, port).sync();
f.channel().closeFuture().sync();
} finally {
group.shutdownGracefully();
}
}
}
客户端:
package com.my.test.netty;
public class Client2 {
public static void main(String[] args) throws Exception {
new TimeClient().connect("127.0.0.1", 9000, "TIME");
}
}
netty实现TimeServer服务
最新推荐文章于 2019-07-11 17:40:54 发布