2017-04-18 8 views
1

私は何時間もNetty javadocsを見てきましたが、これはまだ分かりません。普通のTCP接続を開くにはどうすればいいですか? NettyのIRCサーバーに接続しますか?NettyでTCP接続(クライアント)を開くにはどうすればよいですか?

+0

であるあなたは、網状の一部として提供されている例をチェックしましたか?そのすべてがそこにあります。 –

+0

@NormanMaurer実際、私はjavadocsを読んでいませんでした。 – SoniEx2

答えて

1

you can find many demo in github of netty

サーバーでnetty5を使用しているAssumeing。 デコーダエンコーダをパイプラインに追加する必要があります。ここ

socketChannel.pipeline().addLast(new StringEncoder() ,new StringDecoder() ,new LineBasedFrameDecoder(1024)); 

以下のような は私のサーバーのデモは、あなたが通常のようにそれを接続するために、ブロッキングソケットを使用することができます

String ip ; 
int port = 9999; 

NioEventLoopGroup workGroup = new NioEventLoopGroup(8); 
NioEventLoopGroup bossGroup = new NioEventLoopGroup(); 
try { 
    bootstrap = new ServerBootstrap(); 
    bootstrap.group(bossGroup, workGroup); 
    bootstrap.channel(NioServerSocketChannel.class); 
    bootstrap.option(ChannelOption.SO_BACKLOG, 100); 
    bootstrap.childHandler(new ChannelInitializer<SocketChannel>() { 
     @Override 
     protected void initChannel(SocketChannel socketChannel) throws Exception { 
      socketChannel.pipeline().addLast(new StringEncoder() ,new StringDecoder() ,new LineBasedFrameDecoder(1024)); 
      socketChannel.pipeline().addLast(new ChannelHandlerAdapter() { 
       @Override 
       public void channelRegistered(ChannelHandlerContext ctx) throws Exception { 
        System.out.println("the num" +num.getAndIncrement()); 
       } 

       @Override 
       public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 
        System.out.println("what i say is :" + msg.toString()); 
        ctx.channel().writeAndFlush("from server " + "reply message is " +msg.toString()+"\n"); 

       } 
      }); 
     } 
    }); 
    ChannelFuture future = bootstrap.bind(port).sync(); 
    System.out.println("Server start at port : " + port); 
    future.channel().closeFuture().sync(); 
}catch (Exception e){ 
    System.out.println("error"); 
}finally { 
    bossGroup.shutdownGracefully(); 
    workGroup.shutdownGracefully(); 
} 


} 

です。 ここに私のクライアントのデモです。

Socket socket = new Socket(); 
    try { 
     socket.connect(new InetSocketAddress("localhost" , 9999)); 
     InputStream inputStream = socket.getInputStream(); 
     OutputStream outputStream = socket.getOutputStream(); 
     outputStream.write("hello".getBytes()); 
     outputStream.flush(); 
     InputStreamReader reader = new InputStreamReader(inputStream) ; 
     char [] temChar = new char[40]; 
     StringBuffer buffer = new StringBuffer(); 

     while (reader.read(temChar) != -1){ 
      buffer.append(temChar); 
      System.out.println(buffer.toString() +"\n"); 
     } 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

"hello"を送信して、同じ単語を返信します。ここ

enter image description here

私のクライアントネッティーデモが

int port = 9999; 
Bootstrap bootstrap ; 
NioEventLoopGroup workGroup = new NioEventLoopGroup(8); 



    try { 
     bootstrap = new Bootstrap(); 
     bootstrap.group(workGroup); 
     bootstrap.channel(NioSocketChannel.class); 
     bootstrap.handler(new ChannelInitializer<SocketChannel>() { 
      @Override 
      protected void initChannel(SocketChannel socketChannel) throws Exception { 
       socketChannel.pipeline().addLast(new StringDecoder() ,new StringEncoder() , new LineBasedFrameDecoder(1024), new ChannelHandlerAdapter(){ 
        @Override 
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 
         System.out.println("recieve method of client : " +msg.toString()); 
        } 
       }); 
      } 
     }); 


     NioSocketChannel nioSocketChannel ; 

     ChannelFuture future = bootstrap.connect("localhost" , 9999).sync(); 

     //you can also invoke writeAndFlush() in other thread with channel , 
     // it is the same as server 
     System.out.println("try to write hello"); 
     Channel channel = future.channel(); 
     channel.writeAndFlush("hello\n\r"); 


     future.channel().closeFuture().sync(); //it will block until 
                // you invoke 
                // channel.close(); 




     System.out.println("finish: " + port); 

    }catch (Exception e){ 
     e.printStackTrace(); 
     System.out.println("error"); 
    }finally { 

     workGroup.shutdownGracefully(); 
    } 


} 
+0

'ソケットソケット=新しいソケット();'ちょっと待っているのは単純なJavaソケットです... * netty *で*クライアント*を作るには?誰もがサーバーを作る方法について話していますが、私は*クライアント*の作り方を教えてくれる人はいません。 – SoniEx2

+0

@ SoniEx2私はアップデート –

+0

を持っています。github https://github.com/netty/netty/tree/4.1/example/src/main/java/io/netty/exampleにある多くのデモを見つけることができます。 –

関連する問題