Netty server demo

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 控制台

telnet image

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)

五、参考

  1. http://www.infoq.com/cn/articles/netty-high-performance
  2. https://my.oschina.net/plucury/blog/192577

转载于:https://my.oschina.net/mickelfeng/blog/1545410

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值