2011-06-19 27 views
2

我在Android和C#中有一個客戶端,它們通過套接字進行通信。 我有這個問題 - 如果我在調試模式下運行我的客戶端應用程序,並將斷點放在正確的位置 - 它完美的工作,但沒有它它不。 客戶端將圖像的地址發送到服務器,服務器將其縮略圖,將其轉換爲字節[]並將其發回。客戶端獲取字節[],將其轉換回圖像並顯示出來。 我也發現,當它沒有得到正確的byte []時,它的大小是2896,有時是1448,不管發送數組的原始大小是多少。爲什麼應用程序在調試過程中工作但在運行時無法工作?

這裏的客戶機:

private void connectSocket(String a){ 

    try { 
     InetAddress serverAddr = InetAddress.getByName("192.168.1.2"); 
     Socket socket = new Socket(serverAddr, 4444); 
     String message = a; 
     flag = 0; 
     if(a.indexOf(".jpg")>0){ 
      flag = 1; 
     } 

     ListItems.clear(); 
     if(!a.equalsIgnoreCase("get_drives"))){ 
        //..... 
     } 
     if(!ListItems.isEmpty()){    
      if(ListItems.get(0).matches("^[A-Z]{1}:$")){ 
       ListItems.set(0, ListItems.get(0)+"\\"); 
      } 
     } 
     PrintWriter out = null; 
     BufferedReader in = null; 
     InputStream is = null;   
     try { 
      out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true); 
      in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
      is = socket.getInputStream(); 
      out.println(message); 

      String text = ""; 
      String finalText = ""; 
      if(flag!=1){ 
       while ((text = in.readLine()) != null) { 
        finalText += URLDecoder.decode(text, "UTF-8"); 
        } 
       String[] result = finalText.split("#"); 
       for(int i = 0; i<result.length; i++) 
        ListItems.add(result[i]); 
       } 
      if(flag ==1){     

        byte[] buffer = new byte[9999]; 
       //placing breakpoint at the next line or before it makes it work fine      
        int size = is.read(buffer); 
       //but placing a breakpoint at the line below doesn't make it work 
       //it starts getting another byte array 
        byte[] bufffer2 = new byte[size]; 
        for(int g = 0; g<size;g++){ 
         bufffer2[g] = buffer[g]; 
        } 
        //is.read(bufffer2); 
        //int read = is.read(buffer); 

       image = (ImageView)findViewById(R.id.imageView1); 
       //while ((text = in.readLine()) != null) { 
       // byte[] b = in.readLine().getBytes(); 
        Bitmap bmp=BitmapFactory.decodeByteArray(bufffer2,0,bufffer2.length);      
        image.setImageBitmap(bmp); 
       //} 
      } 
      adapter.notifyDataSetChanged(); 

     } catch(Exception e) { 
      Log.e("TCP", "S: Error", e); 
     } finally { 
      socket.close(); 
     } 

    } catch (UnknownHostException e) { 
     Log.e("TCP", "C: UnknownHostException", e); 
     e.printStackTrace(); 
    } catch (IOException e) { 
     Log.e("TCP", "C: IOException", e); 
     e.printStackTrace(); 
    }  
} 

這裏的服務器:在InputStream的

public class serv { 
public static void Main() { 
try { 
    IPAddress ipAd = IPAddress.Parse("192.168.1.2"); 
    TcpListener myList=new TcpListener(ipAd,4444); 

    m:  
    myList.Start(); 

    Socket s=myList.AcceptSocket(); 

    byte[] b=new byte[100]; 
    int k=s.Receive(b); 

    char cc = ' '; 
    string test = null; 
    Console.WriteLine("Recieved..."); 
    for (int i = 0; i < k-1; i++) 
    { 
     Console.Write(Convert.ToChar(b[i])); 
     cc = Convert.ToChar(b[i]); 
     test += cc.ToString(); 
    } 

    string[] response = null; 
    ASCIIEncoding asen = new ASCIIEncoding(); 
    switch (test) 
    { 
     default: 
      MyExplorer(test, s); 
      break; 

    } 

    s.Close(); 
    myList.Stop(); 
    goto m; 

} 
catch (Exception e) { 
    Console.WriteLine("Error..... " + e.StackTrace);   
}  
} 

public static void MyExplorer(string r, Socket s){ 
    Image imgThumb = null; 
    if (r.Contains(".jpg")) 
    { 
     Image image = null; 
     image = Image.FromFile(r); 
     // Check if image exists 
     if (image != null) 
     { 
      imgThumb = image.GetThumbnailImage(100, 100, null, new IntPtr()); 
      s.Send(imageToByteArray(imgThumb)); 
      byte[] b = imageToByteArray(imgThumb); 
     } 
     return; 
    } 

} 

public static byte[] imageToByteArray(System.Drawing.Image imageIn) 
{ 
    MemoryStream ms = new MemoryStream(); 
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); 
    return ms.ToArray(); 
} 

} 

回答

4

讀(字節[])方法讀取的字節數當前可用。這並不意味着稍後將不會有更多字節可用。所以,你需要做的是這樣

while(true) { 
    int s = is.read(buffer); 
    if(s == -1) break; 

    // else 
    add read buffer to image array 
} 

extract image from image array 

其中「圖像陣列」是一些字節數組,你不斷增加讀取緩衝區,直到你到達然後結束流。

你的代碼與斷點一起工作的原因是,當你通過調試器時,整個流是可用的,而在非調試時,當你進行讀操作時,整個流是不可用的。

+1

,,。最後一句話做了訣竅.. 2 f ## ng天,同樣的問題.. – ngesh

1

這種差異很可能是由於在調試器上,您可以減慢和停止應用程序 - 這會給服務器響應您的呼叫留出時間。它看起來像你沒有使用任何異步方法來調用連接套接字(嗯,我無法看到你的代碼中的任何證據)。
要解決這個問題,你應該嘗試擴展AsyncTask對象examples here
編輯:@Carsten可能有正確的解決方案,但你應該仍然使用AsyncTask,如果你還沒有。

相關問題