public static int SERVER_PORT = 9000 ;
public static String ip = "127.0.0.1" ;
private void start ( ) {
EventLoopGroup eventExecutors = new NioEventLoopGroup ( ) ;
try {
Bootstrap bootstrap = new Bootstrap ( ) ;
bootstrap. group ( eventExecutors) ;
bootstrap. channel ( NioSocketChannel. class ) ;
bootstrap. option ( ChannelOption. SO_KEEPALIVE, true ) ;
bootstrap. remoteAddress ( ip, SERVER_PORT) ;
bootstrap. handler ( new ChannelInitializer < SocketChannel> ( ) {
protected void initChannel ( SocketChannel socketChannel) {
socketChannel. pipeline ( ) . addLast ( new TeaDecoder ( ) ) ;
socketChannel. pipeline ( ) . addLast ( new TeaEncoder ( ) ) ;
socketChannel. pipeline ( ) . addLast ( new MyChannelHandlerAdapter ( new NettyClientProcessor ( ) ) ) ;
socketChannel. pipeline ( ) . addLast ( new LoggingHandler ( LogLevel. INFO) ) ;
}
} ) ;
ChannelFuture future = bootstrap. connect ( ip, SERVER_PORT) . sync ( ) ;
if ( future. isSuccess ( ) ) {
System. out. println ( "连接服务器成功" ) ;
}
future. channel ( ) . closeFuture ( ) . sync ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
} finally {
eventExecutors. shutdownGracefully ( ) ;
}
}
public static void main ( String[ ] args) {
NettyClint2 nettyClint2 = new NettyClint2 ( ) ;
nettyClint2. start ( ) ;
}