Netty server demo
IntelliJ IDEA 导入Netty
以前没用过IDEA,因此有些生疏,记录一下:
另外我是在Ubuntu下装的IDEA,仔细想想这种IDE好像用什么系统都没关系。。
1.Netty观望瞎下载他们的jar包,我下载的版本是netty-4.1.11.final
2.下载完之后解压,有个文件夹叫做all-in-one,只用导入里面两个jar就可以了
3.file->project structure->Modules->dependencies->”+”->jars or directory->找到all-in-one目录即可。
参考:
http://zyjustin9.iteye.com/blog/2172445
一、简介
Netty是一个高性能、异步事件驱动的NIO框架,它提供了对TCP、UDP和文件传输的支持,作为一个异步NIO框架,Netty的所有IO操作都是异步非阻塞的,通过Future-Listener机制,用户可以方便的主动获取或者通过通知机制获得IO操作结果
二、maven依赖
<!-- https://mvnrepository.com/artifact/io.netty/netty-transport -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-transport</artifactId>
<version>4.1.6.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.netty/netty-handler -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-handler</artifactId>
<version>4.1.6.Final</version>
</dependency>
三、示列代码
package netty;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
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.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import java.util.concurrent.ExecutionException;
/**
* Created by zhengyong on 16/10/21.
*/
public class NettyServer {
private static ServerBootstrap bootstrap;
public static void main(String[] args) throws InterruptedException, ExecutionException {
EventLoopGroup boss = new NioEventLoopGroup(1);
EventLoopGroup worker = new NioEventLoopGroup();
bootstrap = new ServerBootstrap();
bootstrap.group(boss,worker).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(new ConsoleHandler());
}
});
// Start the client.
ChannelFuture ch = bootstrap.bind(8082).sync();
System.out.println("start server success, you can telnet 127.0.0.1 8082 to send massage for this server");
// Wait until the connection is closed.
ch.channel().closeFuture().sync();
}
}
package netty;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.io.UnsupportedEncodingException;
/**
* Created by zhengyong on 16/10/21.
*/
public class ConsoleHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws UnsupportedEncodingException {
System.out.println(msg);
ctx.write(msg);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.flush();
}
}
四、运行结果
telnet 控制台
idea控制台
start server success, you can telnet 127.0.0.1 8082 to send massage for this server
PooledUnsafeDirectByteBuf(ridx: 0, widx: 7, cap: 1024)
PooledUnsafeDirectByteBuf(ridx: 0, widx: 7, cap: 1024)
五、参考