android socket连接被拒绝。

山城忙碌人 山城忙碌人 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
问题描述:
1 手机锁屏,或者应用退到后台,或者直接关闭网络,重新打开网络后,socket连接直接被拒绝连接。
异常信息如下:failed to connect to /42.96.162.391 (port 7799) after 20000ms: isConnecte falied:ECONNERFUSED(Connection resused)
2 如果重新打开app,重新运行又能访问,不会出现socket被拒绝的情况。
3 在重新连接前,socket,inputStream,outputStream 都关闭的,但还是连接失败。
4 部分代码如下:

try {
if (inputStream != null) {
inputStream.close();
}
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
} finally {
if (socket != null && !socket.isClosed()) {
socket.close();
}
}
}
socket = null;
inputStream = null;
outputStream = null;

Socket socket = new Socket();
socket.connect(new InetSocketAddress(host, port),
20000);
outputStream = socket.getOutputStream();
inputStream = socket.getInputStream();


注:连接被拒绝的问题,不是在每个手机上都出现,目前测试小米3断网,重新连接后这个异常必现,其它手机暂时没发现这个问题。

我在手机断网时,把后台service,以及sockt,outputStream,inputStream 都停止了。在service的onDestroy方法重新启动服务。然后小米3联网后,仍然有这个问题。
...全文

相关推荐

评论 1
收藏 已收藏
分享
返回顶部
山城忙碌人
Android 80,490 成员 91,371 内容