概述
1.依赖的jar包
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.8.Final</version>
</dependency>
2. 服务端代码
public class NettyServer {
public void start(int port) throws Exception
{
ServerBootstrap strap = new ServerBootstrap();
EventLoopGroup boosGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
strap.group(boosGroup, workerGroup).
channel(NioServerSocketChannel.class).
option(ChannelOption.SO_BACKLOG, 1024).
childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new NettyServerHandler());
}
});
ChannelFuture future=strap.bind(port).sync();
future.channel().closeFuture().sync();
}finally {
boosGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
System.out.println("start server");
new NettyServer().start(8000);
}
}
NettyServerHandler
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf=(ByteBuf)msg;
byte [] data=new byte[buf.readableBytes()];
buf.readBytes(data);
System.out.println(new String(data));
String responseMsg="收到"+System.getProperty("line.separator");
ByteBuf sendMsg= Unpooled.copiedBuffer(responseMsg.getBytes());
ctx.writeAndFlush(sendMsg);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
super.channelReadComplete(ctx);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
super.exceptionCaught(ctx, cause);
}
}
启动服务后,可以使用telnet 127.0.0.1 8000 来测试
3.客户端代码
public class NettyClient {
public void connect(int port,String host) throws Exception
{
EventLoopGroup eventLoopGroup=new NioEventLoopGroup();
Bootstrap bootstrap=new Bootstrap();
try {
bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new NettyClientHandler());
}
});
ChannelFuture future = bootstrap.connect(host, port).sync();
future.channel().closeFuture().sync();
}finally {
eventLoopGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception{
new NettyClient().connect(8000,"127.0.0.1");
}
}
NettyClientHandler
public class NettyClientHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ByteBuf buf = Unpooled.copiedBuffer("hello world".getBytes());
ctx.writeAndFlush(buf);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf=(ByteBuf)msg;
byte[] data=new byte[buf.readableBytes()];
buf.readBytes(data);
System.out.print(new String(data));
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
super.exceptionCaught(ctx, cause);
}
}
最后
以上就是鲜艳面包为你收集整理的netty 入门案例hello world的全部内容,希望文章能够帮你解决netty 入门案例hello world所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复