views:

210

answers:

5

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?

I know this is not true for UDP in general.

+5  A: 

In short, no. You shouldn't be making any assumptions about the order of data received on a UDP socket, even over localhost. It might work, it might not, and it's not guaranteed to.

Pi
A: 

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.

DigitalRoss
+14  A: 

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.

Brian Agnew
Setting a large recv buffer with setsockopt helps but probably doesn't guarantee anything. Certainly my tests show that it has a direct effect on packet loss on windows 7. But with no flow control you're just delaying the inevitable.
Len Holgate
+2  A: 

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(). 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.

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. As a bonus it's easy to test your server process because it is just reading and writing from 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.

ConcernedOfTunbridgeWells
A: 

Loopback UDP is incredibly unreliable on many platforms, you can easily see 50%+ data loss. 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. 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.

Steve-o