gpt4 book ai didi

java - 安卓服务器: why it times out incoming connections randomly?

转载 作者:太空宇宙 更新时间:2023-11-04 11:45:04 25 4
gpt4 key购买 nike

我必须创建必须监听传入连接的 Android 服务器应用程序。它是非根应用程序,它监听随机高端口。该代码很简单,并且在桌面 Java 上运行良好,它只是监听端口并实现非常简单的自定义请求/响应协议(protocol)。

但是我发现,出于某种原因,即使 Android 应用程序现在处于 accept 方法中(即它应该等待传入连接),连接也经常超时。

我还发现,有时甚至系统应用程序(例如 ADB 服务器)也会无缘无故地定期超时传入连接。请参阅 paping 输出示例:

paping -p 5555 192.168.0.105
paping v1.5.5 - Copyright (c) 2011 Mike Lovell

Connecting to 192.168.0.105 on TCP 5555:

Connection timed out
Connection timed out
Connection timed out
Connection timed out
Connection timed out
Connection timed out
Connection timed out
Connection timed out
Connection timed out
Connection timed out
Connection timed out
Connection timed out
Connection timed out
Connection timed out
Connection timed out
Connected to 192.168.0.105: time=118.02ms protocol=TCP port=5555
Connected to 192.168.0.105: time=140.02ms protocol=TCP port=5555
Connected to 192.168.0.105: time=57.01ms protocol=TCP port=5555
Connected to 192.168.0.105: time=77.51ms protocol=TCP port=5555
Connected to 192.168.0.105: time=97.01ms protocol=TCP port=5555
Connected to 192.168.0.105: time=122.02ms protocol=TCP port=5555
Connected to 192.168.0.105: time=135.52ms protocol=TCP port=5555
Connected to 192.168.0.105: time=52.01ms protocol=TCP port=5555
Connected to 192.168.0.105: time=72.51ms protocol=TCP port=5555
Connected to 192.168.0.105: time=92.51ms protocol=TCP port=5555
Connected to 192.168.0.105: time=105.51ms protocol=TCP port=5555
Connected to 192.168.0.105: time=5.50ms protocol=TCP port=5555

所以,看起来设备超时了一些传入数据,然后开始接受,后来随机超时。 ADB 是系统应用程序,因此它应该遵循任何准则并且相对没有错误。

有谁知道如何解决该问题并使 Android 设备能够快速应答传入连接?否则,任何需要传入流量的应用程序都将出现错误且不可靠。

最佳答案

如果设备未在使用中,Android 操作系统会在一段时间后关闭 Wifi radio 。但我不确定它认为“使用中”是什么。也许这与它有关......

来自https://developer.android.com/reference/android/net/wifi/WifiManager.WifiLock.html

Normally the Wi-Fi radio may turn off when the user has not used the device in a while.

在我的几个应用程序中,我使用 Wifi Lock 来保持 radio 处于唤醒状态。像这样的东西:注意:应用程序应请求android.permission.WAKE_LOCK

public class MyActivity extends Activity {
private WifiManager.WifiLock wifiLock;

@Override
protected void onResume {
super.onResume();
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "SYNCVR");
wifiLock.acquire();
}

@Override
protected void onPause() {
wifiLock.release();
}
}

创建 wifi 锁时您可以指定几个不同的选项:来自 https://developer.android.com/reference/android/net/wifi/WifiManager.html

WIFI_MODE_FULL

In this Wi-Fi lock mode, Wi-Fi will be kept active, and will behave normally, i.e., it will attempt to automatically establish a connection to a remembered access point that is within range, and will do periodic scans if there are remembered access points but none are in range.

-

WIFI_MODE_FULL_HIGH_PERF

In this Wi-Fi lock mode, Wi-Fi will be kept active as in mode WIFI_MODE_FULL but it operates at high performance with minimum packet loss and low packet latency even when the device screen is off.

-

WIFI_MODE_SCAN_ONLY

In this Wi-Fi lock mode, Wi-Fi will be kept active, but the only operation that will be supported is initiation of scans, and the subsequent reporting of scan results.

希望这有帮助:)

关于java - 安卓服务器: why it times out incoming connections randomly?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42434875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com