UDP Sockets Via Example
Overview
This NetBurner application demonstrates UDP socket communication using the NetBurner Development Kit (NNDK). The application creates multiple UDP sockets across different network interfaces and allows bidirectional communication with other hosts on the network.
Features
- Multiple Socket Types: Creates receive-only (RX), transmit-only (TX), and bidirectional (RX/TX) UDP sockets
- Multi-Interface Support: Automatically creates sockets for all available network interfaces
- Port Management: Uses sequential port numbering starting from port 5000
- Select-based Receiving: Demonstrates non-blocking UDP packet reception using the select() system call
- Interactive Menu: Provides a simple command interface via serial terminal
Application Functionality
Socket Creation
The application creates three types of UDP sockets for each interface:
- RX Socket: Receive-only socket for incoming UDP packets
- RX/TX Socket: Bidirectional socket for both sending and receiving
- TX Socket: Transmit-only socket for outgoing UDP packets
Interface Detection
The program automatically detects all available network interfaces and creates sockets for:
- All interfaces (general sockets)
- Specific interface numbers
- Specific IP addresses
Port Assignment
Starting from port 5000, the application assigns ports sequentially:
- Port N: RX socket
- Port N+1: RX/TX socket
- Port N+2: TX socket
Usage
Setup
- Compile and flash the application to your NetBurner device
- Connect to the device using MTTTY serial port program
- Wait for the network to initialize and obtain a DHCP address
Operation
- Enter Destination IP: When prompted, enter the IP address of the UDP host you want to communicate with
- View Socket Information: The application displays a table showing:
RX | RXTX| TX | On
5000| 5001| 5002|All
5003| 5004| 5005|ifn1 on 192.168.1.100
5006| 5007| 5008|IP:192.168.1.100
- Send Test Packets: Press 'T' or 't' to transmit test messages to all TX-capable sockets
- Receive Packets: The application automatically displays received UDP packets with source information
Commands
- T or t: Transmit test packets to all transmit-capable sockets
Code Structure
Main Components
AddSocketsIfn(): Creates sockets for a specific interface number
AddSocketsIP(): Creates sockets for a specific IP address
TransmitAll(): Sends test messages through all transmit sockets
UserMain(): Main application loop handling initialization and packet processing
Key Variables
rx_fd[]: Array of receive-capable socket file descriptors
tx_fd[]: Array of transmit-capable socket file descriptors
using_port: Current port number for socket creation
nrxfd, ntxfd: Counters for RX and TX socket arrays
Testing
External UDP Hosts
You can test the application using:
- NetBurner Java UDP example application
- NetBurner UDP terminal program
- Any standard UDP client/server application
Packet Reception
When UDP packets are received, the application displays:
- Number of bytes received
- Source IP address
- Local port number
- Remote port number
- Hexadecimal dump of packet data
API Information
This example demonstrates the NetBurner UDP Sockets API functions:
For C++ applications, NetBurner also provides a UDPPacket class API for object-oriented UDP handling.
Notes
- The application enables system diagnostics for debugging (should be disabled in production)
- All sockets use UDP protocol for lightweight, connectionless communication
- The select() mechanism allows efficient handling of multiple socket file descriptors
- Socket creation spans all detected network interfaces automatically