UDP Communication question(s)

grease_lighting

Well-known member
Folks, please bear with me. I don't have the <vocabulary> to discuss this, nor do I want to wade thru verbage detailing datagrams & link layers.

I'm having a difficult time understanding UDP communications. I have a project that uses a Teensy 3.0 (yes) and a REV 1 arduino ethernet interface that uses the W5100. My mental model is similar to Serial Port communications where the Tx and Rx lines are connected together and used as required. Which may be where some of my problem is. As far as control of the w5100 goes, I believe the SS line is like a chip select that enables the w5100 to listen & talk over the SPI interface and disables the w5100 SPI interface when it is inactive (high).
When I send data out over UDP, it appears pretty straight forward.
Code:
    Serial.println("udp.begin");
    Serial.println(tmp); 
    if ( strlen(tmp) > 0 ) {                                        //  do not send empty messages
      p1 = Udp.beginPacket(dcs100IP, dcsPort);                      //  send only 1 - time
      Udp.write(tmp);                                               //  This gets sent
      p2 = Udp.endPacket();                                         //  send & get status
      Serial.print("Send Packet Status: ");                         //  packet send status    1 == OK   0 == error (any)
      if (p2 == 0)  {Serial.println("Fail.");}
      if (p2 == 1)  {Serial.println("Success.");}
    } else {Serial.println("- BYPASSED - ");}

But, what is going on on the receive side? Is the receiver, like my Serial Port model, always connected to the network and always receiving & buffering the packets so it catches them all and only picks out the packets destined for the UDP port ? How do I get UDP to wait for data like tucking Serial.Read() into a while-loop?
 
In OS's the convention is to have a listen thread for UDP that passes packets to a handler. In TCP its connections rather than packets that are passed to a handler.

Beware the traps of UDP, no guarantee of delivery, no guarantee of delivery in the same order, no guarantee that there won't be packet duplications. In a local network it may be reliable of course, but over the net in general UDP is subject to all these vaguaries.

The "advantage" of UDP is no state is kept.

The advantage of TCP is that the packet switching network is hidden from you and you see a point-to-point connection, but this requires complexity and state at both ends.
 
Back
Top