前言
平時(shí)日常開發(fā)用得最多是Http通訊,接口調(diào)試也比較簡(jiǎn)單的,也有比較強(qiáng)大的框架支持(OkHttp)。
個(gè)人平時(shí)用到socket通訊的地方是Android與外設(shè)通訊,Android與ssl服務(wù)通訊,這種都是基于TCP/IP通訊,而且服務(wù)端和設(shè)備端協(xié)議都是不能修改的,只能按照相關(guān)報(bào)文格式進(jìn)行通信。
但使用socket通訊問題不少,一般有兩個(gè)難點(diǎn):
1、socket通訊層要自己寫及IO流不正確使用,遇到讀取不到數(shù)據(jù)或者阻塞卡死現(xiàn)象或者數(shù)據(jù)讀取不完整
2、請(qǐng)求和響應(yīng)報(bào)文格式多變(json,xml,其它),解析麻煩,如果是前面兩種格式都簡(jiǎn)單,有對(duì)應(yīng)框架處理,其它格式一般都需要自己手動(dòng)處理。
本次基于第1點(diǎn)問題做了總結(jié),歸根結(jié)底是使用read()或readLine()導(dǎo)致的問題
Socket使用流程
1、創(chuàng)建socket
2、連接socket
3、獲取輸入輸出流
字節(jié)流:
1
2
|
InputStream mInputStream = mSocket.getInputStream(); OutputStream mOutputStream = mSocket.getOutputStream(); |
字符流:
1
2
|
BufferedReader mBufferedReader = new BufferedReader( new InputStreamReader(mSocket.getInputStream(), "UTF-8" )); PrintWriter mPrintWriter = new PrintWriter( new BufferedWriter( new OutputStreamWriter(mSocket.getOutputStream(), "UTF-8" )), true ); |
至于實(shí)際使用字節(jié)流還是字符流,看實(shí)際情況使用。如果返回是字符串及讀寫與報(bào)文結(jié)束符(/r或/n或/r/n)有關(guān),使用字符流讀取,否則字節(jié)流。
4、讀寫數(shù)據(jù)
5、關(guān)閉socket
如果是Socket短連接,上面五個(gè)步驟都要走一遍;
如果是Socket長連接,只需關(guān)注第4點(diǎn)即可,第4點(diǎn)使用不慎就會(huì)遇到上面出現(xiàn)的問題。
實(shí)際開發(fā)中,長連接使用居多,一次連接,進(jìn)行多次收發(fā)數(shù)據(jù)。
特別注意:使用長連接不能讀完數(shù)據(jù)后立馬關(guān)閉輸入輸出流,必須再最后不使用的時(shí)候關(guān)閉
Socket數(shù)據(jù)讀寫
當(dāng)socket阻塞時(shí),必須設(shè)置讀取超時(shí)時(shí)間,防止調(diào)試時(shí),socket讀取數(shù)據(jù)長期掛起。
1
|
mSocket.setSoTimeout( 10 * 1000 ); //設(shè)置客戶端讀取服務(wù)器數(shù)據(jù)超時(shí)時(shí)間 |
使用read()讀取阻塞問題
日常寫法1:
1
2
3
4
5
6
7
8
9
10
11
|
mOutputStream.write(bytes); mOutputStream.flush(); byte [] buffer = new byte [ 1024 ]; int n = 0 ; ByteArrayOutputStream output = new ByteArrayOutputStream(); while (- 1 != (n = mInputStream .read(buffer))) { output.write(buffer, 0 , n); } //處理數(shù)據(jù) output.close(); byte [] result = output.toByteArray(); |
上面看似沒有什么問題,但有時(shí)候會(huì)出現(xiàn)mInputStream .read(buffer)阻塞,導(dǎo)致while循環(huán)體里面不會(huì)執(zhí)行
日常寫法2:
1
2
3
4
5
|
mOutputStream.write(bytes); mOutputStream.flush(); int available = mInputStream.available(); byte [] buffer = new byte [available]; in.read(buffer); |
上面雖然不阻塞,但不一定能讀取到數(shù)據(jù),available 可能為0,由于是網(wǎng)絡(luò)通訊,發(fā)送數(shù)據(jù)后不一定馬上返回。
或者對(duì)mInputStream.available()修改為:
1
2
3
4
|
int available = 0 ; while (available == 0 ) { available = mInputStream.available(); } |
上面雖然能讀取到數(shù)據(jù),但數(shù)據(jù)不一定完整。
而且,available方法返回估計(jì)的當(dāng)前流可用長度,不是當(dāng)前通訊流的總長度,而且是估計(jì)值;read方法讀取流中數(shù)據(jù)到buffer中,但讀取長度為1至buffer.length,若流結(jié)束或遇到異常則返回-1。
最終寫法(遞歸讀?。?/strong>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
/** * 遞歸讀取流 * * @param output * @param inStream * @return * @throws Exception */ public void readStreamWithRecursion(ByteArrayOutputStream output, InputStream inStream) throws Exception { long start = System.currentTimeMillis(); while (inStream.available() == 0 ) { if ((System.currentTimeMillis() - start) > 20 * 1000 ) { //超時(shí)退出 throw new SocketTimeoutException( "超時(shí)讀取" ); } } byte [] buffer = new byte [ 2048 ]; int read = inStream.read(buffer); output.write(buffer, 0 , read); SystemClock.sleep( 100 ); //需要延時(shí)以下,不然還是有概率漏讀 int a = inStream.available(); //再判斷一下,是否有可用字節(jié)數(shù)或者根據(jù)實(shí)際情況驗(yàn)證報(bào)文完整性 if (a > 0 ) { LogUtils.w( "========還有剩余:" + a + "個(gè)字節(jié)數(shù)據(jù)沒讀" ); readStreamWithRecursion(output, inStream); } } /** * 讀取字節(jié) * * @param inStream * @return * @throws Exception */ private byte [] readStream(InputStream inStream) throws Exception { ByteArrayOutputStream output = new ByteArrayOutputStream(); readStreamWithRecursion(output, inStream); output.close(); int size = output.size(); LogUtils.i( "本次讀取字節(jié)總數(shù):" + size); return output.toByteArray(); } |
上面這種方法讀取完成一次后,固定等待時(shí)間,等待完不一定有數(shù)據(jù),若沒有有數(shù)據(jù),響應(yīng)時(shí)間過長,會(huì)影響用戶體驗(yàn)。我們可以再優(yōu)化一下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
/** * 遞歸讀取流 * * @param output * @param inStream * @return * @throws Exception */ public void readStreamWithRecursion(ByteArrayOutputStream output, InputStream inStream) throws Exception { long start = System.currentTimeMillis(); int time = 500 ; //毫秒,間看實(shí)際情況 while (inStream.available() == 0 ) { if ((System.currentTimeMillis() - start) >time) { //超時(shí)退出 throw new SocketTimeoutException( "超時(shí)讀取" ); } } byte [] buffer = new byte [ 2048 ]; int read = inStream.read(buffer); output.write(buffer, 0 , read); int wait = readWait(); long startWait = System.currentTimeMillis(); boolean checkExist = false ; while (System.currentTimeMillis() - startWait <= wait) { int a = inStream.available(); if (a > 0 ) { checkExist = true ; // LogUtils.w("========還有剩余:" + a + "個(gè)字節(jié)數(shù)據(jù)沒讀"); break ; } } if (checkExist) { if (!checkMessage(buffer, read)) { readStreamWithRecursion(output, inStream, timeout); } } } /** * 讀取等待時(shí)間,單位毫秒 */ protected int readWait() { return 100 ; } /** * 讀取字節(jié) * * @param inStream * @return * @throws Exception */ private byte [] readStream(InputStream inStream) throws Exception { ByteArrayOutputStream output = new ByteArrayOutputStream(); readStreamWithRecursion(output, inStream); output.close(); int size = output.size(); LogUtils.i( "本次讀取字節(jié)總數(shù):" + size); return output.toByteArray(); } |
上面這種延遲率大幅降低,目前正在使用該方法讀取,再也沒有出現(xiàn)數(shù)據(jù)讀取不完整和阻塞現(xiàn)象。不過這種,讀取也要注意報(bào)文結(jié)束符問題,何時(shí)讀取完畢問題。
使用readreadLine()讀取阻塞問題
日常寫法:
1
2
3
4
|
mPrintWriter.print(sendData+ "\r\n" ); mPrintWriter.flush(); String msg = mBufferedReader.readLine(); //處理數(shù)據(jù) |
細(xì)心的你發(fā)現(xiàn),發(fā)送數(shù)據(jù)時(shí)添加了結(jié)束符,如果不加結(jié)束符,導(dǎo)致readLine()阻塞,讀不到任何數(shù)據(jù),最終拋出SocketTimeoutException異常
特別注意:
報(bào)文結(jié)束符:根據(jù)實(shí)際服務(wù)器規(guī)定的來添加,必要時(shí)問后端開發(fā)人員或者看接口文檔是否有說明
不然在接口調(diào)試上會(huì)浪費(fèi)很多寶貴的時(shí)間,影響后期功能開發(fā)。
使用readLine()注意事項(xiàng):
- 1、讀入的數(shù)據(jù)要注意有/r或/n或/r/n
這句話意思是服務(wù)端寫完數(shù)據(jù)后,會(huì)打印報(bào)文結(jié)束符/r或/n或/r/n;
同理,客戶端寫數(shù)據(jù)時(shí)也要打印報(bào)文結(jié)束符,這樣服務(wù)端才能讀取到數(shù)據(jù)。
- 2、沒有數(shù)據(jù)時(shí)會(huì)阻塞,在數(shù)據(jù)流異常或斷開時(shí)才會(huì)返回null
- 3、使用socket之類的數(shù)據(jù)流時(shí),要避免使用readLine(),以免為了等待一個(gè)換行/回車符而一直阻塞
上面長連接是發(fā)送一次數(shù)據(jù)和讀一次數(shù)據(jù),保證了當(dāng)次通訊的完整性,必須要時(shí)需要同步處理。
也有長連接,客戶端開線程循環(huán)阻塞等待服務(wù)端數(shù)據(jù)發(fā)送數(shù)據(jù)過來,比如:消息推送。平時(shí)使用長連接都是分別使用不同的命令發(fā)送數(shù)據(jù)且接收數(shù)據(jù),來完成不同的任務(wù)。
總結(jié)
實(shí)際開發(fā)中,長連接比較復(fù)雜,還要考慮心跳,丟包,斷開重連等問題。使用長連接時(shí),要特別注意報(bào)文結(jié)束符問題,結(jié)束符只是用來告訴客戶端或服務(wù)端數(shù)據(jù)已經(jīng)發(fā)送完畢,客戶端或服務(wù)端可以讀取數(shù)據(jù)了,否則客戶端或服務(wù)端會(huì)一直阻塞在read()或者readLine()方法。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/u011082160/article/details/100779231