Table Of Contents

Transferring Data over a Network Using UDP

Version:
Last Modified: September 26, 2017

Use the User Datagram Protocol (UDP) to transfer data to a single client, known as unicast communication, or between a number of processes or processors that have opted into a group or receivers, known as multicast communication, 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 system you want to send data to and the system you want to receive data from.

spd-note-note
Note  

UDP transfers data faster, with higher throughput and lower latency, than TCP, because UDP does not require the overhead that TCP does. UDP does not guarantee successful and proper delivery of the data you send, and if you send data in multiple packets, they may not arrive at the destination in the order you sent them. For more reliable network communication, you can use TCP.

What to Use

What to Do

  1. Create the following diagram in a VI targeted to the system that you want to receive data.

    Customize the gray sections for your unique programming goals.

    The UDP Open node opens a local UDP socket to send a datagram to a single receiver 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.

    To send a single datagram to multiple receivers in parallel, known as multicast communication, useUDP Multicast Open instead of the standard UDP Open node, as shown in the following image.

    UDP multicast communication lowers network traffic compared to unicast communication, which must iterate through the list of clients. To receive data sent by a multicast sender, the sender broadcasts a multicast IP address. All receivers must join a multicast group by specifying the same multicast IP address. Multicast addresses are in the 224.0.0.0 to 239.255.255.255 range.

    UDP Read nodes read from the socket. 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 system that you want to send data.

    Customize the gray sections for your unique programming goals.

    The UDP Open node opens a local UDP socket to send a datagram to a single receiver on the port or service name you specify.

    To send a single datagram to multiple receivers in parallel, known as multicast communication, useUDP Multicast Open instead of the standard UDP Open node, as shown in the following image.

    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 multicast 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 a reasonable size, 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. When the data packet is greater than the Maximum Transmit Unit (MTU) of the network hardware being used, data fragmentation happens. For standard cabled Ethernet, which has an MTU of 1,500 bytes, about 1,000 bytes is a good rule of thumb to prevent any fragmentation from happening.
    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 communicate using UDP when you simultaneously run the applications.

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. Different IP addresses that refer to the 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 is lost without any warning or notification.
    • Verify that the ports you have specified are valid, open ports. Valid UDP ports range from 0 to 65,535. 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.

Example

Search within the programming environment to access the following installed example: Simple UDP


Recently Viewed Topics