47093

Is UDP a reliable protocol for IPC?

Question:

If I use UDP purely for inter-process communication (i.e., in 1 system, <strong>with no network involved</strong>), can I consider it to be reliable? Or do I still need to worry about packets getting dropped, etc.?

<strong>Note</strong> that this is a <em>practical</em> question, not a theoretical one. If the answer differs across OSes, please explain how, in particular including Windows, Linux, and Mac.

<hr /><h3>Edit: Nope, it's not reliable -- example below.</h3>

Thanks to the current answer for pointing me in the right direction.<br /> This code drops a packet on Windows 8.1 (I get Received: 18432 (DROPPED PACKET)).<br /> (I'm not sure why it doesn't run on Linux, but it should be close to working.)

#include <stdio.h> #ifdef _WIN32 #include <ws2tcpip.h> #else #include <unistd.h> #include <sys/socket.h> #include <netinet/in.h> #include <netinet/ip.h> #endif int main() { #ifdef _WIN32 typedef int socklen_t; #else typedef int SOCKET; #endif SOCKET r = socket(AF_INET, SOCK_DGRAM, 0); struct sockaddr_in addr = { AF_INET }; addr.sin_addr.s_addr = htonl(0x7F000001); { socklen_t addrlen = sizeof(addr); if (bind(r, (struct sockaddr *)(&addr), addrlen) == -1 || getsockname(r, (struct sockaddr *)(&addr), &addrlen) == -1) { return 1; } } SOCKET s = socket(AF_INET, SOCK_DGRAM, 0); int tids = 0; for (long c = 0, i = 0, j = 0; c < 1000000; ++c) { if ((c + 1) % 10) { int n = sendto(s, (char const *)(&i), sizeof(i), 0, (struct sockaddr const *)(&addr), sizeof(addr)); if (n != sizeof(i)) { return 1; } // else { fprintf(stderr, "Sent: %lu\n", i); } ++i; } else { struct sockaddr temp; socklen_t templen = sizeof(temp); long v; int n = recvfrom(r, (char *)(&v), sizeof(v), 0, (struct sockaddr *)(&temp), &templen); if (n != sizeof(v)) { return 2; } else if (v != j) { fprintf(stderr, "Received: %lu (DROPPED PACKET)\n", v); return 3; } // else { fprintf(stderr, "Received: %lu\n", v); } ++j; } } }

Answer1:

<blockquote>

If I use UDP purely for inter-process communication (i.e., in 1 system, with no network involved), can I consider it to be reliable?

</blockquote>

No. UDP packets can (and sometimes will) still be dropped even when all communication is being done over the same host.

You can demonstrate this for yourself, if you want; set up two UDP-socket-using programs on the same host, with program A sending UDP packets to program B that receives them and records them. (Include sequence numbers in the UDP packets so that program B can easily tell when a packet was not received).

Once that is working and packets are being transmitted at a decent rate, put some code into program B so that every so often it calls sleep(5) (or similar, so that program B fails to call recv() on its UDP socket for a significant amount of time). You'll likely see that after the sleep() call returns, program B reports that some packets were skipped -- because while B was asleep, the incoming-packets buffer for its UDP socket became full and then some packets were dropped by the networking stack because there was no place to put them.

Recommend

  • Standard #ifdef for Cygwin
  • Winsock accept() returning WSAENOTSOCK (code 10038)
  • Pass str as an int array to a Python C extended function (extended using SWIG)
  • Compiling mono as static library
  • Error while running npm “prefix -g is not recognized as an internal or external command”
  • Calculate cash for hours, cash till 8 hours is 3$, after 8 it goes +5. Without “if”
  • How to printk the first 34 bytes (Ethernet and IP headers) of each packet (EtherType: 0x0806, Protoc
  • How to encode h.264 live stream to RTP packet with Java
  • RTCP receiver report sending interval
  • ICU support in a 32-bit build of Qt5 with the VS2012 compiler causes Qt5 build failure; Webkit is al
  • Visual C++: Exporting decorated function name in a def file
  • Does php scandir() exclude hidden files under Windows?
  • HTML select, correct option selected in DOM, but wrong item shown in firefox
  • Sobel filter in x direction in CUDA
  • QML cannot assign assign shared_ptr to [unknown property type]
  • Accessing structure members with a pointer
  • fscanf not reading floats correctly
  • Call C++ class member function from C (Visual Studio)
  • Reaping zombie process - child
  • Detecting null parameter in preprocessor macro
  • Optimizing database types to compact database (SQLite)
  • TFS: Get latest causes slow project reloading
  • Running a C# exe file
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Matrix multiplication with MKL
  • Buffer size for converting unsigned long to string
  • Hits per day in Google Big Query
  • File not found error Google Drive API
  • How to get Windows thread pool to call class member function?
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Reading document lines to the user (python)
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Converting MP3 duration time
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal