【发布时间】:2017-02-24 09:20:08
【问题描述】:
我必须创建必须侦听传入连接的 Android 服务器应用程序。它是非根应用程序,它侦听随机高端口。代码很简单,在桌面 Java 上运行良好,它只监听端口并实现非常简单的自定义请求/响应协议。
然而我发现,无论出于何种原因,即使是 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 设备快速响应传入的连接?否则,任何需要传入流量的应用程序都会出现错误且不可靠。
【问题讨论】:
-
the connection time out often.??哪个连接?服务器在听不是吗?请清理干净。 -
是的,服务器正在监听,并且出于某种原因,一些传入连接随机超时(根本无法连接),即使 Java 在
accept方法调用中等待。 -
你不是在责怪一个有问题的路由器吗?试过其他设备吗?
-
多年来,我从未在同一本地网络中看到桌面设备有任何问题。
标签: java android sockets serversocket