NetBurner 3.5.0
PDF Version
 
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 application 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

/*
* This application will send/receive UDP packets with another host on a network,
* such as a PC. Use the MTTTY serial port program to access the menu and
* prompts to specify the destination IP address and port number.
*
* NetBurner also supplies an API for handling UDP as a C++ Class using UDPPacket.
*
* For an external UDP host you can use the NetBurner Java example, or the
* NetBurner UDP terminal program.
*/
#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];
/*
* This task will wait for incoming UDP packets and process them.
*/
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)
{
}
}
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);
}
}
/*
* UserMain
*/
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)
{
}
}
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");
}
}
Used to hold and manipulate IPv4 and IPv6 addresses in dual stack mode.
Definition ipv6_addr.h:41
#define TICKS_PER_SECOND
System clock ticks per second.
Definition nbrtos/include/constants.h:41
#define MAIN_PRIO
Recommend UserMain priority.
Definition nbrtos/include/constants.h:97
uint8_t OSTaskCreatewName(void(*task)(void *dptr), void *data, void *pstktop, void *pstkbot, uint8_t prio, const char *name)
Create a new task.
void OSTimeDly(uint32_t to_count)
Delay the task until the specified value of the system timer ticks. The number of system ticks per se...
Definition nbrtos.h:1732
const char * GetReleaseTag()
Returns the NNDK release tag information.
int recvfrom(int sock, puint8_t buffer, int len, IPADDR *pAddr, uint16_t *pLocal_port, uint16_t *pRemote_port)
Receive a UDP packet.
Definition udp.h:884
int sendto(int sock, puint8_t what_to_send, int len_to_send, const IPADDR &to_addr, uint16_t remote_port)
Send a UDP packet.
Definition udp.h:810
int CreateTxUdpSocket(const IPADDR &send_to_addr, uint16_t remote_port, uint16_t local_port)
Open a UDP socket for transmitting UDP packets.
Definition udp.h:754
int CreateRxUdpSocket(uint16_t listening_port)
Open a UDP socket for receiving incoming UDP packets.
void init()
System initialization. Ideally called at the beginning of all applications, since the easiest Recover...
bool WaitForActiveNetwork(uint32_t ticks_to_wait=120 *TICKS_PER_SECOND, int interface=-1)
Wait for an active network connection on at least one interface.
NetBurner System Initialization Header File.
#define USER_TASK_STK_SIZE
Definition nbrtos/include/constants.h:131
NetBurner System Functions.
NetBurner User Datagram Protocol Header File.