小米手机TCP连接一些奇怪现象

在小米手机上,一个Android TCP客户端在熄屏后经历3分钟的有效连接时间问题。重新启动手机或修改应用程序包名会影响连接持续时间。将应用锁定在后台可以防止被杀死,但问题仍然存在。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

小米手机tcp连接一些奇怪现象

本人写了一个android TCP 客户端一个例子,以TCP&UDP测试工具模拟服务器,每隔一段时间(30S)向手机客户端发送一个字符串。我手上有一台红米note3作为测试机,编译后在手机安装,熄灭屏幕后测试tcp一直处于连接状态。可是我做如下操作会出现一些奇怪的现象(小米机型都出现):

  1. 如果我将手机重新启动后再打开TcpTest app,熄灭屏幕后,发现这个时候长连接大概只有3分钟的有效时间,测试每次必现。
  2. 这个时候如果将AndroidManifest.xml 的包名随意改动下,重新再编译安装,这个时候tcp就又可以一直连接了,但是关机重启后又只能连接两三分钟了。

测试例子下载

以上测试都是app运行后熄灭屏进行的,为了不让app进程被kill掉,请下拉应用将APP锁上 如下图
如图

tcp连接代码 TcpSocket.java


package com.example.tcpsocket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import android.util.Log;

public class TcpSocket {
    private DataOutputStream out;// 发送数据流
    private DataInputStream in;// 接收数据流
    private Socket mSocket;// socket连接对象
    private SocketAddress address;
    private int timeOut = 1000 * 30;// 延迟时间
    // 启动一个线程,不停接收服务器数据
    private RecThrad recThread;// 接收数据线程
    private SendThread sendThread;// 发送线程
    private ConnectThread connThread;//
    private boolean threadBoo = true;
    private TCPSocketCallback callBack;// 回调接口
    private byte[] buffer = new byte[1024 * 1];// 缓冲区字节数组,信息不能大于此缓冲区
    private byte[] tmpBuffer;// 临时缓冲区
    private static List<byte[]> datas = new ArrayList<byte[]>();// 待发送数据队列

    // 构造
    public TcpSocket(TCPSocketCallback callback) {
        // TODO Auto-generated constructor stub
        this.callBack = callback;

    }

    // 开始连接
    public void startConnect(String ip, int port) {
        // 启动连接线程
        connThread = new ConnectThread(ip, port);
        connThread.start();
    }

    // 获取当前连接状态
    public boolean getConnectStatus() {
        if (mSocket != null)
            return mSocket.isConnected();
        else
            return false;
    }

    public void sendData(byte[] data) {
        if (out != null) {
            try {
                out.write(data);
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
                callBack.disconnect();
            }

        }
    }

    public void writeDate(byte[] data) {
        datas.add(data);// 将发送数据添加到发送队列
    }

    class ConnectThread extends Thread {
        String ip;
        int port;

        public ConnectThread(String ip, int port) {
            this.ip = ip;
            this.port = port;
        }

        @Override
        public void run() {
            super.run();
            mSocket = new Socket();
            address = new InetSocketAddress(ip, port);
            try {
                mSocket.connect(address, timeOut);
                mSocket.isConnected();
                callBack.connected();
                out = new DataOutputStream(mSocket.getOutputStream());// 获取网络输出流
                in = new DataInputStream(mSocket.getInputStream());// 获取网络输入流
                threadBoo = true;
                //启动接收和发送数据线程
                recThread = new RecThrad();
                recThread.start();
                sendThread = new SendThread();
                sendThread.start();

            } catch (IOException e1) {
                e1.printStackTrace();
                try {
                    if (out != null) {
                        out.close();
                    }
                    if (in != null) {
                        in.close();
                    }
                    if (mSocket != null && !mSocket.isClosed()) {// 判断socket不为空并且是连接状态
                        mSocket.close();// 关闭socket
                    }
                } catch (Exception e2) {
                    // TODO: handle exception
                }
                if (callBack != null)
                    callBack.disconnect();
            }
        }
    }

    /**
     * 发送线程
     */
    class SendThread extends Thread {
        @Override
        public void run() {
            super.run();
            while (threadBoo) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (datas.size() > 0) {
                    byte[] data = datas.remove(0);
                    sendData(data);
                }
            }
            this.close();
        }

        public void close() {
            threadBoo = false;
        }
    }

    /**
     * 接收数据线程 关闭资源 打开资源
     */
    class RecThrad extends Thread {

        public void run() {
            super.run();
            if (threadBoo) {
                if (in != null) {
                    int len = 0;
                    try {
                        while ((len = in.read(buffer)) > 0) {
                            tmpBuffer = new byte[len];
                            System.arraycopy(buffer, 0, tmpBuffer, 0, len);
                            callBack.receive(tmpBuffer);
                            tmpBuffer = null;
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                        try {
                            if (out != null) {
                                out.close();
                            }
                            if (in != null) {
                                in.close();
                            }
                            if (mSocket != null && !mSocket.isClosed()) {// 判断socket不为空并且是连接状态
                                mSocket.close();// 关闭socket
                            }
                        } catch (Exception e2) {
                            // TODO: handle exception
                        }
                        if (callBack != null)
                            callBack.disconnect();
                    }
                }
            }
        }

        public void close() {
            threadBoo = false;
            this.close();
        }
    }

    // 关闭所有资源
    public void close() {
        threadBoo = false;
        try {
            if (mSocket != null) {
                if (!mSocket.isInputShutdown()) {
                    mSocket.shutdownInput();
                }
                if (!mSocket.isOutputShutdown()) {
                    mSocket.shutdownOutput();
                }
            }
            if (mSocket != null && !mSocket.isClosed()) {// 判断socket不为空并且是连接状态
                mSocket.close();// 关闭socket
            }
            if (out != null) {
                out.close();
            }
            if (in != null) {
                in.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out = null;
            in = null;
            mSocket = null;// 制空socket对象
            recThread = null;
            sendThread = null;
            connThread = null;
            callBack = null;
        }
    }
}

启动tcp代码

void starTcp(){
        if(tcp == null){
            tcp = new TcpSocket(new TCPSocketCallback() {

                @Override
                public void receive(byte[] buffer) {
                    tcp.writeDate(buffer);
                }

                @Override
                public void disconnect() {

                }

                @Override
                public void connected() {

                }
            });
            tcp.startConnect(ip, port);

        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值