2014-01-28 67 views
4

我正在開發一個客戶端服務器應用程序,其中服務器(用Java編寫的桌面程序)通過UDP向同一無線網絡中的Android設備發送屏幕截圖。當然,由於數據報大小大於udp標準大小(65 K),我正在創建它們的片段,並使用UDP發送循環到我的Android設備。Android設備(Galaxy Note 2除外)無法獲取UDP數據包

我的問題是,它完美的作品在三星Galaxy Note 2(安卓4.2.2),但不適用於其他Android設備(甚至設備運行相同的Android版本爲前星系標籤3,銀河注1)。代碼卡在socket.receive(數據報)上,甚至沒有收到單個數據包。

有人可以幫助我嗎?

public ClientListener(int port, int fps, AppDelegate del){ 
     delegate = del; 
     framesPerSecond = fps; 

     try{ 

      serverAddr = getLocalIpAddress(); 
      dgp = new DatagramPacket(buf, buf.length); 

     }catch (Exception e){ 
      Log.e("ClientActivity", "C: Error", e); 
     } 
     serverPort = port; 
    } 



    public void run() { 
      try { 
       socket = new DatagramSocket(serverPort, serverAddr); 
       connected = true; 

       Timer timer = new Timer(); 
       int frames = 1000/framesPerSecond; 

       Log.e("FRAMES", ""+frames); 
       timer.scheduleAtFixedRate(getImageTask, 0, frames); 

       listen();    
      } 
      catch (Exception e) { 
       Log.e("ClientActivity", "Client Connection Error", e); 
      } 
    } 



private void listen() 
    { 

     while(connected){ 

      try{ 

       socket.receive(dgp); //gets stuck here on devices other than note 2 




       byte[] data = dgp.getData(); 
       System.out.println("data received in datagram clientListener====="+data); 
      /* Read header infomation */ 
      short session = (short) (data[1] & 0xff); 
      short slices = (short) (data[2] & 0xff); 
      int maxPacketSize = (int) ((data[3] & 0xff) << 8 | (data[4] & 0xff)); // mask 

      short slice = (short) (data[5] & 0xff); 
      int size = (int) ((data[6] & 0xff) << 8 | (data[7] & 0xff)); // mask 



       System.out.println("------------- PACKET -------------"); 
       System.out.println("SESSION_START = " 
         + ((data[0] & SESSION_START) == SESSION_START)); 
       System.out.println("SSESSION_END = " 
         + ((data[0] & SESSION_END) == SESSION_END)); 
       System.out.println("SESSION NR = " + session); 
       System.out.println("SLICES = " + slices); 
       System.out.println("MAX PACKET SIZE = " + maxPacketSize); 
       System.out.println("SLICE NR = " + slice); 
       System.out.println("SIZE = " + size); 
       System.out.println("------------- PACKET -------------\n"); 


      /* If SESSION_START falg is set, setup start values */ 
      if ((data[0] & SESSION_START) == SESSION_START) { 
       if (session != currentSession) { 
        currentSession = session; 
        slicesStored = 0; 
        /* Consturct a appropreately sized byte array */ 
        imageData = new byte[slices * maxPacketSize]; 
        slicesCol = new int[slices]; 
        sessionAvailable = true; 
       } 
      } 

      /* If package belogs to current session */ 
      if (sessionAvailable && session == currentSession) { 
       if (slicesCol != null && slicesCol[slice] == 0) { 
        slicesCol[slice] = 1; 
        System.arraycopy(data, HEADER_SIZE, imageData, slice 
          * maxPacketSize, size); 
        slicesStored++; 
       } 
      } 

      /* If image is complete dispay it */ 
      if (slicesStored == slices) { 
       ByteArrayInputStream bis = new ByteArrayInputStream(
         imageData); 
       Bitmap bp=BitmapFactory.decodeStream(bis); 
         delegate.getController().setImage(bp); 




         Log.e("Testing", "Received image"); 






      } 

回答

0

對不起,我的愚蠢球員。實際上,出於某種奇怪的原因,服務器無法在同一時間迎合來自多個Android設備的多個連接。應該只有一臺設備連接到服務器或請求連接。在我的情況下,我已經通過我的筆記2登錄,並且同時嘗試使用其他設備。所以我的其他設備無法接收數據包。