Netty 4 퍼 가기 서비스의 실현 방안
Handler 에서 새로운 Bootstrap 을 사용 하 는 원리 입 니 다.이 새로운 Bootstrap 과 바깥쪽 Bootstrap 은 같은 채널 을 공유 할 수 있 습 니 다.
새로운 Bootstrap 은 클 라 이언 트 모드 로 전송 목표 서비스 주소 에 연결 되 며 구체 적 으로 다음 과 같이 실현 합 니 다.
import java.net.InetSocketAddress;
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
public class Aaa {
public static void main(String[] params) {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer() {
@Override
protected void initChannel(Channel ch) throws Exception {
// TODO Auto-generated method stub
ch.pipeline().addLast(new StringDecoder());
ch.pipeline().addLast(new StringEncoder());
ch.pipeline().addLast(new SimpleChannelInboundHandler() {
private ChannelHandlerContext innerCtx;
ChannelFuture connectFuture;
@Override
public void channelActive(ChannelHandlerContext ctx)
throws Exception {
Bootstrap bootstrap = new Bootstrap();
bootstrap.channel(NioSocketChannel.class).handler(
new SimpleChannelInboundHandler() {
// , ,
@Override
protected void channelRead0(
ChannelHandlerContext ctx, ByteBuf in)
throws Exception {
innerCtx = ctx;
byte[] dst = new byte[in.readableBytes()];
in.readBytes(dst);
System.out.println("Received data" + new String(dst));
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println(" "+ctx.channel().toString());
}
} );
bootstrap.group(ctx.channel().eventLoop());// 。 channel eventLoop
connectFuture = bootstrap.connect(
// new InetSocketAddress("192.168.60.49", 23456));
new InetSocketAddress("localhost", 23456));
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
// TODO Auto-generated method stub
if (connectFuture.isDone()) {
// do something with the data
//channel , EventLoop,
// channel
if (innerCtx != null && innerCtx.channel().isActive()) {
innerCtx.writeAndFlush(msg);
}
}
}
});
ChannelFuture future = bootstrap.bind(new InetSocketAddress(8080));
future.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture channelFuture)
throws Exception {
if (channelFuture.isSuccess()) {
System.out.println("Server bound 8080");
} else {
System.err.println("Bind attempt failed");
channelFuture.cause().printStackTrace();
}
}
});
}
});
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.