力机器人到UDP数据包立即发送?人到、数据包、机器、UDP

2023-09-06 13:58:17 作者:胡闹是一种依赖

我尝试用通过WiFi连接我的银河王牌到我的笔记本电脑。这两种设备都连接到通过WiFi路由器,双方使用Java。

I am experimenting with connecting my Galaxy Ace to my laptop via WiFi. Both devices are connected to a router via WiFi, and both sides use Java.

在一个TCP连接,不时给了我很高的ping,我决定要基于UDP的连接,是能够控制的时候包实际发送的。

After a TCP connection gave me very high pings from time to time, I decided to base the connection on UDP, to be able to control when the packets are actually sent.

不过,这似乎是安卓还是缓冲UDP数据包,并不会立即发送它们。这一点,或者它关闭的WiFi完全,如果存在用于第二部分分数没有发送输出的数据。

However, it seems like Android still buffers UDP packets and does not send them immediately. This, or it shuts down WiFi completely, if there is no outgoing data transmitted for some fraction of a second.

首先,我ping通了电话不定期约每秒一次,重复发送ping请求,只要没有回答收到(包括丢包UDP):

First, I pinged the phone at irregular intervals of about once a second, repeatedly sending ping requests, as long as no answer was received (covering packet loss in UDP):

    computer -> phone -> computer
    Pinging 192.168.1.40: 148.05968ms
    Pinging 192.168.1.40: 524.41156ms
    Pinging 192.168.1.40: 705.8688ms
    Pinging 192.168.1.40: 3.705367ms
    Pinging 192.168.1.40: 3.872159ms
    Pinging 192.168.1.40: 549.4541ms
    Pinging 192.168.1.40: 479.29843ms
    Pinging 192.168.1.40: 3.89936ms
    Pinging 192.168.1.40: 428.85876ms
    Pinging 192.168.1.40: 739.28125ms

我的工作解决此问题通过电话发送数据包的计算机数据每100ms的只有1个字节,变着法子在常规以外的其他:

I worked around this problem by sending packets from phone to computer with just 1 byte of data every 100ms, changing nothing on the routine other than that:

    computer -> phone -> computer
    Pinging 192.168.1.40: 4.147753ms
    Pinging 192.168.1.40: 3.738213ms
    Pinging 192.168.1.40: 14.133768ms
    Pinging 192.168.1.40: 4.470561ms
    Pinging 192.168.1.40: 3.628386ms
    Pinging 192.168.1.40: 3.898334ms
    Pinging 192.168.1.40: 3.512401ms
    Pinging 192.168.1.40: 7.907006ms
    Pinging 192.168.1.40: 5.234216ms
    Pinging 192.168.1.40: 5.639137ms

这是至关重要的我的应用程序具有低延迟,所以我会不停地发送空包这样的(至少只要有发送没有真正的数据)。我想知道,我是否可以强制机器人以尽可能快地作出反应,而无需扔周围所有的网络无用的数据。那么,有没有更好的解决方案?

It is crucial for my application to have low latency, so I would keep sending empty packets like this (at least as long as there is no real data transmitted). I am wondering, if I can force android to respond as fast as possible, without needing to throw useless data all around the network. So, is there a more elegant solution?

顺便说一句,我假设这个问题是智能手机,而不是电脑,但也有可能是电脑等待进入的数据包,然后发送它的数据包。从我所了解的网络,这是非常不可能的,但。

By the way, I am assuming the problem is the smartphone, not the computer, though it could also be the computer waiting for incoming packets and then sending its packets. From what I know about networking, this is very unlikely, though.

感谢您的帮助!

推荐答案

据的Java而言,一旦DatagramSockect.send(...)被调用,数据报发送。没有缓冲在Java应用程序空间,并且没有办法控制在OS任何缓冲

As far as Java is concerned, once DatagramSockect.send(...) has been called, the datagram is "sent". There is no buffering in Java application space, and no way to control any buffering in the OS.

这是不可能的TCP或UDP的退缩的数据包。这是更为可能的是,根本问题是什么在WiFi的水平......或者可能具有路由...和操作系统延迟发送数据包,直到问题解决本身。

It is unlikely that either TCP or UDP is "holding back" packets. It is far more likely that the root problem is something at the WiFi level ... or possibly with routing ... and the OS is delaying sending the packets until the problem resolves itself.

谢谢,我现在已经实现发送这些保持活动数据包仅如果有在这段时间内发送没有其他数据包。

Thanks, I now implemented sending those keep-alive packets only if there was no other packet sent during that time.

这听起来像你保持活跃坪是有预期的效果......从我推断,问题的根源是/是WiFi的链接正在由于不活动下降。 (这听起来像一个不消耗电池功能...)这表明,一种替代解决将是看看你是否可以调整的WiFi超时。

It sounds like your keep-alive pings are having the desired effect ... and from that I infer that the root problem is / was that the WiFi "link" was being dropped due to inactivity. (It sounds like a "don't drain the battery" feature ...) This suggests that an alternative fix would be to see if you can tweak the WiFi timeout.

除此之外,会是更快的数据包发送到另一个端口,其上没有人听?它应该还是有不一样的效果,对吧?

Besides that, would it be faster to send packets to another port, on which nobody is listening? It should still have the same effect without, right?

这是不可能作出任何区别。没有任何证据表明,这个问题是造成拥堵。证据指向您的手机关闭不活动的无线网络,以节省电源...

It is unlikely to make any difference. There is no evidence that the problem is caused by congestion. The evidence points to your phone turning off inactive WiFi to save power ...