android - 如何对android中的 "keep-alive"包使用react?

标签 android sockets tcp

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

下面是我的套接字连接的代码段。似乎“keep-alive”数据包无法用输入流读取...

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”数据包是这样的:

Keep-alive packet

最佳答案

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

关于android - 如何对android中的 "keep-alive"包使用react?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26726587/

相关文章:

android - 在 LinearLayout 中获取 View 的位置 (Android)

PHP流套接字不解密数据

java - Java中使用TCP协议(protocol)的多客户端多实例集中式服务器游戏系统

sockets - 从没有数据大小的lua tcp套接字接收数据

java - Labview TCP与Java的连接

java - 如何使用fragment在viewpager中打开sd卡中的多个图像?

android - 如何将一个 Composable 作为其参数传递给另一个 Composable 并在 Jetpack Compose 中显示/运行它

android - 如何访问对话框中的 EditText 字段?

java - UDP 支持多个连接到同一个端口,发送和接收

linux - 传出 TCP 端口匹配监听端口