2011-06-19 82 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,如果你还没有。

相关问题