NetBurner 3.1
UDP Sockets

The NetBurner API provides two mechanisms to implement UDP: sockets and a C++ class. The choice of which to use is Dependant of which method you find most comfortable using. There is not a performance difference.

Receiving UDP Packets using UDP Sockets

To receive UDP Packets:

  • Use the CreateRxUdpSocket() function to open a listening socket. It will return a file descriptor.
  • Use the recvfrom() function to receive packets. The industry standard behavior of this function is to block forever until a packet is received. If you want to allow your applicatiion to have better control, you can wrap the recvfrom() function inside a select() function using the UDP file descriptor and a timeout.

Sending UDP Packets using UDP Sockets

To send UDP Packets:

UDP Send/Receive Example Using Sockets

#include <init.h>
#include <stdlib.h>
#include <string.h>
#include <system.h>
#include <udp.h>
#include <utils.h>
const char *AppName = "UDP Sockets Example";
// Allocate stack space for the listen task
uint32_t UdpReceiveTaskStack[USER_TASK_STK_SIZE];
void UdpReceiveTask(void *pd)
{
int listenPort = (int)pd;
iprintf("UdpReceiveTask monitoring port %d\r\n", listenPort);
// Create a UDP socket for receiving
int udpFd = CreateRxUdpSocket(listenPort);
if (udpFd <= 0)
{
iprintf("Error Creating UDP Listen Socket: %d\r\n", udpFd);
while (1)
{
OSTimeDly(TICKS_PER_SECOND);
}
}
else
{
iprintf("Listening for UDP packets on port %d\r\n", listenPort);
}
while (1)
{
IPADDR sourceIpAddress; // UDP packet source IP address
uint16_t localPort; // Port number UDP packet was sent to
uint16_t sourcePort; // UDP packet source port number
char buffer[80];
int len = recvfrom(udpFd, (uint8_t *)buffer, 80, &sourceIpAddress, &localPort, &sourcePort);
buffer[len] = '\0';
iprintf("\r\nReceived a UDP packet with %d bytes from : %I\r\n%s\r\n", len, sourceIpAddress, buffer);
}
}
void UserMain(void *pd)
{
int portNumber;
IPADDR destIpAddress;
char buffer[80];
init(); // Initialize network stack
WaitForActiveNetwork(TICKS_PER_SECOND * 5); // Wait for DHCP address
iprintf("Application: %s\r\nNNDK Revision: %s\r\n", AppName, GetReleaseTag());
// Get destination IP address
iprintf("Enter the UDP Server destination IP address: ");
gets(buffer);
destIpAddress = AsciiToIp(buffer);
iprintf("\r\n");
// Get the port number. This application uses the same port number for send and receive
iprintf("Enter the source/destination port number: ");
gets(buffer);
portNumber = atoi(buffer);
iprintf("\r\n");
// Create a UDP socket for sending
int udpFd = CreateTxUdpSocket(destIpAddress, portNumber, portNumber);
if (udpFd <= 0)
{
iprintf("Error Creating UDP Socket: %d\r\n", udpFd);
while (1)
{
OSTimeDly(TICKS_PER_SECOND);
}
}
else
{
iprintf("Sending/Receiving with host %I: %d\r\n", destIpAddress, portNumber);
}
// Create UDP receive task. The priority is higher than UserMain() so packets get processed as they are received
OSTaskCreatewName( UdpReceiveTask,
(void *)portNumber,
&UdpReceiveTaskStack[USER_TASK_STK_SIZE] ,
UdpReceiveTaskStack,
MAIN_PRIO - 1, // higher priority than UserMain
"UDP Receive" );
iprintf("Enter data and hit enter to send.\r\n");
while (1) // Loop forever displaying UDP data
{
gets(buffer);
iprintf("\r\n");
iprintf("Sending \"%s\" using UDP to %I : %d\r\n", buffer, destIpAddress, portNumber);
sendto(udpFd, (uint8_t *)buffer, strlen(buffer), destIpAddress, portNumber);
iprintf("\r\n");
}
}