UDP 套接字读取最后传入的字节 [英] UDP Socket reading last incoming byte

查看:33
本文介绍了UDP 套接字读取最后传入的字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个套接字,它正在发送大量字节,经常重复相同的信息,但接收器无法一次获得所有这些字节.我有办法减慢发送方的速度,但现在我有延迟,因为没有足够的字节来.我需要实时传输.

I have a socket which is sending a massiv among of byte, is often repeating the same information, but the receiver couldn't get all this bytes in a time. I had a way to slow down the sender side but now I have latencies, due that not enough bytes are coming. I need a real time transfer.

有没有办法让接收者读取最后一个传入的字节并丢弃其他字节然后在程序中执行?

Is there a way that the receiver read the last incoming byte and discard the other ones then pursue in the program?

while (sock->isConnect() && tcpsock->isConnect()) {
    // with while instead of if, stuck in the loop
    if (sock->RecData(buff, 14)) { //receiving 14 bytes from UDP
        sendAngles2(buff);
        logger->setLatenz(logger->RECV);
    }
    logger->setLatenz(logger->MAIN);
    logger->LogData();
    // rest of the code 
}

推荐答案

不幸的是,没有标准/正常的方法来做到这一点 1 -- 当一个 UDP 套接字接收到一个新的数据包时一个完整的接收缓冲区,新的数据包会被丢弃,即使最好丢弃旧的(缓冲的)数据包.

Unfortuntely, there's no standard/normal way of doing this 1 -- when a new packet is received for a UDP socket that has a full receive buffer, the new packet is dropped, even when it would be preferrable to drop the older (buffered) packets.

您可以做的一件事是使用 setsockopt(fd, SOL_SOCKET, SO_RECVBUF,... 来减小套接字接收缓冲区的大小.这样做将导致它在以下情况下更快地开始丢弃数据包接收器落后,自相矛盾地减少处理数据包的延迟,类似于bufferbloat缓解技术.

One thing you can do is use setsockopt(fd, SOL_SOCKET, SO_RECVBUF,... to crank down the size of the socket receive buffer. Doing this will cause it to start dropping packets sooner when the receiver falls behind, paradoxically reducing the latency in processing packets, similar to bufferbloat mitigation techniques.

1我知道在 Linux 或 BSD 上没有办法做到这一点;一些实时操作系统可能有办法做到这一点

这篇关于UDP 套接字读取最后传入的字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆