我想重新使用由HttpURLConnection創建的套接字。但我發現當我調查從遠程Web服務器傳入/傳出的原始數據包時,它始終會創建新的套接字。 以下是我的代碼來訪問我的遠程Web服務器。在Android中使用HttpURLConnection重新使用套接字
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..<[email protected]?.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
[email protected]?.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../[email protected]?.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
[email protected]@.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
[email protected]@.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
[email protected]?.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
[email protected]?.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
[email protected]@.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
[email protected]?.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..<[email protected]?..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
[email protected]?..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
[email protected]?..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
[email protected]@..[.......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...*[email protected]@..........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
[email protected]?..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
[email protected]?..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
[email protected]@..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
[email protected]?..v...u.......P.{0k..S............
......x.
在第一次連接時,TCP端口48427用於連接服務器。在下一次,我希望重新使用TCP套接字,但是,TCP連接新創建與TCP端口40642.有什麼我想念重新使用套接字?
ADD: 我在tcpdump日誌中發現了一些不期望的行爲。 Web服務器發送HTML頁面中的'Keep-Alive:timeout = 5,max = 100'和'Connection:Keep-Alive'選項。 但是,設備立即發送FIN而無需等待5秒鐘。我懷疑設備可能無法解析保持活動或超時選項。我使用Android 4.0.4在Galaxy S3中測試了這些代碼。
嘗試沒有固定長度流模式。 – EJP 2013-02-15 09:43:55
謝謝你的意見,EJP。刪除固定長度的流模式是行不通的。 – chun 2013-02-17 01:11:44