如何应对“保持活力"在android中打包? [英] how to react to the "keep-alive" package in android?

查看:28
本文介绍了如何应对“保持活力"在android中打包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My android app connects the server with TCP socket, to make sure the connection is ok, the server sends the "keep-alive" msg every 10s when idle, I can grab the packet in WireShark, but in my app, I can't handle the packet anywhere, seems that the packet can't be read with the socket.

下面是我的套接字连接的代码段.似乎无法使用输入流读取保持活动"数据包...

Below is the code segment of my socket connection. Seems that the "keep-alive" packet just can't be read with the 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 中的keep-alive"数据包是这样的:

The "keep-alive" packet in WireShark is like this:

推荐答案

如果您指的是常规 TCP 保持活动状态,那么您无需检测或执行任何操作.您的 TCP 实现负责确认它.里面没有应用程序数据,所以你没有什么可看的.

If you mean a regular TCP keep-alive, there's nothing for you to detect or do. Your TCP implementation takes care of acknowledging it. There's no application data in it, so there's nothing for you to read.

这篇关于如何应对“保持活力"在android中打包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆