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.
#include <stdlib.h>
#include <string.h>
#include <utils.h>
const char *
AppName =
"UDP Sockets Example";
uint32_t UdpReceiveTaskStack[USER_TASK_STK_SIZE];
void UdpReceiveTask(void *pd)
{
int listenPort = (int)pd;
iprintf("UdpReceiveTask monitoring port %d\r\n", listenPort);
if (udpFd <= 0)
{
iprintf("Error Creating UDP Listen Socket: %d\r\n", udpFd);
while (1)
{
}
}
else
{
iprintf("Listening for UDP packets on port %d\r\n", listenPort);
}
while (1)
{
uint16_t localPort;
uint16_t sourcePort;
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;
char buffer[80];
iprintf("Enter the UDP Server destination IP address: ");
gets(buffer);
destIpAddress = AsciiToIp(buffer);
iprintf("\r\n");
iprintf("Enter the source/destination port number: ");
gets(buffer);
portNumber = atoi(buffer);
iprintf("\r\n");
if (udpFd <= 0)
{
iprintf("Error Creating UDP Socket: %d\r\n", udpFd);
while (1)
{
}
}
else
{
iprintf("Sending/Receiving with host %I: %d\r\n", destIpAddress, portNumber);
}
(void *)portNumber,
&UdpReceiveTaskStack[USER_TASK_STK_SIZE] ,
UdpReceiveTaskStack,
MAIN_PRIO - 1,
"UDP Receive" );
iprintf("Enter data and hit enter to send.\r\n");
while (1)
{
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");
}
}