我想重新使用由HttpURLConnection创建的套接字。但我发现当我调查从远程Web服务器传入/传出的原始数据包时,它总是会创建新的套接字。 以下是我访问远程Web服务器的代码。
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 is = 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();
return buffer.toString();
}
并且两次调用该函数。
try {
java.net.CookieManager cookieManager = new java.net.CookieManager();
System.setProperty("http.keepAlive","true");
getFromServer();
getFromServer();
} catch (Exception e) {
e.printStackTrace();
}
如您所见,我的删除服务器IP为192.168.137.2。我的设备IP是192.168.3.117。我预计在尝试此代码时,服务器和设备之间只有一个TCP连接。但是有两次TCP连接与设备中的不同TCP端口。以下是tcpdump登录服务器。
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.
在第一个连接中,TCP端口48427用于连接服务器。在下一次,我期望重新使用TCP套接字,但是,TCP端口40642新创建的TCP连接。有什么我想错过重新使用套接字吗?
ADD: 我在tcpdump日志中发现了一些不期望的行为。 Web服务器在html标头中发送带有“Keep-Alive:timeout = 5,max = 100”和“Connection:Keep-Alive”选项的html页面。 但是,设备立即发送FIN而不等待5秒。我怀疑设备可能无法解析保持活动或超时选项。我在Android 4.0.4的Galaxy S3中测试了这段代码。
答案 0 :(得分:2)
看起来这是Android中的一个错误。 Keep-Alive不适用于gzip压缩内容。