Socket doesn't throw exception in Android

栏目: 服务器 · 发布时间: 7年前

内容简介:Several threads fromWe have a simple client server architecture between our mobile device and our server both written in Java. An extremely simple ServerSocket and Socket implementation. However one problem is that when the client terminates abruptly (with

Several threads from STACK OVERFLOW :

We have a simple client server architecture between our mobile device and our server both written in Java. An extremely simple ServerSocket and Socket implementation. However one problem is that when the client terminates abruptly (without closing the socket properly) the server does not know that it is disconnected. Furthermore, the server can continue to write to this socket without getting any exceptions. Why?

Another,

If I put a nonsense url no exception is thrown and none of the rest of my code is executed not even the rest of the asynctask that called the method that connects.

Typically:

try {
        socket.connect(new InetSocketAddress(address, port), 2000);
    } catch (Exception e) {
        //will never reach this block
    }

I am facing the same problem. I have tried every means below these questions, but it still did not work. I found that some apps downloaded from Google Play has the same problem that it could connect and send data to a invalid address.

There is no doubt that there is a bug of Android SDK, could found in this thread: no IOException on socket read/write after remote close as well.

My solution is to switch the minSdkVersion to a newer version such as 21 and to use the latest build tools.

Android 开发过程中,遇到一个比较诡异的问题:Socket.connect 即使连接失败也不会抛出任何异常,这个问题在 STACK OVERFLOW 上已经有非常多的讨论,但我翻了无数答案没有找到合适的解决方法。最后可以确认这是Android历史遗留的一个臭虫,至于在哪个版本SDK中被修复了还不得而知,我最后将 minSdkVersion 从15换到21解决了这个问题,代价就是APP只能在 Android 5.0 以后的版本上运行。

强迫症这个病,得治。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Perl语言编程

Perl语言编程

克里斯蒂安森 (Tom Christiansen) (作者)、Brian D Foy (作者)、Larry Wall (作者)、Jon Orwant (作者) / 苏金国 (译者)、吴爽 (译者) / 中国电力出版社 / 2014-9-1 / 148

从1991年第一版问世以来,《Perl语言编程》很快成为无可争议的Perl宝典,如今仍是这种高实用性语言的权威指南。Perl最初只是作为一个功能强大的文本处理工具,不过很快发展成为一种通用的编程语言,可以帮助成千上万的程序员、系统管理员,以及像你一样的技术爱好者轻松完成工作。 人们早已经翘首以待这本“大骆驼书”的更新,如今终于得偿所愿。在这一版中,三位颇有声望的Perl作者讲述了这种语言当前......一起来看看 《Perl语言编程》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具