Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: multicast UDP issues

  1. #1

    multicast UDP issues

    running T4.1 with IDE Arduino-1.8.16 Teensyduino-1.55 and NativeEthernet.

    I'm trying to read from a UDP multicast address (grabbing a text SAP message sent by ffmpeg encoding/streaming tool)
    The file is being sent, as I can read it using a python script( shown below) However my Teensy code receives the data but
    when I print to the serial monitor it is blank.

    As a simple test I can use netcat to send a text file on disk and my Teensy code can display correctly.
    (Only thing is the UDP IP and port reported seem wrong - I am using port 9875)

    nc -u 9875 < a.txt
    Teensy result:
    my ip
    Received packet of size 55
    From, port 63758
    the quick brown fox!
    Jumps over the lazy dog

    My code running on the Teensy is this:

    #include <NativeEthernet.h>
    #include <NativeEthernetUdp.h>
    byte mac[] = { 0x0A, 0x1B, 0x3C, 0x4D, 0x5E, 0x6E};
    byte ip[] = { 192, 168, 2, 155};
    EthernetUDP Udp;
    byte mcastip[] = { 224, 2, 127, 254};
    int mport = 9875;
    void setup()
      while (!Serial);
      Ethernet.begin(mac, ip);
      Udp.beginMulticast(mcastip, mport);
      IPAddress ipaddr = Ethernet.localIP();
      Serial.print("my ip "); Serial.println(ipaddr);
    void loop()
      int packetSize = Udp.parsePacket();
      if (packetSize) {
        char packetBuffer[packetSize];
        Serial.print("Received packet of size ");
        Serial.print("From ");
        IPAddress remote = Udp.remoteIP();
        for (int i = 0; i < 4; i++) {
          Serial.print(remote[i], DEC);
          if (i < 3) {
        Serial.print(", port ");
   , packetSize);
    However with the ffmpeg command, which sends the SDP text every few seconds I get no printed text, although the
    result shows the packet has a size which is correct. (Again IP and port seem wrong)

    Received packet of size 309
    From, port 62806

    This python script does show the SDP text data ( and anything else I send perfectly):

    import socket
    import struct
    multicast_addr = ''
    bind_addr = ''
    port = 9875
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    membership = socket.inet_aton(multicast_addr) + socket.inet_aton(bind_addr)
    sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, membership)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.bind((bind_addr, port))
    while True:
        message, address = sock.recvfrom(255)
    So any ideas why this isn't working on the Teensy? Maybe weird control characters?

    My text file shows in the python script output like this:
    b'the quick brown fox!\nJumps over the lazy dog\n12345678\n\n'

    it shows the SDP text like this which is what I expect:
    b'$\x00\xc6\xb2\xc0\xa8\x026application/sdp\x00v=0\r\no=- 0 0 IN IP4\r\ns=No Name\r\nc=IN IP4\r\nt=0 0\r\na=tool:libavformat 58.76.100\r\nm=audio 5004 RTP/AVP 96\r\nb=AS:128\r\na=rtpmap:96 MPEG4-GENERIC/44100/2\r\na=fmtp:96 profile-level-id=1;mode=AAC-hbr;sizelength=13'


  2. #2
    Senior Member
    Join Date
    Mar 2017
    Oakland, CA, USA
    Since youre using NUL-terminated string-based printing to print to the console, it will stop printing at the first NUL character. Id expect it to print $, though, since a NUL is after that.

  3. #3
    Quote Originally Posted by shawn View Post
    Since youre using NUL-terminated string-based printing to print to the console, it will stop printing at the first NUL character. Id expect it to print $, though, since a NUL is after that.
    Ahh, ok that makes sense. I guess I need a way to 'decode' the raw hex and get rid of the odd characters.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts