netty java tcp,如何在Netty中打开TCP连接(客户端)?

I've been looking at the Netty javadocs for hours and I still can't figure this out. How do I open a plain old TCP connection, e.g. to an IRC server, in Netty?

解决方案

Assumeing that you are using the netty5 in server .

you should add the decoder and encoder to your pipeline.

like below

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

here is my server demo

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() {

@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();

}

}

then you can use the blocking socket to connect it as normal .

here is my client demo.

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();

}

then i send “hello” and it reply the same word .

wv5u3.png

here is my client netty demo

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() {

@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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值