Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何回应";“保持活力”;安卓软件包?_Android_Sockets_Tcp - Fatal编程技术网

Android 如何回应";“保持活力”;安卓软件包?

Android 如何回应";“保持活力”;安卓软件包?,android,sockets,tcp,Android,Sockets,Tcp,我的android应用程序使用TCP套接字连接服务器,为了确保连接正常,服务器空闲时每10秒发送一次“keep alive”消息,我可以在WireShark中抓取数据包,但在我的应用程序中,我无法在任何地方处理数据包,似乎无法使用套接字读取数据包 下面是我的套接字连接的代码段。似乎“保持活动”数据包无法用inputstream读取 public class SocketBase { private Socket mSocket; private DataOutputStream

我的android应用程序使用TCP套接字连接服务器,为了确保连接正常,服务器空闲时每10秒发送一次“keep alive”消息,我可以在WireShark中抓取数据包,但在我的应用程序中,我无法在任何地方处理数据包,似乎无法使用套接字读取数据包

下面是我的套接字连接的代码段。似乎“保持活动”数据包无法用inputstream读取

public class SocketBase {
    private Socket mSocket;
    private DataOutputStream out;
    private DataInputStream in;
    private SocketCallback callback;
    private int timeOut = 1000 * 30;


    public SocketBase(SocketCallback callback) {
        this.callback = callback;
    }


    public void connect(String ip, int port) throws Exception {
        mSocket = new Socket();
        SocketAddress address = new InetSocketAddress(ip, port);
        mSocket.connect(address, timeOut);
        if (mSocket.isConnected()) {
            out = new DataOutputStream(mSocket.getOutputStream());
            in = new DataInputStream(mSocket.getInputStream());
            callback.connected();
        }
    }


    public void write(byte[] buffer) throws IOException {
        if (out != null) {
            out.write(buffer);
            out.flush();
        }
    }


    public void disconnect() {
        try {
            if (mSocket != null) {
                if (!mSocket.isInputShutdown()) {
                    mSocket.shutdownInput();
                }
                if (!mSocket.isOutputShutdown()) {
                    mSocket.shutdownOutput();
                } 
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
                mSocket.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            callback.disconnect();
            out = null;
            in = null;
            mSocket = null;
        }
    }


    public void read() throws IOException {
        if (in != null) {
            byte[] buffer = new byte[1024*1];
            byte[] tmpBuffer;
            int len = 0;
            Log.i("SOCKET", "something comming");
            while ((len = in.read(buffer)) > 0) {
                tmpBuffer = new byte[len];
                System.arraycopy(buffer, 0, tmpBuffer, 0, len);
                callback.receive(tmpBuffer);
                tmpBuffer = null;
            }
        }
    }
}
WireShark中的“保持活动”数据包如下所示:


如果您指的是常规TCP保持活动状态,则您无需检测或执行任何操作。您的TCP实现负责确认它。其中没有应用程序数据,因此没有任何内容可供阅读。

不清楚您在说什么。你说的“保命包”是什么意思?你真的是说定期的TCP保持活力吗?你100%确定吗?我想做的是检测“保持活动”并确定客户端是否与服务器失去连接,现在当我关闭服务器时,实际上连接已丢失,但没有任何内容“通知”客户端,以便我可以“最终”处理它。@smallzhan哦,这不可能工作。您需要尝试将数据发送到服务器或自己发送keep alives。