Setting up UDP Communication with the NI-XNET IP Stack
- Updated2025-04-14
- 3 minute(s) read
You can use the NI-XNET IP stack to establish message-oriented communication with UDP.
This procedure requires you to create a JSON configuration string to define your IP stack.
UDP is a message-oriented protocol—it involves an interface that functions as a sender and another interface that functions as a receiver. The sender and receiver do not establish a connection. The sender sends and the receiver receives only when it is configured to, which may or may not be at a time when the sender is prepared to send to a receiver or when the receiver is prepared to receive from a sender.
Once you have generated the JSON configuration, setting up UDP communication involves the following high-level operations:
- Instantiating the IP stack.
- Creating a socket and, depending on whether you are programming a sender or receiver, iteratively writing or reading data on the connection.
- Closing the socket and the IP stack.
Use senders together with receivers to establish communication.
Complete the following steps to create UDP senders and receivers with the NI-XNET IP stack:
-
Call IP Stack Create and pass in the JSON configuration
string you created.
Tip You can name the stack to help identify it. The IP Stack Info Tool and IP Stack Get Info can return this name.
- Call IP Stack Wait to ensure the interface is ready for communication.
- Call UDP Socket Open and specify a local port for the socket.
-
Depending on the role you are programming, create a loop to write data (sender)
or read data (receiver) over time:
Role Description Sender Call a UDP Socket Write function: - Provide either the text or the binary to write.
- Provide the remote
address to send data to.
This address should match the address for the receiver that you set up in the JSON configuration.
- Provide the remote port
to send data to.
This value should match the local port you specified for the receiver in the receiver code.
Receiver - Call the corresponding UDP Socket
Read function to the data type the sender is
writing.
You can optionally set a maximum size for messages and a timeout value.
- To prevent timeout errors from stopping the receiver, clear error -13860.
You can add a wait time for iterations of the loop. You can also add a manual stop to give the end user the ability to halt the writing or reading of data.
- To close the connection, call UDP Socket Close.
- To clear (close) the IP stack, call IP Stack Clear.
-
Incorporate the sender and receiver code in your application to establish UDP
communication.
Note The sender and receiver must be configured with opposing states. Use the PHY State setting in NI configuration software to control the state interactively, or use the PHY State Configured property to control the state programmatically.
LabVIEW Programming Examples for UDP Communication
For example code for establishing UDP communication, refer to NI-XNET IP Stack Simple UDP.lvproj.
Related Information
- Generating NI-XNET IP Stack JSON Configuration
Programming TCP or UDP communication with the NI-XNET IP stack first requires you to generate a JSON configuration string. This JSON configuration defines the IP stack that you create with the NI-XNET IP stack API and is how you implement, for example, the type of addressing you use in your application (static or DHCP).
- Setting up TCP Communication with the NI-XNET IP Stack
You can use the NI-XNET IP stack to establish connection-oriented communication with TCP.
- Automotive Ethernet Socket API
The automotive Ethernet socket API enables you to create BSD-like network sockets for TCP communication and UDP communication. This implementation is independent of the limitations of the IP stack native to your operating system.
- NI-XNET IP Stack Supported Protocols
Understand the versions of the Internet protocol and which communication protocols you can use with the NI-XNET IP stack.
- Choosing Between TCP and UDP for the NI-XNET IP Stack
The NI-XNET IP stack supports communication with TCP (Transmission Control Protocol) and UDP (Universal Datagram Protocol). Each protocol is best suited for different situations, though TCP is suitable for most applications.
- Interactively Viewing PXIe-8520 IP Stacks
The NI-XNET IP Stack Info tool is a simple executable that returns configuration information about active IP stacks on local or remote target systems.