zoukankan      html  css  js  c++  java
  • netty使用(2)创建一个EchoServer并且部署运行

    1.打开Eclipse,File->New->Java Project,在弹出的对话框中,project name填写XXX,然后点击“finish”

    2.右键该工程,new->Class,在弹出的对话框中,Package随便写一个club.test,Name写myhello

     3.下载netty,解压,选择all-in-one文件夹里面的final那个jar包。

    4.把jar包添加到工程中去,

    方法a:右键工程,new->Folder,命名为比如nettylib,复制netty的jar包,选中nettylib文件夹后,Ctrl+V粘帖。

    右键,这个.jar包,Build path,Add to build path

    方法b:右键工程,Build Path->Build Path Configurations,在弹出的对话框中,选中Libraries选项卡,点击Add External JAR

    5. myhello.java文件代码如下

    package club.test;
    
    
    import io.netty.bootstrap.ServerBootstrap;
    import io.netty.channel.ChannelFuture;
    import io.netty.channel.ChannelInitializer;
    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.buffer.ByteBuf;
    import io.netty.buffer.Unpooled;
    import io.netty.channel.ChannelFutureListener;
    import io.netty.channel.ChannelHandler.Sharable;
    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.ChannelInboundHandlerAdapter;
    import io.netty.util.CharsetUtil;
    
    import java.net.InetSocketAddress;
    
    /**
     * Listing 2.2 EchoServer class
     *
     * @author <a href="mailto:norman.maurer@gmail.com">Norman Maurer</a>
     */
    public class myhello {
        private final int port;
    
        public myhello(int port) {
            this.port = port;
        }
    
        public static void main(String[] args)
            throws Exception {
            if (args.length != 0) {
                System.err.println("Usage: " + myhello.class.getSimpleName() +
                    " <port>"
                );
                return;
            }
            //int port = Integer.parseInt(args[0]);
            new myhello(8888).start();
        }
    
        /**
         * @throws Exception
         */
        public void start() throws Exception {
            final goodHandler serverHandler = new goodHandler();
            EventLoopGroup group = new NioEventLoopGroup();
            try {
                ServerBootstrap b = new ServerBootstrap();
                b.group(group)
                    .channel(NioServerSocketChannel.class)
                    .localAddress(new InetSocketAddress(port))
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(serverHandler);
                        }
                    });
    
                ChannelFuture f = b.bind().sync();
                System.out.println(myhello.class.getName() +
                    " started and listening for connections on " + f.channel().localAddress());
                f.channel().closeFuture().sync();
            } finally {
                group.shutdownGracefully().sync();
            }
        }
    }
    @Sharable
    class goodHandler extends ChannelInboundHandlerAdapter {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) {
            ByteBuf in = (ByteBuf) msg;
            System.out.println(
                    "Server received: " + in.toString(CharsetUtil.US_ASCII));
            ctx.write(in);
        }
    
        @Override
        public void channelReadComplete(ChannelHandlerContext ctx)
                throws Exception {
            ctx.writeAndFlush(Unpooled.EMPTY_BUFFER)
                    .addListener(ChannelFutureListener.CLOSE);
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx,
            Throwable cause) {
            cause.printStackTrace();
            ctx.close();
        }
    }

     如果需要参数,右键工程,run as ==》run configurations

    在arguments,program arguments中输入

     6.导出可运行jar包

    右键工程,export,在弹出对话框中选择java,ruannable JAR  file,

    launch configuration对应本工程,目标文件夹选择好,其他默认。

     控制台中,到当前路径

    java -jar XXX.jar

    如果程序需要参数,直接加在最后。

  • 相关阅读:
    Oracle 查询出来的数据取第一条
    如何将Oracle 当前日期加一天、一分钟
    Oracle 增加修改删除字段
    asp.net,简单权限。存取读XML
    SQL中使用update inner join和delete inner join
    防止浏览器记住用户名及密码的简单实用方法
    vb.net 接口POST方式传参数提交返回值
    导入EXCEL表时,提示"找不到可安装的ISAM"怎么办
    vb.net读取EXCEL
    导入excel错误:外部表不是预期的格式 解决方案
  • 原文地址:https://www.cnblogs.com/legion/p/8257797.html
Copyright © 2011-2022 走看看