当前位置:编程学习 > JAVA >>

android tcp流接收问题

最近在做一个android上的tcp远程图像传输,在虚拟机上测试没问题,但是真机上每张图片都被分了好几次接收导致图片显示不出来,哪位大神知道什么原因么,初学android,代码写的不好。
in = socket.getInputStream();
tcp 接收线程如下
public void run() {
        try {
            while (true) {            
             //byte[] buffer = new byte[50];
             byte[] JpgData = new byte[10240];
            
                if (socket != null && !socket.isClosed()) {// 如果socket没有被关闭
                    if (socket.isConnected()) {// 判断socket是否连接成功
                        if (!socket.isInputShutdown()) {                         
                         if(Mainform.instance.IsJpg())
                         {
                         byte[] buffer = new byte[in.available()];
                         if(in.read(buffer,0,buffer.length)>0)
                                      {   
                         bmp = BitmapFactory.decodeByteArray(JpgData, 0, JpgData.length); 
                         Message msg = Mainform.instance.handler.obtainMessage();
                                      msg.what= 3;
                                      msg.arg1 =1; 
                                      msg.obj =JpgData.length;
                              Mainform.instance.handler.sendMessage(msg);                   
                                      }
                    
                         }
                         else
                         {
                         byte[] buffer = new byte[in.available()];
                         if(in.read(buffer,0,buffer.length)>0)
                                      {
                         System.out.println("in: " + buffer.length);
                                 String msg_str =new String(buffer, 0, buffer.length, "gb2312");
                                 System.out.println(msg_str);
                                 getMessage(msg_str);
                                      }
                         }                                                                        
                                 }                        
                         }
                     }
                 }
         }
    catch (Exception ex) {
            try {
                socket.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
           // workStatus = Constant.TAG_CONNECTFAILURE;// 如果出现异常,提示网络连接出现问题。
            ex.printStackTrace();
        }
    }

底层采用的arm,发送用的
send(ptcpServer->connect_fd,JpegData,sizeof(JpegData),0);
虚拟机上一张图片直接一次就接收完毕,但是再真机上用的3G网络,函数就进去了三次
if(Mainform.instance.IsJpg())
                         {
                         byte[] buffer = new byte[in.available()];
                         if(in.read(buffer,0,buffer.length)>0)
                                      {   
                         bmp = BitmapFactory.decodeByteArray(JpgData, 0, JpgData.length); 
                         Message msg = Mainform.instance.handler.obtainMessage();
                                      msg.what= 3;
                                      msg.arg1 =1; 
                                      msg.obj =JpgData.length;
                              Mainform.instance.handler.sendMessage(msg);                   
                                      }
                    
                         }

导致bmp获取失败,这个应该怎么处理才行,初学android一个月,很多都不懂,麻烦详细点,谢啦
补充:Java ,  Eclipse
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,