java - Socket reuse with HttpURLConnection in android -
java - Socket reuse with HttpURLConnection in android -
i want re-use sockets created httpurlconnection. figured out creates new sockets when investigate raw packets incoming/outgoing from/to remote web server. next code access remote web server.
public string getfromserver() throws ioexception { url url = new url("http://192.168.137.2/test.jsp"); httpurlconnection http = (httpurlconnection)url.openconnection(); http.setrequestproperty("content-type", "application/x-www-form-urlencoded;charset=utf8"); http.setrequestproperty("connection", "keep-alive"); http.setusecaches(true); http.setfixedlengthstreamingmode(0); http.connect(); inputstream = http.getinputstream(); bufferedreader in = new bufferedreader(new inputstreamreader(is)); int c; stringbuffer buffer =new stringbuffer(); while((c=in.read())!=-1) { buffer.append((char)c); } log.i("hv","debug3:"+buffer); is.close(); homecoming buffer.tostring(); }
and calling function twice.
try { java.net.cookiemanager cookiemanager = new java.net.cookiemanager(); system.setproperty("http.keepalive","true"); getfromserver(); getfromserver(); } grab (exception e) { e.printstacktrace(); }
as can see code, remove server ip 192.168.137.2. , device ip 192.168.3.117. expected there 1 tcp connection between server , device when seek code. there 2 times tcp connection different tcp port in device. next tcpdump log in server.
15:40:01.739295 ip 192.168.3.117.48427 > 192.168.137.2.80: flags [s], seq 2864255093, win 14600, options [mss 1460,sackok,ts val 15969200 ecr 0,nop,wscale 6], length 0 e..<..@.?.r....u.....+.p...u......9............ ............ 15:40:01.739322 ip 192.168.137.2.80 > 192.168.3.117.48427: flags [s.], seq 3335569309, ack 2864255094, win 14480, options [mss 1460,sackok,ts val 28276944 ecr 15969200,nop,wscale 7], length 0 ..........,........u.p.+.......v..8. ..x......... 15:40:01.741673 ip 192.168.3.117.48427 > 192.168.137.2.80: flags [.], ack 1, win 229, options [nop,nop,ts val 15969202 ecr 28276944], length 0 e..4..@.?.r5...u.....+.p...v........a...... ......x. 15:40:01.742168 ip 192.168.3.117.48427 > 192.168.137.2.80: flags [p.], seq 1:252, ack 1, win 229, options [nop,nop,ts val 15969203 ecr 28276944], length 251 e../..@.?.q9...u.....+.p...v............... ......x.get /test.jsp http/1.1 content-type: application/x-www-form-urlencoded;charset=utf8 connection: keep-alive user-agent: dalvik/1.6.0 (linux; u; android 4.0.4; shv-e210s build/imm76d) host: 192.168.137.2 accept-encoding: gzip content-length: 0 15:40:01.742193 ip 192.168.137.2.80 > 192.168.3.117.48427: flags [.], ack 252, win 122, options [nop,nop,ts val 28276945 ecr 15969203], length 0 ......@.@.e........u.p.+.......q...z ..x..... 15:40:01.743685 ip 192.168.137.2.80 > 192.168.3.117.48427: flags [p.], seq 1:396, ack 252, win 122, options [nop,nop,ts val 28276945 ecr 15969203], length 395 e....p@.@.d .......u.p.+.......q...zq...... ..x.....http/1.1 200 ok date: fri, 15 feb 2013 06:40:01 gmt set-cookie: jsessionid=aa93f68d2c44cdcab57554ab70b91058; path=/ content-type: text/html;charset=utf8 vary: accept-encoding content-encoding: gzip content-length: 112 keep-alive: timeout=5, max=100 connection: keep-alive .............)i-........i.-*j.+.s+.tpkmr04."+..+.c.....#.cc.}.*.....r.v......}..(....z\....j.`.......[.....'}... 15:40:01.745860 ip 192.168.3.117.48427 > 192.168.137.2.80: flags [.], ack 396, win 245, options [nop,nop,ts val 15969203 ecr 28276945], length 0 e..4..@.?.r3...u.....+.p...q...)....?8..... ......x. 15:40:01.765863 ip 192.168.3.117.48427 > 192.168.137.2.80: flags [f.], seq 252, ack 396, win 245, options [nop,nop,ts val 15969207 ecr 28276945], length 0 e..4..@.?.r2...u.....+.p...q...)....?3..... ......x. 15:40:01.765939 ip 192.168.137.2.80 > 192.168.3.117.48427: flags [f.], seq 396, ack 253, win 122, options [nop,nop,ts val 28276950 ecr 15969207], length 0 ......@.@.e........u.p.+...)...r...z ..x..... 15:40:01.768316 ip 192.168.3.117.48427 > 192.168.137.2.80: flags [.], ack 397, win 245, options [nop,nop,ts val 15969208 ecr 28276950], length 0 e..4..@.?.r1...u.....+.p...r...*....?,..... ......x. 15:40:01.773846 ip 192.168.3.117.40642 > 192.168.137.2.80: flags [s], seq 343617337, win 14600, options [mss 1460,sackok,ts val 15969209 ecr 0,nop,wscale 6], length 0 e..<..@.?..s...u.......p.{/9......9.q.......... ............ 15:40:01.773855 ip 192.168.137.2.80 > 192.168.3.117.40642: flags [s.], seq 2163692151, ack 343617338, win 14480, options [mss 1460,sackok,ts val 28276952 ecr 15969209,nop,wscale 7], length 0 ..........,........u.p....rw.{/:..8. ..x......... 15:40:01.775312 ip 192.168.3.117.40642 > 192.168.137.2.80: flags [.], ack 1, win 229, options [nop,nop,ts val 15969209 ecr 28276952], length 0 e..4..@.?..z...u.......p.{/:..rx........... ......x. 15:40:01.775952 ip 192.168.3.117.40642 > 192.168.137.2.80: flags [p.], seq 1:305, ack 1, win 229, options [nop,nop,ts val 15969209 ecr 28276952], length 304 e..d..@.?..i...u.......p.{/:..rx....}...... ......x.get /test.jsp http/1.1 content-type: application/x-www-form-urlencoded;charset=utf8 connection: keep-alive user-agent: dalvik/1.6.0 (linux; u; android 4.0.4; shv-e210s build/imm76d) host: 192.168.137.2 accept-encoding: gzip cookie: jsessionid=aa93f68d2c44cdcab57554ab70b91058 content-length: 0 15:40:01.775977 ip 192.168.137.2.80 > 192.168.3.117.40642: flags [.], ack 305, win 122, options [nop,nop,ts val 28276953 ecr 15969209], length 0 ......@.@..[.......u.p....rx.{0j...z ..x..... 15:40:01.776954 ip 192.168.137.2.80 > 192.168.3.117.40642: flags [p.], seq 1:332, ack 305, win 122, options [nop,nop,ts val 28276953 ecr 15969209], length 331 e...*.@.@..........u.p....rx.{0j...zlq..... ..x.....http/1.1 200 ok date: fri, 15 feb 2013 06:40:01 gmt content-type: text/html;charset=utf8 vary: accept-encoding content-encoding: gzip content-length: 113 keep-alive: timeout=5, max=100 connection: keep-alive .............)i-........i.-*j.+.s+.tpkmr04."+..+.c.....#.cc.}.*.....r.v......}..(....z\....j..`...........~..~... 15:40:01.778945 ip 192.168.3.117.40642 > 192.168.137.2.80: flags [.], ack 332, win 245, options [nop,nop,ts val 15969210 ecr 28276953], length 0 e..4..@.?..x...u.......p.{0j..s............ ......x. 15:40:01.781966 ip 192.168.3.117.40642 > 192.168.137.2.80: flags [f.], seq 305, ack 332, win 245, options [nop,nop,ts val 15969210 ecr 28276953], length 0 e..4..@.?..w...u.......p.{0j..s............ ......x. 15:40:01.782049 ip 192.168.137.2.80 > 192.168.3.117.40642: flags [f.], seq 332, ack 306, win 122, options [nop,nop,ts val 28276955 ecr 15969210], length 0 ......@.@..y.......u.p....s..{0k...z ..x..... 15:40:01.783749 ip 192.168.3.117.40642 > 192.168.137.2.80: flags [.], ack 333, win 245, options [nop,nop,ts val 15969211 ecr 28276955], length 0 e..4..@.?..v...u.......p.{0k..s............ ......x.
in first connection, tcp port 48427 used connect server. in next time, expected re-use tcp socket, but, tcp connection created newly tcp port 40642. there miss re-use sockets?
add : found un-expected behavior in tcpdump log. web server sends html page 'keep-alive: timeout=5, max=100' , 'connection: keep-alive' options in html header. but, device sends fin without waiting 5 seconds. suspect device may not able parse keep-alive or timeout options. tested code in galaxy s3 android 4.0.4.
looks bug in android. keep-alive doesn't work gzipped content.
https://code.google.com/p/android/issues/detail?id=43132
java android http
Comments
Post a Comment