(转)Android 客户端与PC服务端socket通信接收与发送图片

Android 客户端与PC服务端socket通信接收与发送图片(终结者)

https://blog.csdn.net/CSDN_zhi/article/details/71077050

android使用socket实现实时视频

https://www.cnblogs.com/lijiongquan/p/4729445.html

视频多少帧时才不卡顿

https://blog.csdn.net/XYKenny/article/details/94589494

app接收并显示视频:

public void OpenTCP() {
        System.out.println("***startTCP***" + Ip + ":" + tcpPort);
        tcpsendData = "1";
        try {
            tcpSocket = new Socket(Ip, Integer.parseInt(tcpPort));
//            tcpSocket.setSoTimeout(1000);
        } catch (Exception ex) {
            Log.e("TCP连接错误:", "TCP连接错误");
            Log.e("TCP连接错误:", ex.toString());
            ex.printStackTrace();
        }
        if (tcpSocket != null) {
            new Thread(TcpReceiver).start();
        }

    }

    Runnable TcpReceiver = new Runnable() {
        @Override
        public void run() {
byte[] buffer0=null;
            byte[] buffer=null;

            System.out.println("***startTCP---tcpPort***" + tcpPort);
            while (true) {

                try {
//                    tcpSocket = new Socket(Ip, Integer.parseInt(tcpPort));
                    //接收数据
                    buffer0 = new byte[10];
                    InputStream inputStream0 = tcpSocket.getInputStream();
                    DataInputStream input0 = new DataInputStream(inputStream0);
                    int length0 = input0.read(buffer0);
                    if (length0 <10) {
                        //发送数据
           
                        OutputStream OutStream = tcpSocket.getOutputStream();
                        OutStream.write("1".getBytes());
                        OutStream.flush();
      

                        String lengthMsg = new String(buffer0, 0, length0, "gb2312");
                        Log.e("lengthMsg:", "" + lengthMsg);
                        int size = Integer.parseInt(lengthMsg);
                        buffer = new byte[size];
                        int len = 0;
                        int offset=0;
                        while (len < size) {
                            offset=size-len;
                            if(offset>=1024*5){
                                offset=1024*5;
                            }
                            len += input0.read(buffer, len, offset);
//                            System.out.println("***len***" + len);
                        }
                        System.out.println("***buffer.length***" + buffer.length);
                        final Bitmap bitmap = BitmapFactory.decodeByteArray(buffer, 0, buffer.length);

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                image.setImageBitmap(bitmap);
                           }
                        });
                       //                        if (!bitmap.isRecycled()) {
//                            bitmap.recycle();
//                        }
                    } else {
                        continue;
                    }
//                    tcpSocket.close();

                } catch (IOException e) {
                    Log.e("接收错误:", e.toString());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity_tcp.this, "网络接收断开", Toast.LENGTH_SHORT).show();
                        }
                    });
                    try {
                        tcpSocket = new Socket(Ip, Integer.parseInt(tcpPort));
                    }catch (IOException e1) {
                        e1.printStackTrace();
                    }

                }
            }

        }
    };

猜你喜欢

转载自www.cnblogs.com/cekong/p/11504334.html