简体   繁体   English

Linux:UDP接收消息的速度比发送消息的速度慢

[英]Linux: UDP receive message slower than that is send out

I am using a arm-cpu based board, running a uclinux with kernel Linux version 2.6.12.3-a9-17 to receive data from a PC by eithernet, using UDP protocol. 我使用的是基于arm-cpu的板,运行uclinux,内核Linux版本为2.6.12.3-a9-17,以使用UDP协议通过任一网络从PC接收数据。

My problem is, on the board, although I can receive all the messages (no lost), the time interval between incoming messages is limited to 20ms at most. 我的问题是,在板上,尽管我可以接收所有消息(没有丢失),但是传入消息之间的时间间隔最多限制为20ms。 That is, if I send out messages every 30ms, everything is OK. 也就是说,如果我每30毫秒发送一次消息,则一切正常。 However, if I send out message every 10ms, the board shows incoming UDP package every 20ms ( So it will take twice the time that PC use to receive all the messages). 但是,如果我每隔10ms发送一次消息,开发板就会每隔20ms显示一次传入的UDP包(因此PC接收所有消息所用的时间将是原来的两倍)。

I am using these code to measure the time interval between two incoming message: 我正在使用以下代码来测量两条传入消息之间的时间间隔:

struct timeval tnew, told, dt;
gettimeofday(&tnew,0);
told = tnew;

while (1) {
    memset(buf, 100, 0);
    int recvlen = recvfrom(fd, buf, 100 , 0,
                           (struct sockaddr *)&addr_recv, &addr_len);
    if (recvlen>0) {
        gettimeofday(&tnew ,0);
        timersub(&tnew,&told,&dt);
        told = tnew;
        printf("UDP intput: %fms, string:%s\n", (float)dt.tv_usec / 1000.0,buf);
    }

Here is the result: 结果如下:

on PC: 在PC上:

UDP output: 10.019000ms, string: 353:ABCDEFG
UDP output: 10.067000ms, string: 354:ABCDEFG
UDP output: 10.068000ms, string: 355:ABCDEFG
UDP output: 10.068000ms, string: 356:ABCDEFG
UDP output: 10.004000ms, string: 357:ABCDEFG
UDP output: 10.120000ms, string: 358:ABCDEFG

on board: 在船上:

UDP intput: 20.000000ms, string: 353:ABCDEFG, 
UDP intput: 20.002000ms, string: 354:ABCDEFG, 
UDP intput: 19.998000ms, string: 355:ABCDEFG, 
UDP intput: 20.051000ms, string: 356:ABCDEFG, 
UDP intput: 19.953000ms, string: 357:ABCDEFG, 
UDP intput: 19.996000ms, string: 358:ABCDEFG, 

I think this interval should be defined by something in the linux system. 我认为此间隔应该由linux系统中的某些东西定义。 How can I fix it? 我该如何解决?

Btw, iptables of the board shows (EXT = eth0): 顺便说一句,董事会的iptables显示(EXT = eth0):

 iptables -A INPUT -i $EXT -p udp -d 0/0 --dport 1024:65535 -j ACCEPT

Thank you very much. 非常感谢你。

thank you for your kind replies. 多谢您的回覆。 I found out the problem is due to a usleep(10) and the end of while. 我发现问题是由于usleep(10)和一会儿结束。 On the board, usleep(10) takes exactly 20ms, while on another PC it takes 0.1ms. 在板上,usleep(10)只需20毫秒,而在另一台PC上则需要0.1毫秒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM