可以为posix recv设置超时会导致丢失udp数据包吗? [英] Can setting a timeout for posix recv cause lost udp packets?

查看:117
本文介绍了可以为posix recv设置超时会导致丢失udp数据包吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了此答案有关如何设置posix套接字超时的信息.答案的linux部分:

I found this answer on how to set a timeout for posix socket. The linux part of that answer:

// LINUX
struct timeval tv;
tv.tv_sec = timeout_in_seconds;
tv.tv_usec = 0;
setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);

以及posix文档中的引言:

and the quote from the posix documentation:

SO_RCVTIMEO

设置超时值,该值指定输入功能完成之前等待的最长时间.它接受一个时间间隔 具有秒数和微秒数的结构,用于指定 限制等待输入操作完成的时间.如果一个 接收操作已阻塞了这么长时间而没有接收 附加数据,则应返回部分计数或将errno设置为 如果未接收到数据,则为[EAGAIN]或[EWOULDBLOCK].此的默认值 option为零,表示接收操作不应 暂停.此选项采用timeval结构.请注意,并非全部 实施允许设置此选项.

Sets the timeout value that specifies the maximum amount of time an input function waits until it completes. It accepts a timeval structure with the number of seconds and microseconds specifying the limit on how long to wait for an input operation to complete. If a receive operation has blocked for this much time without receiving additional data, it shall return with a partial count or errno set to [EAGAIN] or [EWOULDBLOCK] if no data is received. The default for this option is zero, which indicates that a receive operation shall not time out. This option takes a timeval structure. Note that not all implementations allow this option to be set.

我不明白的是:这会导致udp软件包丢失吗? 如果在收到udp软件包时达到超时怎么办?

What I dont understand is: Can this cause loosing udp packages? What if the timeout is reached while a udp package is received?

与此相关:设置UDP的recv fcn的超时时间套接字

PS:我知道UDP本质上是不可靠的,所以我的问题实际上主要是关于在处理udp消息时发生超时的情况.

PS: I am aware that UDP is inherently unreliable, so my question is really mainly about the case where the timeout occurs while an udp message is processed.

推荐答案

否;它不会使您更有可能丢弃数据包.

No; it doesn't make you more likely to drop packets.

了解较低级别的网络传输是如何发生的;您有一个网卡.当此卡接收数据时,无论您的程序在做什么,它都会将数据存储到其自己的存储区中.当您致电recv时;您要求操作系统将数据从网卡内存移至程序内存.这意味着,如果您的线程正在执行其他操作时有数据包进入;它不仅被删除,而且在您的线程下次获取数据时进行处理.

Looking at how network transport happens at a lower level; you have a network card. As this card receives data, irrespective of what your program is doing, it stores the data into it's own memory area. When you call recv; you're asking the OS to move data from the network cards memory to your programs memory. This means that if a packet comes in while your thread is doing something else; it isn't just dropped, but processed the next time your thread comes to get data.

如果您的线程没有足够频繁地调用recv;则网卡的内存将已满.发生这种情况时,将无法存储新的数据包;如果使用的是TCP,则路由器将被告知无法处理.如果是UDP,则将其丢弃.正是这一部分使UDP本质上不可靠,因为它可能在数据包传输期间的任何时候发生.

If your thread doesn't call recv often enough; then the memory for the network card will become full. When this happens no new packets can be stored; and if it's using TCP then the router will be told that it's not able to process it; if it's UDP then it will simply be dropped. It is this part that makes UDP inherently unreliable as it can happen at any point during the transport of the packet.

超时会影响线程等待数据在网卡存储区中显示的时间;除非您再也不会致电recv;不会影响丢包.

The timeout impacts how long the thread will wait for data to appear in the networkcard memory area; and unless you never call recv again; does not impact dropped packets or not.

这篇关于可以为posix recv设置超时会导致丢失udp数据包吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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