2 derrickjl Derrickjl 于 2017.03.14 23:22 提问

android中使用Netty当客户端时候出现以下错误连接不上

Caused by: io.netty.channel.ChannelException: Failed to open a socket.
at io.netty.channel.socket.nio.NioSocketChannel.newSocket(NioSocketChannel.java:63)
at io.netty.channel.socket.nio.NioSocketChannel.(NioSocketChannel.java:80)
at io.netty.channel.socket.nio.NioSocketChannel.(NioSocketChannel.java:73)
at java.lang.Class.newInstance(Native Method)
at io.netty.channel.ReflectiveChannelFactory.newChannel(ReflectiveChannelFactory.java:38)
at io.netty.bootstrap.AbstractBootstrap.initAndRegister(AbstractBootstrap.java:319) 
at io.netty.bootstrap.Bootstrap.doResolveAndConnect(Bootstrap.java:163) 
at io.netty.bootstrap.Bootstrap.connect(Bootstrap.java:119) 
at com.client.Client.connect(Client.java:43) 
at com.example.shx.serachnote.Main.onCreate(Main.java:43) 
at android.app.Activity.performCreate(Activity.java:6664) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
Caused by: java.net.SocketException: Permission denied
at sun.nio.ch.Net.socket0(Native Method)
at sun.nio.ch.Net.socket(Net.java:433)
at sun.nio.ch.Net.socket(Net.java:426)
at sun.nio.ch.SocketChannelImpl.(SocketChannelImpl.java:107)
at sun.nio.ch.SelectorProviderImpl.openSocketChannel(SelectorProviderImpl.java:60)
at io.netty.channel.socket.nio.NioSocketChannel.newSocket(NioSocketChannel.java:61)
at io.netty.channel.socket.nio.NioSocketChannel.(NioSocketChannel.java:80) 
at io.netty.channel.socket.nio.NioSocketChannel.(NioSocketChannel.java:73) 
at java.lang.Class.newInstance(Native Method) 
at io.netty.channel.ReflectiveChannelFactory.newChannel(ReflectiveChannelFactory.java:38) 
at io.netty.bootstrap.AbstractBootstrap.initAndRegister(AbstractBootstrap.java:319) 
at io.netty.bootstrap.Bootstrap.doResolveAndConnect(Bootstrap.java:163) 
at io.netty.bootstrap.Bootstrap.connect(Bootstrap.java:119) 
at com.client.Client.connect(Client.java:43) 
at com.example.shx.serachnote.Main.onCreate(Main.java:43) 
at android.app.Activity.performCreate(Activity.java:6664) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

3个回答

Z_DingHao
Z_DingHao   2017.03.16 19:41

打开socket错误,权限没有声明,在AndroidManifest.xml文件中加入权限



当然 如果你是通过wifi连接socket的话记得开wifi的权限

Z_DingHao
Z_DingHao   2017.03.16 19:41



Z_DingHao
Z_DingHao   2017.03.16 19:42
 <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!