引言

最近在做网络访问功能,用到了失败重传,效果却不理想。其问题点在于,对同一个地址连续访问多次,如果第一次失败花了几秒钟的时间,后续访问失败根本不用花几秒(甚至到了几毫秒)就结束。一度让我怀疑,我应不应该做失败重传,好在经过我的实验,找到了问题所在,并得出了相应结论以及解决办法。但更为深层次的原因,还需要继续探究。所以现在跟我一起看看是怎么回事儿吧!

既然是失败重传,那么就得创造网络访问失败的环境,这个环境是这样:手机正常连接路由器WiFi,将路由器WAN口的网线拔掉,即让路由断网。

环境准备好了,就开始上代码运行,代码详情如下

println("lr------ programming launch")
for (i in 0 until 5) {
    val start = System.currentTimeMillis()
    var connection: HttpURLConnection? = null
    try {
        connection = URL("https://www.baidu.com").openConnection() as HttpURLConnection
        connection.connectTimeout = 2000
        connection.readTimeout = 2000
        connection.connect()
    } catch (e: Exception) {
        println("lr------ err:$e")
    } finally {
        connection?.disconnect()
    }
    println("lr------ running time: " + (System.currentTimeMillis() - start) / 1_000.0 + "s")
}
println("lr------ programming end")

上述代码进行了5次同一个地址的网络连接请求,我们看看在我们创造网络访问失败的环境的情况下的打印输出

lr------ programming launch
lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
lr------ running time: 16.043s
lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
lr------ running time: 0.005s
lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
lr------ running time: 0.005s
lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
lr------ running time: 0.004s
lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
lr------ running time: 0.004s
lr------ programming end

可以看到,第一网络连接请求花了16s的时间,后续4次都只是花了几毫秒,反复测试也是如此。后几次的访问看上去就是简单复制了前一次的错误信息,根本没有去真实的访问网络,或者说,第一次的连接或许没有断开过,即便调用了connection?.disconnect()

如何解决这个问题呢,有了上面的猜想,我决定在每次网络访连接请求后都延时1200毫秒,于是得到如下代码

println("lr------ programming launch")
for (i in 0 until 5) {
    val start = System.currentTimeMillis()
    var connection: HttpURLConnection? = null
    try {
        connection = URL("https://www.baidu.com").openConnection() as HttpURLConnection
        connection.connectTimeout = 2000
        connection.readTimeout = 2000
        connection.connect()
    } catch (e: Exception) {
        println("lr------ err:$e")
    } finally {
        connection?.disconnect()
    }
    println("lr------ running time: " + (System.currentTimeMillis() - start) / 1_000.0 + "s")
    TimeUnit.MILLISECONDS.sleep(1200)
}
println("lr------ programming end")

运行后,打印输出如下

lr------ programming launch
lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
lr------ running time: 16.037s
lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
lr------ running time: 0.002s
lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
lr------ running time: 16.029s
lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
lr------ running time: 0.006s
lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
lr------ running time: 16.029s
lr------ programming end

通过这次打印输出,我发现其规律,无论我尝试多少次,花费时间是间隔的,即前一次花了16秒,后一次就花几毫秒。看上去情况有所改善,猜想是延时时间设置小了,于是我改成了2000毫秒,再次运行后,打印输出如下

lr------ programming launch
lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
lr------ running time: 16.195s
lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
lr------ running time: 16.029s
lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
lr------ running time: 16.03s
lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
lr------ running time: 16.033s
lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
lr------ running time: 16.031s
lr------ programming end

可以看到,几次网络连接请求花费的时间都是16秒左右,看上去这个问题解决了。但前面提到,网络连接请求同一个网络地址的时候,会出现这个问题,那连续网络连接请求的是不同的网络地址会是一个怎样的情况,我们执行下面的代码来看情况

lr------ programming launch
lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu0.com": No address associated with hostname
lr------ running time: 16.044s
lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu1.com": No address associated with hostname
lr------ running time: 16.032s
lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu2.com": No address associated with hostname
lr------ running time: 16.029s
lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu3.com": No address associated with hostname
lr------ running time: 16.031s
lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu4.com": No address associated with hostname
lr------ running time: 16.031s
lr------ programming end

看上去问题也得到解决了,此外,像Unable to resolve host "xxx"这类错误,整个网络连接请求流程没有完全建立,为什么这么说呢,因为抓包是抓不到滴!!!

最后

如果想要成为架构师或想突破20~30K薪资范畴,那就不要局限在编码,业务,要会选型、扩展,提升编程思维。此外,良好的职业规划也很重要,学习的习惯很重要,但是最重要的还是要能持之以恒,任何不能坚持落实的计划都是空谈。

如果你没有方向,这里给大家分享一套由阿里高级架构师编写的《Android八大模块进阶笔记》,帮大家将杂乱、零散、碎片化的知识进行体系化的整理,让大家系统而高效地掌握Android开发的各个知识点。
img
相对于我们平时看的碎片化内容,这份笔记的知识点更系统化,更容易理解和记忆,是严格按照知识体系编排的。

欢迎大家一键三连支持,若需要文中资料,直接扫描文末CSDN官方认证微信卡片免费领取↓↓↓

PS:群里还设有ChatGPT机器人,可以解答大家在工作上或者是技术上的问题
图片

Logo

数据库是今天社会发展不可缺少的重要技术,它可以把大量的信息进行有序的存储和管理,为企业的数据处理提供了强大的保障。

更多推荐