![]() |
Network Dual-Stack Component
Version 7.0 (Beta)
MDK-Professional Middleware for IPv4 and IPv6 Networking
|
UDP Socket functions. More...
UDP Socket functions.
UDP is a byte stream service. It does not know anything about the format of the data being sent. It simply takes the data, encapsulates it into a UDP packet, and sends it to the remote peer.
The UDP protocol does not wait for any acknowledgment and is unable to detect any lost packets. If this is required, it must be done by the application layer. However, it is better to use a TCP Socket for reliable communication.
To send data, the function netUDP_Open opens a socket for communication. You need to specify the socket number and the local port to be used. The next free socket can be allocated using the function netUDP_GetSocket. This function requires a callback function to be present that is called by the Network Core whenever an UDP event occurs. The UDP callback function is inside the module UDP_Socket.c. To add the module to your project, simply right-click on the Source group, select Add New Item to Group, then click on User Code Template and scroll in the template files list until you find the UDP Socket template. Customize this module to the application's needs.
If netUDP_Open is called with a local port number of 0, the Network Core determines which port to use. Later, this information can be retrieved using the function netUDP_GetLocalPort. To set certain UDP options (for IPv4 or IPv6) for the opened socket, use netUDP_SetOption.
The template file TCP_Socket_Client.c also contains an exemplary send function, called send_data. This shows how to use netUDP_GetBuffer (for memory allocation) and netUDP_Send for data transmission. Adapt this function to your application's needs.
UDP communication will be stopped using netUDP_Close. The UDP socket will be closed. Subsequently, call netUDP_ReleaseSocket as well, as this function finally frees the memory used by the UDP socket.
Code Example UDP_Socket.c
To send data to multiple hosts, you need to join a specific group of hosts with netIGMP_Join function. Then you can use the same functions as described in udp_send_data. When you want to stop sending and receiving the data from that group address, you have to leave a specific group with netIGMP_Leave function.