关于netty框架使用在Android设备上的疑惑

feer 发布于 2016/01/13 10:05
阅读 3K+
收藏 0

android 设备作为客户端,使用Netty去连接服务器,代码如下:


try {
 EventLoopGroup workerGroup =  new  NioEventLoopGroup() ;    
 bootstrap  =  new  Bootstrap() ;   
 bootstrap .group(workerGroup) ;    
 bootstrap .channel(NioSocketChannel. class ) ;   
 bootstrap .option(ChannelOption. TCP_NODELAY , true ) ;         bootstrap .handler( new  ChannelInitializer<SocketChannel>() {  @Override    public void  initChannel (SocketChannel ch)  throws  Exception { 
       ch.pipeline().addFirst("decoder", new      LengthFieldBasedFrameDecoder(1000, 0, 4, 0, 0));  ch.pipeline().addLast(new SkySimClientHandler());  }
    });  
long startTime = System.currentTimeMillis();  // 
启动 client 发起异步连接操作  
channelFuture = bootstrap.connect(sHost, iPort).sync();  printLog("start suc to connect ip :" + sHost ); 
 boolean isConnectSuc = channelFuture.isSuccess(); 
 if(isConnectSuc){ 
       this.workerGroup = workerGroup; 
 }
    SkySimLogger.d("SkySim", "SSTcpConnect" + "Start :bootstrap.connect");  
long distanceTime = System.currentTimeMillis() - startTime;  printLog("connect success. waste time : " + distanceTime +" isConnectSuc = "+ isConnectSuc);  
bReadable = true;  
if( issTcpCallback != null ){ issTcpCallback.tcpConnect(0);  }
 //等待客户端链路关闭  
channelFuture.channel().closeFuture().sync();   
if( issTcpCallback != null ){ try{ issTcpCallback.tcpDisconnect(0);  }catch (Exception CBError){ 
issTcpCallback = null;  }
    }
 bConnect = false;  
bReadable = false; 
 SkySimLogger.d("SkySim", "SSTcpConnect" + "Start : End"); } catch (Exception e){
    printLog("start() occur " + e);  bConnect = false;  bReadable = false; 
 SkySimLogger.d("SkySim", "SSTcpConnect" + "Start error:" + e.toString());  
try{
        Thread.sleep(10000);  }catch (Exception eS){
        eS.printStackTrace();  }
}
 finally {
    printLog("run to finally");  
/*  if( issTcpCallback != null && (oldReadable == true) ){  try{  issTcpCallback.tcpDisconnect(0);  }catch (Exception CBError){  issTcpCallback = null;  }  }  */ 
 oldReadable = bReadable;  
//优雅退出,释放NIO线程组 
 if(workerGroup != null) workerGroup.shutdownGracefully(); }



诡异的问题在于:我把Android设备的所有外网(即包括wifi/移动网络)都关掉(也试过把手机设置成飞行模式)来跑这个程序,居然也能连接上服务器,即能执行到
channelFuture = bootstrap.connect(sHost, iPort).sync(); 这行代码后面,且
boolean isConnectSuc = channelFuture.isSuccess(); 打印为true


望各位大侠能解惑,到底是为什么能在手机没有任何外网的情况下也能连接上服务器, 公司的Android设备测试遍都存在这个诡异的问题。

另使用的Netty版本为:

netty-all-4.0.32.Final.ja
加载中
0
游客
游客
你已经脱离了地球引力
0
wangccsy
wangccsy
我是打死连接不上。直接在电脑上使用客户端连接服务没有问题。
0
花间小酌
花间小酌

哥们  不知道怎么弄的  我这死活连不上   在电脑上能连。异常是这样的:io.netty.channel.ChannelException: Unable to create Channel from class class io.netty.channel.socket.nio.NioSocketChannel。代码是这样的:

public void connetc(int port, String host) throws Exception{
    EventLoopGroup group = new NioEventLoopGroup();   try{
        Bootstrap b = new Bootstrap();  b.group(group).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true).handler(new ChannelInitializer<SocketChannel>() { @Override  public void initChannel(SocketChannel ch) throws Exception{
                ByteBuf delimiter = Unpooled.copiedBuffer(delimiterStr.getBytes());  ch.pipeline().addLast(new DelimiterBasedFrameDecoder(4096, delimiter));  ch.pipeline().addLast(new StringDecoder());  ch.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler(60));  ch.pipeline().addLast("loginAuthReqHandler", new LoginAuthReqHandler(id));  ch.pipeline().addLast("heartBeatReqHandler", new HeartBeatRequestHandler());  ch.pipeline().addLast("pushHandler", new PushHandler());   }
        });   System.out.println("------------------------------------future----------->" );   ChannelFuture future = b.connect(host, port).sync();   future.channel().closeFuture().sync();  }finally {
        group.shutdownGracefully();  }
}

fengyc
fengyc
回复 @狐狸伊凡 : 项目暂时不用netty,就用socket.
x
xgp001
权限问题?
狐狸伊凡
狐狸伊凡
回复 @fengyc : 有解决方案了吗 我也是这个问题
fengyc
fengyc
兄弟,我也是这种现象,使用普通socket是能连接的,
0
晓斐斐
晓斐斐

你是不是傻  ,都关了哪来网络, 这还被你解读成灵异吗?

现在的网络是通过usb共享的 。  你拔掉USB再看。

返回顶部
顶部