How to send data from Fragment to an Activity in Android?

Why no more TCP keep alive probe send after some data send to Send-Q?

  • I have a client(mobile app) connect to a Server, both of them are in a local network environment. and I have the following TCP Keepalive configuration set in the server: net.ipv4.tcp_keepalive_intvl = 3 net.ipv4.tcp_keepalive_probes = 5 net.ipv4.tcp_keepalive_time = 60 (Full tcp configuration attached below) so far so good, and then I turn the mobile to flight mode, so, the network connection is down, from client's perspective, though no FIN send from either side of course. next I have server send some data to the client, since there is no FIN, so, the server still think the connection is ESTABLISHED. but surely the data can not be transmitted to the client and I checked with NETSTAT and confirm the connection is in ESTABLISHED state and there is 613 bytes in Send-Q. but the weird think is: after this, there is no more tcp keep alive probe send from server to client, SO, the server thinks client is still connected even after a long period. is this because there is a Non-ACKed data, so the keep alive probe will not send before it get acked? if so, what should i do in this situation? I'm looking for some method to quickly network failure detection [edit] what is confusing me is this line in the  http://tools.ietf.org/html/rfc1122#page-101 it clearly says : Keep-alive packets MUST only be sent when no data or             acknowledgement packets have been received for the             connection within an interval. in my case, there is no ACK received, so, why not the server send the keep alive probe ? the server is a CentOS 6.5 with kernel 2.6.32-358.6.2.el6.x86_64 thanks. another question is, from tcpdump running on the server, i see the server is trying to retransmit the data 10 times before give up ( see below), my question is which configuration controls this 10 number? ############### TCP dump ###################### 17:52:48.481743 IP 192.168.4.234.xmpp-client > 192.168.4.81.36560: Flags [P.], seq 1676:2289, ack 1018, win 260, options [nop,nop,TS val 10842742 ecr 1057770], length 613 17:52:48.700216 IP 192.168.4.234.xmpp-client > 192.168.4.81.36560: Flags [P.], seq 1676:2289, ack 1018, win 260, options [nop,nop,TS val 10842797 ecr 1057770], length 613 17:52:48.921172 IP 192.168.4.234.xmpp-client > 192.168.4.81.36560: Flags [P.], seq 1676:2289, ack 1018, win 260, options [nop,nop,TS val 10842852 ecr 1057770], length 613 17:52:49.361151 IP 192.168.4.234.xmpp-client > 192.168.4.81.36560: Flags [P.], seq 1676:2289, ack 1018, win 260, options [nop,nop,TS val 10842962 ecr 1057770], length 613 17:52:50.241216 IP 192.168.4.234.xmpp-client > 192.168.4.81.36560: Flags [P.], seq 1676:2289, ack 1018, win 260, options [nop,nop,TS val 10843182 ecr 1057770], length 613 17:52:52.004194 IP 192.168.4.234.xmpp-client > 192.168.4.81.36560: Flags [P.], seq 1676:2289, ack 1018, win 260, options [nop,nop,TS val 10843623 ecr 1057770], length 613 17:52:55.528194 IP 192.168.4.234.xmpp-client > 192.168.4.81.36560: Flags [P.], seq 1676:2289, ack 1018, win 260, options [nop,nop,TS val 10844504 ecr 1057770], length 613 17:53:02.585267 IP 192.168.4.234.xmpp-client > 192.168.4.81.36560: Flags [P.], seq 1676:2289, ack 1018, win 260, options [nop,nop,TS val 10846268 ecr 1057770], length 613 17:53:16.680273 IP 192.168.4.234.xmpp-client > 192.168.4.81.36560: Flags [P.], seq 1676:2289, ack 1018, win 260, options [nop,nop,TS val 10849792 ecr 1057770], length 613 17:53:44.904276 IP 192.168.4.234.xmpp-client > 192.168.4.81.36560: Flags [P.], seq 1676:2289, ack 1018, win 260, options [nop,nop,TS val 10856848 ecr 1057770], length 613   ############### Server TCP Configuration ############### net.ipv4.neigh.lo.anycast_delay = 100 net.ipv4.neigh.lo.app_solicit = 0 net.ipv4.neigh.lo.base_reachable_time_ms = 30000 net.ipv4.neigh.lo.delay_first_probe_time = 5 net.ipv4.neigh.lo.gc_stale_time = 60 net.ipv4.neigh.lo.locktime = 100 net.ipv4.neigh.lo.mcast_solicit = 3 net.ipv4.neigh.lo.proxy_delay = 80 net.ipv4.neigh.lo.proxy_qlen = 64 net.ipv4.neigh.lo.retrans_time_ms = 1000 net.ipv4.neigh.lo.ucast_solicit = 3 net.ipv4.neigh.lo.unres_qlen = 31 net.ipv4.neigh.lo.unres_qlen_bytes = 65536 net.ipv4.ping_group_range = 1 0 net.ipv4.route.error_burst = 1250 net.ipv4.route.error_cost = 250 net.ipv4.route.gc_elasticity = 8 net.ipv4.route.gc_interval = 60 net.ipv4.route.gc_min_interval = 0 net.ipv4.route.gc_min_interval_ms = 500 net.ipv4.route.gc_thresh = -1 net.ipv4.route.gc_timeout = 300 net.ipv4.route.max_size = 2147483647 net.ipv4.route.min_adv_mss = 256 net.ipv4.route.min_pmtu = 552 net.ipv4.route.mtu_expires = 600 net.ipv4.route.redirect_load = 5 net.ipv4.route.redirect_number = 9 net.ipv4.route.redirect_silence = 5120 net.ipv4.tcp_abort_on_overflow = 0 net.ipv4.tcp_adv_win_scale = 1 net.ipv4.tcp_allowed_congestion_control = cubic reno net.ipv4.tcp_app_win = 31 net.ipv4.tcp_available_congestion_control = cubic reno net.ipv4.tcp_base_mss = 512 net.ipv4.tcp_challenge_ack_limit = 100 net.ipv4.tcp_congestion_control = cubic net.ipv4.tcp_dsack = 1 net.ipv4.tcp_early_retrans = 3 net.ipv4.tcp_ecn = 2 net.ipv4.tcp_fack = 1 net.ipv4.tcp_fastopen = 1 net.ipv4.tcp_fin_timeout = 60 net.ipv4.tcp_frto = 2 net.ipv4.tcp_keepalive_intvl = 3 net.ipv4.tcp_keepalive_probes = 5 net.ipv4.tcp_keepalive_time = 60 net.ipv4.tcp_limit_output_bytes = 131072 net.ipv4.tcp_low_latency = 0 net.ipv4.tcp_max_orphans = 8192 net.ipv4.tcp_max_syn_backlog = 128 net.ipv4.tcp_max_tw_buckets = 8192 net.ipv4.tcp_mem = 47544 63394 95088 net.ipv4.tcp_min_tso_segs = 2 net.ipv4.tcp_moderate_rcvbuf = 1 net.ipv4.tcp_mtu_probing = 0 net.ipv4.tcp_no_metrics_save = 1 net.ipv4.tcp_notsent_lowat = -1 net.ipv4.tcp_orphan_retries = 0 net.ipv4.tcp_reordering = 3 net.ipv4.tcp_retrans_collapse = 1 net.ipv4.tcp_retries1 = 3 net.ipv4.tcp_retries2 = 15 net.ipv4.tcp_rfc1337 = 0 net.ipv4.tcp_rmem = 4096 87380 6291456 net.ipv4.tcp_sack = 1 net.ipv4.tcp_slow_start_after_idle = 1 net.ipv4.tcp_stdurg = 0 net.ipv4.tcp_syn_retries = 6 net.ipv4.tcp_synack_retries = 5 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_thin_dupack = 0 net.ipv4.tcp_thin_linear_timeouts = 0 net.ipv4.tcp_timestamps = 1 net.ipv4.tcp_tso_win_divisor = 3 net.ipv4.tcp_tw_recycle = 0 net.ipv4.tcp_tw_reuse = 0 net.ipv4.tcp_window_scaling = 1 net.ipv4.tcp_wmem = 4096 16384 4194304 net.ipv4.tcp_workaround_signed_windows = 0 net.ipv4.udp_mem = 47544 63394 95088 net.ipv4.udp_rmem_min = 4096 net.ipv4.udp_wmem_min = 4096 net.ipv4.xfrm4_gc_thresh = 32768

  • Answer:

    The relevant variable that controls the number of retries for pending data is net.ipv4.tcp.retries2 which is set to 15 (the default) in your config.   Using the  exponential backoff algorithm, each re-transmit (up to 15) waits longer and longer.  A setting of 15 will keep the connection in ESTABLISHED mode for around 15 - 30 minutes, depending on the average RTT before your mobile device went into airplane mode. You don't see KEEP_ALIVE probes because those are only used when there is no data waiting to be transmitted.  Since there is data in the queue, the re-transmit  algorithm for data is what is used. You could lower the number to get faster disconnects with a corresponding risk of dropping a valid connection if the network becomes slow. If the mobile app runs on Android,, the app can detect when the airplane mode is enabled/disabled by listening for  the android.intent.action.AIRPLANE_MODE broadcast intent.   It can't send anything at that point, but it could know when to reestablish the connection when airplane mode ends, or it could exit gracefully when airplane mode starts.

Joe Herman at Quora Visit the source

Was this solution helpful to you?

Other answers

tcp_keepalive_time the interval between the last data packet sent (simple ACKs are not considered data) and the first keepalive probe; after the connection is marked to need keepalive, this counter is not used any further. According to this, it means that the keep alive packets are not sent when you are repeatedly sending some data ( even if client is not available ). Assuming the situation where your client is lost, and the server has no data to send, then server after 60 seconds from the last data packet's sent time ( keepalive_time ) sends a keep alive packet, and then it continues this sending of keep alive probes until the no_of_probes are completed, then it comes to know that the client is lost. In your case, the server has data and it is trying to send. So, there wont be any keep alive packets generated until the server stops sending data. Server here is sending for 10 times and its giving up, concluding that the client is down. There is no role of keep alive in knowing that client is down, but the non-reception of Ack's for the sent packets from the server. Ref: http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/usingkeepalive.html

Vamshi Reddy

As long as data is flowing, there is no need for the keepalive to trigger and send packets to determine the state of the connection because its evident that the connection is established. Keepalive will trigger after the data flow stops for a while, this value is configurable in server. I am not sure which timer really controls that. On your number of retries, it is controlled by /proc/sys/net/ipv4/tcp_retries1 /proc/sys/net/ipv4/tcp_retries2 Read about these to get more info. If you want to close the connection, then you must do it from the client side, because it has info that network is going down.

Manish Awasthi

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.