简体   繁体   English

在同一台机器上通过 UDP 套接字发送数据可靠吗?

[英]Is sending data via UDP sockets on the same machine reliable?

If i use UDP sockets for interprocess communication, can i expect that all send data is received by the other process in the same order?如果我使用 UDP 套接字进行进程间通信,我是否可以期望其他进程以相同的顺序接收所有发送数据?

I know this is not true for UDP in general.我知道这通常不适用于 UDP。

No. I have been bitten by this before.不,我以前被这个咬过。 You may wonder how it can possibly fail, but you'll run into issues of buffers of pending packets filling up, and consequently packets will be dropped.您可能想知道它怎么可能会失败,但是您会遇到待处理数据包缓冲区填满的问题,因此数据包将被丢弃。 How the network subsystem drops packets is implementation-dependent and not specified anywhere.网络子系统如何丢弃数据包取决于实现,并没有在任何地方指定。

In short, no.简而言之,没有。 You shouldn't be making any assumptions about the order of data received on a UDP socket, even over localhost.您不应该对 UDP 套接字上接收到的数据顺序做出任何假设,即使是通过本地主机。 It might work, it might not, and it's not guaranteed to.它可能有效,也可能无效,也不能保证。

No, there is no such guarantee, even with local sockets.不,即使使用本地套接字,也没有这样的保证。 If you want an IPC mechanism that guraantees in-order delivery you might look into using full-duplex pipes with popen() .如果您想要一种保证按顺序交付的 IPC 机制,您可能会考虑使用带有popen()的全双工管道。 This opens a pipe to the child process that either can read or write arbitrarily.这将打开一个通向可以任意读取或写入的子进程的管道。 It will guarantee in-order delivery and can be used with synchronous or asynchronous I/O ( select() or poll() ), depending on how you want to build the application.它将保证按顺序交付,并可与同步或异步 I/O( select()poll() )一起使用,具体取决于您希望如何构建应用程序。

On unix there are other options such as unix domain sockets or System V message queues (some of which may be faster) but reading/writing from a pipe is dead simple and works.在 unix 上还有其他选项,例如 unix 域套接字或 System V 消息队列(其中一些可能更快),但从管道读取/写入非常简单且有效。 As a bonus it's easy to test your server process because it is just reading and writing from Stdio.作为奖励,测试您的服务器进程很容易,因为它只是从 Stdio 读取和写入。

On windows you could look into Named Pipes, which work somewhat differently from their unix namesake but are used for precisely this sort of interprocess communication.在 Windows 上,您可以查看命名管道,它的工作方式与它们的 Unix 同名管道略有不同,但恰好用于此类进程间通信。

Loopback UDP is incredibly unreliable on many platforms, you can easily see 50%+ data loss.环回 UDP 在许多平台上非常不可靠,您可以轻松看到 50% 以上的数据丢失。 Various excuses have been given to the effect that there are far better transport mechanisms to use.已经有各种借口可以使用更好的传输机制。

There are many middleware stacks available these days to make IPC easier to use and cross platform.现在有许多可用的中间件堆栈使 IPC 更易于使用和跨平台。 Have a look at something like ZeroMQ or 29 West's LBM which use the same API for intra-process, inter-process (IPC), and network communications.看看像ZeroMQ29 West 的 LBM 之类的东西,它们使用相同的 API 进行进程内、进程间 (IPC) 和网络通信。

The socket interface will probably not flow control the originator of the data, so you will probably see reliable transmission if you have higher level flow control but there is always the possibility that a memory crunch could still cause a dropped datagram.套接字接口可能不会对数据的发起者进行流量控制,因此如果您有更高级别的流量控制,您可能会看到可靠的传输,但内存紧缩仍然有可能导致数据报丢失。

Without flow control limiting kernel memory allocation for datagrams I imagine it will be just as unreliable as network UDP.如果没有流量控制限制数据报的内核内存分配,我想它会像网络 UDP 一样不可靠。

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

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