2012-09-17 64 views
0

我寫了2個應用程序:一個用於Android系統,使用ServerSocket的:的Android/Windows的TCP客戶機 - 服務器perfformance問題

try { 
    ServerSocket serverSocket = new ServerSocket(3215); 
    while (true) { 
     try { 
      mConnected = false; 
      if (mAllowed) { 
       sendStatusReport(STATUS_IS_ALLOWED); 
      } else { 
       sendStatusReport(STATUS_IS_DENIED); 
      } 
      Socket socket = serverSocket.accept(); 

      wakeLock.acquire(); 
      DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream()); 
      if (mAllowed) { 
       mConnected = true; 
       sendStatusReport(STATUS_IS_CONNECTED); 
       while (mAllowed) { 
        byte[] png = takeScreenshot(); 
        outputStream.writeInt(png.length); 

        outputStream.write(png); 
       } 
       outputStream.writeLong(0); 
      } else { 
       outputStream.writeLong(0); 
      } 
      wakeLock.release(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 

} catch (UnknownHostException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

另一個用於Windows中使用C++:

int recv_count = 0, data_length = 0; 
    const int block_size = 1024*8; 
    unsigned char * data; 
    unsigned char * ptr = data; 
    int received_total = 0; 
    int bytes_to_receive = 0; 

while (received_total < data_length) 
{ 
    if (data_length - received_total < block_size) 
    { 
     bytes_to_receive = data_length - received_total; 
     log::obj()(this, "Left to receive %u bytes of %u, total: %u...", bytes_to_receive, data_length, received_total); 
    } 
    else 
    { 
     bytes_to_receive = block_size; 
    } 

    #define MSG_WAITALL 0x8 
    recv_count = recv(client_socket, (char*)ptr, bytes_to_receive, MSG_WAITALL); 

    if (recv_count != bytes_to_receive) 
    { 
     log::obj()(this, log::warning, "Received %u bytes instead of %u!", recv_count, bytes_to_receive); 
    } 

    ptr += recv_count; 
    received_total += recv_count; 
} 

的問題是,收到客戶端frmaes(〜1MB)在< 2FPS通過WiFi,當屏幕關閉時FPS低至0.2FPS(帶或不帶PARTIAL_WAKE_LOCK)。服務在前臺運行。

log::obj()(this, log::warning, "Received %u bytes instead of %u!", recv_count, bytes_to_receive); 

說,如果沒有MSG_WAITALL客戶端接收〜每recv調用,而不是`BLOCK_SIZE」 500-1000字節。

我是初學者,不明白那個不好的表現的原因。

回答

0

嘗試緩衝您的輸出流,否則,每寫可能會發送一個數據包。

 DataOutputStream outputStream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); 
     if (mAllowed) { 
      mConnected = true; 
      sendStatusReport(STATUS_IS_CONNECTED); 
      while (mAllowed) { 
       byte[] png = takeScreenshot(); 
       outputStream.writeInt(png.length); 

       outputStream.write(png); 
      } 
      outputStream.writeLong(0); 
     } else { 
      outputStream.writeLong(0); 
     } 
     outputStream.flush(); 

改變的唯一行是第一個和最後一個。

+0

謝謝你的回答!測試。沒有結果.... <2 FPS。 png.length接近609849,所以我只有兩個寫... –

+0

有人可以幫忙嗎?代碼非常簡單... –