Table Of Contents

Transferring Data over a Network Using UDP

Version:
    Last Modified: March 10, 2017

    Use User Datagram Protocol (UDP) to transfer data between processes or processors without establishing a connection between the sender and the receiver.

    To transmit data using UDP, you must develop the code to send or receive data on both the processor you want to send data to and the processor you want to receive data from.

    spd-note-note
    Note  

    UDP does not guarantee successful transmission of data. Use UDP to send short, non-critical messages to one or more recipients. For more reliable data transfer, use TCP.

    What to Use

    What to Do

    1. Create the following diagram in a VI targeted to the processor where you want to receive data.

      Customize the gray sections for your unique programming goals.

      The UDP Open node opens a local UDP socket on the port or service name you specify. Make sure the port or service name you wire to UDP Open on the receiver matches the port or service name you write to from the sender.

      If you want to send the same data to multiple recipients at once, you can broadcast the data using a UDP Multicast Open node instead of the UDP Open node, as shown in the following image.

      To receive data broadcast by a multicast sender, all receivers must join a multicast group by specifying the same multicast IP address that the sender broadcasts to. Multicast addresses are in the 224.0.0.0 to 239.255.255.255 range.

      UDP Read nodes read from the specified local port and retrieve data from the sender. If the sender sends multiple datagrams to the receiver before the read operation occurs, UDP Read reads only the latest datagram.
      You can display the data you receive from the sender on the UI of the receiver or further process the data locally.
      After you close the UDP socket, you can no longer send or receive data on the specified port until you reopen the socket. Make sure you no longer want to send or receive data before you close the socket.
    2. Create the following diagram in a VI targeted to the processor from which you want to send data.

      Customize the gray sections for your unique programming goals.

      The UDP Open node opens a local UDP socket on the port or service name you specify.

      If you want to send the same data to multiple recipients at once, open a local multicast socket using a UDP Multicast Open node instead of the UDP Open node.

      UDP Write nodes write to the specified receiver port and send data to the receiver. The address you specify must match the IP address of the receiver, and the port or service name you specify must match the port you specify on the receiver.

      To broadcast data to multiple recipients, specify the same multicast IP address that the receivers read from, as shown in the following image.

      Multicast addresses are in the 224.0.0.0 to 239.255.255.255 range.

      The maximum size limit for each datagram is 65,535 bytes. However, try to keep datagrams to about 1,000 bytes or fewer to minimize the chance that the protocol will need to split and reassemble the underlying data packet during transmission.
      After you close the UDP socket, you can no longer send or receive data on the specified port until you reopen the socket. Make sure you no longer want to send or receive data before you close the socket.

    After you finish developing your sender and receiver applications, your sender and receiver applications will communicate using UDP when you run the applications simultaneously.

    Troubleshooting

    • If a recipient consistently receives datagrams in the wrong order, consider sending shorter messages. The maximum size limit for each datagram is 65,535 bytes. However, try to keep datagrams to about 1,000 bytes or fewer to minimize the chance that the protocol will need to split and reassemble the underlying data packet during transmission.
    • If you have trouble transferring data successfully, try the following troubleshooting strategies:
      • Verify that the port, service name, or address you specify for UDP Write on the sender matches the port or service name you specify for UDP Open or UDP Multicast Open on the receiver. Note that different IP addresses that refer to same computer are not compatible. For example, if you open a UDP socket using the address localhost and write to the numeric IP address of the local computer, such as 10.113.0.53, the receiver does not receive data.
      • If you are broadcasting data, verify that the IP address you specify for UDP Write on the sender matches the IP address you specify for UDP Multicast Open on the receiver.
      • Verify that the recipient VI opens the UDP socket before the datagram arrives from the sender. If the datagram arrives before the recipient opens the appropriate UDP socket, the datagram will be lost without any warning or notification.
      • Verify that the ports you have specified are valid, open ports. Valid UDP ports range from 0 to 65535. To dynamically locate an open port, use port 0 and provide a service name for any UDP Open and UDP Write nodes as illustrated by the images above. Because specifying port 0 causes the nodes to locate a random open port, use port 0 only if you intend to use service names.

    Examples

    Search LabVIEW for the following installed examples: Simple UDP


    Recently Viewed Topics