|
NetBurner 3.5.8
PDF Version |
BSD-style socket functions for UDP communication. More...
Functions | |
| int | CreateRxUdpSocket (uint16_t listening_port) |
| Create receive-only UDP socket bound to specified port. | |
| int | CreateTxUdpSocket (const IPADDR &send_to_addr, uint16_t remote_port, uint16_t local_port) |
| Create transmit-only UDP socket for sending to specified destination. | |
| int | CreateRxTxUdpSocket (const IPADDR &send_to_addr, uint16_t send_to_remote_port, uint16_t local_port) |
| Create bidirectional UDP socket for both sending and receiving. | |
| int | sendto (int sock, puint8_t what_to_send, int len_to_send, const IPADDR &to_addr, uint16_t remote_port) |
| Send UDP packet through socket to specified destination. | |
| int | sendto6via (int sock, puint8_t what_to_send, int len_to_send, const IPADDR &to_addr, uint16_t remote_port, int intfnum) |
| int | sendtovia (int sock, puint8_t what_to_send, int len_to_send, const IPADDR &to_addr, uint16_t remote_port, int intfnum) |
| Send via specific interface. | |
| int | recvfrom (int sock, puint8_t buffer, int len, IPADDR *pAddr, uint16_t *pLocal_port, uint16_t *pRemote_port) |
| Receive UDP packet from socket with sender information. | |
| int | SendFragmentedUdpPacket (const IPADDR &to, uint16_t source_port, uint16_t dest_port, puint8_t data, int length) |
| Send large UDP packet (>MTU) with IP fragmentation. | |
| int | CreateTxUdpSocket4Via (const IPADDR4 send_to_addr, uint16_t remote_port, uint16_t local_port, int interfacenum) |
| int | CreateTxUdpSocket4Via (const IPADDR4 send_to_addr, uint16_t remote_port, uint16_t local_port, const IPADDR4 interfaceIp) |
| int | CreateRxTxUdpSocket4Via (const IPADDR4 send_to_addr, uint16_t send_to_remote_port, uint16_t local_port, int interfacenum) |
| int | CreateRxTxUdpSocket4Via (const IPADDR4 send_to_addr, uint16_t send_to_remote_port, uint16_t local_port, const IPADDR4 interfaceIp) |
| int | CreateRxTxUdpSocket6Via (const IPADDR send_to_addr, uint16_t send_to_remote_port, uint16_t local_port, int interfacenum) |
| int | CreateRxTxUdpSocket6Via (const IPADDR send_to_addr, uint16_t send_to_remote_port, uint16_t local_port, const IPADDR interfaceIp) |
| int | CreateTxUdpSocket6Via (const IPADDR send_to_addr, uint16_t remote_port, uint16_t local_port, int interfacenum) |
| int | CreateTxUdpSocket6Via (const IPADDR send_to_addr, uint16_t remote_port, uint16_t local_port, const IPADDR interfaceIp) |
BSD-style socket functions for UDP communication.
Provides familiar Berkeley Sockets (BSD) style interface for UDP networking. These functions offer an alternative to the UDPPacket class for developers who prefer traditional socket programming or are porting existing code.
The UDP Socket API provides three main operation types:
1. Receive-Only Socket (Server/Listener)
2. Transmit-Only Socket (Client/Sender)
3. Bidirectional Socket (Request/Response)
Local Port (Binding)
Remote Port (Destination)
All socket creation functions return:
Common error codes:
The API automatically handles both IPv4 and IPv6 based on IPADDR type:
For devices with multiple network interfaces:
| Feature | BSD Sockets | UDPPacket Class |
|---|---|---|
| API Style | Familiar C functions | C++ object-oriented |
| Memory Management | Manual | Automatic (RAII) |
| Buffer Access | Copy-based | Zero-copy available |
| Multi-destination | Multiple sends | SendAndKeep() support |
| Integration | select()/poll() | OS_FIFO queues |
| Portability | Standard POSIX | NetBurner-specific |
| Best For | Porting code, C code | New C++ projects |
For packets exceeding MTU, use SendFragmentedUdpPacket():
Note: Fragmentation should be avoided when possible due to:
echo "test" | nc -u 192.168.1.100 8080
nc -ul 8080 ```
|
inline |
#include <udp.h>
Create bidirectional UDP socket for both sending and receiving.
Creates a UDP socket that can both send packets to a specified destination and receive packets on a local port. This combines the functionality of CreateTxUdpSocket() and CreateRxUdpSocket() into a single socket, ideal for request/response patterns and client-server communication.
Socket Characteristics:
Common Use Cases:
| send_to_addr | Destination IP address for sending packets Use AsciiToIp() or AsciiToIp4() to convert from string |
| send_to_remote_port | Destination port number for sending (1-65535) |
| local_port | Local port to bind for receiving (0-65535)
|
Expand for Example Usage
Example 1: DNS Query (Request/Response Pattern)
Example 2: Echo Client with Response
Example 3: Persistent Connection Pattern
|
inline |
#include <udp.h>
|
inline |
#include <udp.h>
|
inline |
#include <udp.h>
|
inline |
#include <udp.h>
| int CreateRxUdpSocket | ( | uint16_t | listening_port | ) |
#include <udp.h>
Create receive-only UDP socket bound to specified port.
Creates a UDP socket configured only for receiving packets. The socket is bound to the specified local port and can receive UDP packets from any source. This is the standard function for creating UDP server/listener sockets.
Socket Characteristics:
Common Use Cases:
| listening_port | Local port number to bind (1-65535)
|
Expand for Example Usage
Example 1: Simple UDP Server
Example 2: Echo Server
Example 3: Broadcast Receiver
|
inline |
#include <udp.h>
Create transmit-only UDP socket for sending to specified destination.
Creates a UDP socket configured only for sending packets to a specific remote address and port. The socket can optionally be bound to a specific local port, or use 0 to auto-assign an ephemeral port (recommended for most clients).
Socket Characteristics:
Common Use Cases:
| send_to_addr | Destination IP address (IPv4 or IPv6 depending on build config) Use AsciiToIp() or AsciiToIp4() to convert from string |
| remote_port | Destination port number (1-65535) Must match the listening port on remote system |
| local_port | Local port to bind (0-65535)
|
Expand for Example Usage
Example 1: Simple UDP Client
Example 2: Periodic Sensor Data Transmission
Example 3: Syslog Client
Example 4: Multiple Destinations (Create Multiple Sockets)
|
inline |
#include <udp.h>
|
inline |
#include <udp.h>
|
inline |
#include <udp.h>
|
inline |
#include <udp.h>
|
inline |
#include <udp.h>
Receive UDP packet from socket with sender information.
Receives a UDP datagram from the specified socket and returns the sender's IP address and port. This is the standard BSD socket function for receiving UDP data. Works with sockets created by CreateRxUdpSocket() or CreateRxTxUdpSocket(). Blocks until data arrives.
| sock | Socket file descriptor from CreateRxUdpSocket() or CreateRxTxUdpSocket() |
| buffer | Destination buffer for received data |
| len | Maximum bytes to read (buffer size, recommend 1500+ bytes) |
| pAddr | [OUT] Sender's IP address (can be NULL) |
| pLocal_port | [OUT] Local port packet arrived on (can be NULL) |
| pRemote_port | [OUT] Sender's port number (can be NULL) |
Example: Echo Server
|
inline |
#include <udp.h>
Send large UDP packet (>MTU) with IP fragmentation.
Sends a UDP packet that exceeds the Maximum Transmission Unit (MTU) by automatically fragmenting it at the IP layer. The function handles both IPv4 and IPv6 addressing and reassembly is handled by the receiving host.
This is useful for sending large datagrams without implementing application-level fragmentation. However, note that IP fragmentation can reduce reliability since loss of any fragment requires retransmission of the entire datagram.
| to | Destination IP address (IPv4 or IPv6) |
| source_port | Source UDP port number. Use 0 for automatic port assignment (recommended) |
| dest_port | Destination UDP port number |
| data | Pointer to data buffer to send |
| length | Length of data in bytes (can exceed MTU) |
Example:
|
inline |
#include <udp.h>
Send UDP packet through socket to specified destination.
Transmits a UDP datagram through the specified socket to the given IP address and port. This is the standard BSD socket function for sending UDP data. Works with sockets created by CreateTxUdpSocket() or CreateRxTxUdpSocket().
Operation:
| sock | Socket file descriptor from CreateTxUdpSocket() or CreateRxTxUdpSocket() Must be a valid socket (>= 0) or behavior is undefined |
| what_to_send | Pointer to data buffer to transmit Buffer content is copied during transmission |
| len_to_send | Number of bytes to send from buffer
|
| to_addr | Destination IP address (IPv4 or IPv6 based on build configuration) Use AsciiToIp() or AsciiToIp4() to convert from string format |
| remote_port | Destination port number (1-65535) Must match listening port on remote system |
Expand for Example Usage
Example 1: Simple Send
Example 2: Send Binary Data
|
inline |
#include <udp.h>