android socket连接请教

hackha2008 2016-07-09 12:44:51
如下代码所示,在andorid stuio中我创建了个SocketThread线程,然后试图在里面进行网络连接,ip,端口设置都正确 ,同事我在android Manifest文件中也添加了网络权限,但是我在调试的时候每次获取的socket对象都为空,请问一下这是什么原因,快被整崩溃了
------------------------------------------------------------------------------------------------
public void conn() {

try {
Log.i(TAG, "连接中……");
//client = new Socket(Ip, Port);
Socket client = new Socket(Ip, Port);
client.setSoTimeout(timeout); // 设置阻塞时间
Log.i(TAG, "连接成功");
in = client.getInputStream();
out = client.getOutputStream();
out.write("haha,shouji fasong chenggong ".getBytes());
out.flush();
Log.i(TAG, "输入输出流获取成功");
} catch (UnknownHostException e) {
Log.i(TAG, "连接错误UnknownHostException 重新获取");
e.printStackTrace();
//conn();
} catch (IOException e) {
Log.i(TAG, "连接服务器io错误");
e.printStackTrace();
} catch (Exception e) {
Log.i(TAG, "连接服务器错误Exception" + e.getMessage());
e.printStackTrace();
}
}
-------------------------------------------------------------------------------------------------------------------------------------

调试捕捉到的代码
**********************************************************************************************************
07-09 12:35:53.217 12041-12041/smarthome.hackha.wificonnection I/socket thread: 创建线程socket
07-09 12:35:53.218 12041-12041/smarthome.hackha.wificonnection I/socket thread: 连接中……
07-09 12:35:53.236 12041-12041/smarthome.hackha.wificonnection D/libc-netbsd: [getaddrinfo]: mtk hostname=192.168.1.6; servname=(null); cache_mode=(null), netid=0; mark=0
07-09 12:35:53.236 12041-12041/smarthome.hackha.wificonnection D/libc-netbsd: getaddrinfo( app_uid:10128
07-09 12:35:53.236 12041-12041/smarthome.hackha.wificonnection D/libc-netbsd: getaddrinfo() uid prop:
07-09 12:35:53.237 12041-12041/smarthome.hackha.wificonnection D/libc-netbsd: getaddrinfo() getuid():10128
07-09 12:35:53.237 12041-12041/smarthome.hackha.wificonnection D/libc-netbsd: [getaddrinfo]: mtk ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0
07-09 12:35:53.243 12041-12041/smarthome.hackha.wificonnection I/System.out: [CDS]connect[/192.168.1.6:8080] tm:90
07-09 12:35:53.252 12041-12041/smarthome.hackha.wificonnection I/socket thread: 连接服务器错误Exceptionnull
**********************************************************************************************************
...全文
778 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
A-Liang 2018-02-07
  • 打赏
  • 举报
回复
mark android调试中也遇到了该情况,mtk hostname=192.168.7.40; servname=(null); netid=0; mark=0 卸载,再次安装又没问题了,
hackha2008 2016-07-09
  • 打赏
  • 举报
回复
我参考了网上的方法,在要调用socket的主线程的的Activity里面的onCreate函数里面添加如下代码: StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build()); 居然就没事了,我也是醉了哦
hackha2008 2016-07-09
  • 打赏
  • 举报
回复
我用三楼的方法获取的错误是"NetworkOnMainThreadException"这个有知道啥子原因的嘛
hackha2008 2016-07-09
  • 打赏
  • 举报
回复
我用手机端的一个socket测试软件过,该ip端口也是正确的。但是仍然连接不上,对于三楼说的这个我试一下的
tcmakebest 2016-07-09
  • 打赏
  • 举报
回复
楼主把错误的类型打出来看估计能知道点什么 Log.i(TAG, "连接服务器错误Exception" + e.getClass().getName());
  • 打赏
  • 举报
回复
你访问对应的IP和port有在运行吗
  • 打赏
  • 举报
回复
应该没错啦

80,337

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧