[問題] SocketChannel連續送request反應時間過長

看板Programming作者 (fish)時間11年前 (2013/12/16 11:11), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
各位前輩大家好,小弟在此請教大家有關Android SocketChannel 使用上的一些問題,以下是程式需求以及遇到的瓶頸: 1. 在一遠端系統上撰寫了伺服器控制照相機,固定輸出QQVGA尺寸 、JPEG格式的圖檔,只要有客戶端程式發送需求,就擷取一張圖 片回傳給客戶端,之後關閉連線,由於考慮到JPEG最複雜的檔案 ,所以伺服器端截取的圖檔最大尺寸為20K bytes,後續JPEG圖 檔的頭尾交由客戶端處理搜尋。 2. 目前在Android系統開發客戶端程式,版本為4.X,小弟先使用按 鈕控制遠端截取一張圖片做測試,平均送收一張花費的時間大約 140~170ms;之後手動連續點按鈕驗證是否可以連續收發,至此 動作也都很正常,SocketChannel反應時間也大約在150~170ms。 以下為SocketChannel實做的部分程式碼: socketChannel = SocketChannel.open(); socketChannel.configureBlocking(true); socketChannel.connect(new InetSocketAddress("192.168.1.20", TCP_SERVER_PORT)); ByteBuffer buf = ByteBuffer.allocate(1024); buf.put((byte)outPut_cmd); buf.flip(); if(outPut_cmd==49){ bytesRead = 0; socketChannel.write(buf); inPut_buf.limit(20480); while(bytesRead<20480){ bytesRead = bytesRead + socketChannel.read(inPut_buf); } /* record tcp receive spend time */ ProcessTime = System.currentTimeMillis() - StartTime; Log.i("TcpClient", "Process time = " + ProcessTime + "ms"); // inPut_buf.flip(); // location = BytesIndexOf(inPut_buf.array(), jpegStart, 0); // Log.i("TcpClient", "jpegStart location = " + location.intValue()); // Log.i("TcpClient", "start location = " + location.intValue()); inPut_buf.flip(); location = BytesIndexOf(inPut_buf.array(), jpegEnd, 0); fileSize = location+2; inPut_buf.flip(); fileName = "hm5065_"+String.format("%04d", fileCounter)+".jpg"; /* record jpeg size and use ram buffer index number */ countBundle = new Bundle(); countBundle.putString("file_name", fileName); countBundle.putInt("jpeg_size", fileSize); countBundle.putString("save_type", save_file_hanlder); /* store bundle to message */ message = new Message(); message.setData(countBundle); handler.sendMessage(message); /* clear inPut_buffer */ inPut_buf.clear(); inPut_buf.flip(); fileSize = 0; fileCounter++; 3. 實做Service,在背景執行客戶端擷取圖片的服務,起動service以後 只有第一次的收送時間是150ms,第二次之後的收發時間平均都在三 秒左右,爾後都沒有再回到150ms的水準。 在這裡想請教前輩們幾個問題: a. Android的網路客戶端在使用sevice實做時,連續發送client request 造成reqponse反應時間變長是什麼原因造成的?但使用按鈕發送客戶端的 請求沒有反應時間過長的情況,按鈕間隔大約都到一秒。 b. 小弟在Service thread中也加入了thead.sleep去間隔,確認不是client request太過頻繁所造成,當sleep時間加到500ms左右,反應時間減少 到約2秒左右。 最後小弟附上原始碼連結,懇請版上各位前輩不吝賜教,謝謝。 原始碼下載點:http://ppt.cc/SPhy -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 42.65.58.1 ghostfishM:轉錄至看板 AndroidDev 12/16 15:36
文章代碼(AID): #1Ihcz7MP (Programming)
文章代碼(AID): #1Ihcz7MP (Programming)