java - Why does DefaultHttpClient send data over a half-closed socket? -
i'm using defaulthttpclient threadsafeclientconnmanager on android (2.3.x) send http requests rest server (embedded jetty).
after ~200 seconds of idle time, server closes tcp connection [fin]. android client responds [ack]. should , leave socket in half-closed state (server still listening, can't send data).
i expect when client tries use connection again (via httpclient.execute), defaulthttpclient detect half-closed state, close socket on client side (thus sending it's [fin/ack] finalize close), , open new connection request. but, there's rub.
instead, sends new http request on half-closed socket. after sending half-closed state detected , socket closed on client-side (with [fin] sent server). of course, server can't respond request (it had sent [fin]), client thinks request failed , automatically retries via new socket/connection.
the end result server sees , processes 2 copies of request.
any ideas on how fix this? (my server correct thing second copy, i'm annoyed payload transmitted twice.)
shouldn't defaulthttpclient detect socket closed when first tries write new http packet, close socket immediately, , start new one? i'm baffled how new http request sent on socket minutes after server sent [fin].
this general limitation of blocking i/o in java. there no way of finding out whether or not opposite endpoint has closed connection other attempting read socket. apache httpclient works problem around employing stale connection check brief read operation. however, check can , disabled. in fact advisable have disabled due latency check introduces. have no idea how version of httpclient shipped android behaves in regard try explicitly enabling check using appropriate config parameter.
a better solution problem might evicting connections connection pool have been idle on particular period of time (say 150 seconds) after period of inactivity.
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d5e652
Comments
Post a Comment