Android推送SDK(10)-DHCP租约到期自动续租问题导致TCP连接异常

主目录见:Android高级进阶知识(这是总目录索引)
[written by 无心追求]

DHCP

  • 简单的来说,DHCP是一个局域网协议,使用UDP协议进行工作,它的作用就是动态的分配IP地址,Gateway地址,DNS服务器地址等信息,一旦租约到期,那么路由器就会把当前的这个ip分配给其他设备使用,所以,对于设备而言要定期请求DHCP Server来更新ip地址信息,保证ip地址有效可用

DHCP租约到期,设备不会自动进行续租而仍然使用旧的ip地址

DHCP租约到期,设备向DHCP Server发起请求,但是没有收到Ack

  • 遇到如上情况会导致ip无法更新,所以设备仍然会使用旧的ip,对于tcp连接来说,使用旧的过期ip就意味着连接不到远程服务器,从我们的日志信息中可以得知,当TCP使用过期的ip去连接远程服务器的时候会报如下异常:java.net.NoRouteToHostException: No route to host,意思是说没有可达Host的路由,确实是这样的,设备连接无线网是连接到路由器上的,而路由器上分配给设备的ip已经过期不可用,那么设备到路由器的链路是通的,但是路由器到远程主机的链路肯定是不通的,所以会报如上错误
  • 正常情况下,我们的Android设备在处于熄屏的状态下,DHCP的租约是1个小时,所以会每隔半小时就请求DHCP Server来更新ip地址:
Line 30221: 09-11 12:58:51.016  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67
    Line 30227: 09-11 12:58:51.246  1835  3791 D DhcpClient: Received packet: 00:0a:f5:43:d8:7c ACK: your new IP /172.28.88.48, netmask /255.255.248.0, gateways [/172.28.88.254] DNS servers: /114.114.114.114 /172.28.1.28 /8.8.8.8 /172.28.1.26 , lease time 3600
    Line 30228: 09-11 12:58:51.247  1835  3789 D DhcpClient: Renewed lease: IP address 172.28.88.48/21 Gateway 172.28.88.254  DNS servers: [ 114.114.114.114 172.28.1.28 8.8.8.8 172.28.1.26 ] Domains dw.gdbbk.com DHCP server /1.1.1.1 Vendor info ������ lease 3600 seconds
    Line 30228: 09-11 12:58:51.247  1835  3789 D DhcpClient: Renewed lease: IP address 172.28.88.48/21 Gateway 172.28.88.254  DNS servers: [ 114.114.114.114 172.28.1.28 8.8.8.8 172.28.1.26 ] Domains dw.gdbbk.com DHCP server /1.1.1.1 Vendor info ������ lease 3600 seconds
    Line 30243: 09-11 12:58:51.252  1835  3789 D DhcpClient: Scheduling renewal in 1799s
    Line 30244: 09-11 12:58:51.253  1835  3789 D DhcpClient: Scheduling rebind in 3149s
    Line 30245: 09-11 12:58:51.253  1835  3789 D DhcpClient: Scheduling expiry in 3599s
    Line 30718: 09-11 13:28:51.280  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67
    Line 30718: 09-11 13:28:51.280  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67
    Line 30722: 09-11 13:28:51.306  1835  3791 D DhcpClient: Received packet: 00:0a:f5:43:d8:7c ACK: your new IP /172.28.88.48, netmask /255.255.248.0, gateways [/172.28.88.254] DNS servers: /114.114.114.114 /172.28.1.28 /8.8.8.8 /172.28.1.26 , lease time 3600
    Line 30723: 09-11 13:28:51.307  1835  3789 D DhcpClient: Renewed lease: IP address 172.28.88.48/21 Gateway 172.28.88.254  DNS servers: [ 114.114.114.114 172.28.1.28 8.8.8.8 172.28.1.26 ] Domains dw.gdbbk.com DHCP server /1.1.1.1 Vendor info ������ lease 3600 seconds
    Line 30723: 09-11 13:28:51.307  1835  3789 D DhcpClient: Renewed lease: IP address 172.28.88.48/21 Gateway 172.28.88.254  DNS servers: [ 114.114.114.114 172.28.1.28 8.8.8.8 172.28.1.26 ] Domains dw.gdbbk.com DHCP server /1.1.1.1 Vendor info ������ lease 3600 seconds
    Line 30747: 09-11 13:28:51.316  1835  3789 D DhcpClient: Scheduling renewal in 1799s
    Line 30748: 09-11 13:28:51.316  1835  3789 D DhcpClient: Scheduling rebind in 3149s
    Line 30749: 09-11 13:28:51.316  1835  3789 D DhcpClient: Scheduling expiry in 3599s
    Line 31114: 09-11 13:58:53.153  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67
    Line 31114: 09-11 13:58:53.153  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67
    Line 31120: 09-11 13:58:53.220  1835  3791 D DhcpClient: Received packet: 00:0a:f5:43:d8:7c ACK: your new IP /172.28.88.48, netmask /255.255.248.0, gateways [/172.28.88.254] DNS servers: /114.114.114.114 /172.28.1.28 /8.8.8.8 /172.28.1.26 , lease time 3600
    Line 31121: 09-11 13:58:53.221  1835  3789 D DhcpClient: Renewed lease: IP address 172.28.88.48/21 Gateway 172.28.88.254  DNS servers: [ 114.114.114.114 172.28.1.28 8.8.8.8 172.28.1.26 ] Domains dw.gdbbk.com DHCP server /1.1.1.1 Vendor info ������ lease 3600 seconds
    Line 31121: 09-11 13:58:53.221  1835  3789 D DhcpClient: Renewed lease: IP address 172.28.88.48/21 Gateway 172.28.88.254  DNS servers: [ 114.114.114.114 172.28.1.28 8.8.8.8 172.28.1.26 ] Domains dw.gdbbk.com DHCP server /1.1.1.1 Vendor info ������ lease 3600 seconds
    Line 31133: 09-11 13:58:53.226  1835  3789 D DhcpClient: Scheduling renewal in 1799s
    Line 31134: 09-11 13:58:53.226  1835  3789 D DhcpClient: Scheduling rebind in 3149s
    Line 31135: 09-11 13:58:53.226  1835  3789 D DhcpClient: Scheduling expiry in 3599s

以上是发起请求后有正常收到Ack的情况,但是接下来设备发起了请求并没有收到Ack,所以导致设备一直不断的去发起DHCP请求,而且此时设备并没有网络切换广播,并且设备本地判断网络是正常可用的,原因是设备到路由器的网络是通的,只是路由器到远程服务器的链路是不通的:

33540: 09-11 14:28:54.007  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67
    Line 33540: 09-11 14:28:54.007  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67
    Line 33557: 09-11 14:28:59.012  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67
    Line 33557: 09-11 14:28:59.012  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67
    Line 33585: 09-11 14:29:04.021  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67
    Line 33585: 09-11 14:29:04.021  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67
    Line 33701: 09-11 14:29:12.088  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67
    Line 33701: 09-11 14:29:12.088  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67
    Line 33724: 09-11 14:29:28.065  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67
    Line 33724: 09-11 14:29:28.065  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67
    Line 33779: 09-11 14:30:03.317  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67
    Line 33779: 09-11 14:30:03.317  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67
    Line 33815: 09-11 14:31:13.838  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67
    Line 33815: 09-11 14:31:13.838  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67
    Line 51658: 09-11 14:33:33.786  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67
    Line 51658: 09-11 14:33:33.786  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67
    Line 53369: 09-11 14:35:32.229  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67
    Line 53369: 09-11 14:35:32.229  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67
    Line 53714: 09-11 14:37:46.146  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67
    Line 53714: 09-11 14:37:46.146  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67
    Line 55611: 09-11 14:40:02.569  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67
    Line 55611: 09-11 14:40:02.569  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67
    Line 57692: 09-11 14:42:01.271  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67
    Line 57692: 09-11 14:42:01.271  1835  3789 D DhcpClient: Unicasting DHCPREQUEST ciaddr=172.28.88.48 request=0.0.0.0 serverid=null to /1.1.1.1:67

在这段时间内,TCP连接是一直报java.net.NoRouteToHostException: No route to host异常,如果TCP当前正处于连接中,那么DHCP的更新可能会导致TCP断线,等到后面设备发起的DHCP请求收到Ack之后,TCP连接立刻恢复正常,并且此时收到了网络切换广播,针对以上问题,如何恢复呢?搜索网上资料是说应用程序每次重新建立一条新的TCP的时候会触发设备请求DHCP Server,但是从我们的log查看发现并没有,请求DHCP Server没有Ack后设备会间隔重试,当设备被触发亮屏后,网络马上恢复正常,DHCP请求也收到Ack,同时也收到网络切换广播

    原文作者:ZJ_Rocky
    原文地址: https://www.jianshu.com/p/fabff1b9cf6a
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞