Example Path: examples/TCP/TcpServerVia
TCP Server Via Example
Overview
This application demonstrates a TCP server implementation for multi-interface NetBurner devices using the listenvia() function. The server creates multiple listening sockets - one that listens on all network interfaces and additional sockets that listen on specific network interfaces.
Features
- Multi-Interface Support: Creates listening sockets for each available network interface
- Multiple Concurrent Connections: Supports up to 10 simultaneous TCP connections
- Port Management: Uses incrementing port numbers starting from port 23
- Single-Task Architecture: Uses select() to handle multiple sockets within one task
- Connection Monitoring: Provides detailed logging of connections and interface information
- Live Web View: A built-in web page shows the listening sockets and connected clients in real time
Technical Details
Configuration
- Base Port: 23 (Telnet)
- Maximum Connections: 10 concurrent connections
- Read Buffer Size: 1,024 bytes
- Connection Queue: Up to 5 pending connection requests per listening socket
Socket Architecture
The application creates the following listening sockets:
- One socket listening on all interfaces (port 23)
- For each available network interface:
- One socket on port 24 (listenPort + 1)
- One socket on port 24 + interface_number
Network Interface Handling
- Automatically detects available network interfaces using GetInterfaceBlock()
- Creates dedicated listening sockets for each interface using listenvia()
- Provides interface-specific connection information to clients
Web Interface
Point a browser at http://<device_ip>/ to see a live dashboard of the server. The page is a self-contained static HTML file (plain CSS, no framework or CDN) with two device-rendered tables:
- Listening Sockets – one row per entry in the listeners[] array, showing the file descriptor, the interface it is bound to, and its local port. The all-interfaces listen() socket and the per-interface listenvia() sockets appear together so you can see the "via" layout at a glance. The bound interface is taken from the socket's slot in listeners[] (slot 0 is the all-interfaces listener; the two listenvia() sockets for interface N occupy slots N*2 and N*2+1), because TcpGetSocketInterface() only reports a bound interface on an accepted socket, not a listening one.
- Connected Clients – one row per fdArray[] slot. For each active client it shows the local port (which listener accepted it – 23 for all-interfaces, 24 and up for a listenvia() socket), the physical interface the connection arrived on (from TcpGetSocketInterface()), and the client IP:port. This table refreshes itself: a small script polls connections.html – the same callback served on its own – every 1.5 seconds, so it stays live without a full-page reload. With scripting disabled the table still renders once when the page loads.
Both tables are produced on the device by <!--CPPCALL --> tags that hand the HTTP socket to ShowListenersWeb() / ShowConnectionsWeb() in main.cpp, which walk the same arrays the server loop uses and print each row with fdprintf(). Open Telnet sessions to the different ports and watch the slots fill in.
Usage
Building and Running
- Compile the application for your NetBurner device
- Deploy to the target hardware
- The application will automatically start listening on configured ports
Testing Connections
Use Telnet to test connections from the command line:
telnet <ip_address> <port>
Examples:
- telnet 192.168.1.100 23 - Connect to the all-interfaces socket
- telnet 192.168.1.100 24 - Connect to interface 1, port 24
- telnet 192.168.1.100 25 - Connect to interface 1, port 25
Client Interaction
- Upon connection, clients receive a welcome message
- Interface information is displayed showing which interface accepted the connection
- Type 'Q' to quit and close the connection
- Server echoes received data and provides connection status
Code Structure
Main Components
Initialization**
Key Functions Used
Error Handling
- Connection Limit: When server is full, new connections are rejected with a message
- Socket Errors: Automatic detection and recovery of failed listening sockets
- Read Errors: Proper cleanup of corrupted client connections
- Interface Failures: Continues operation even if some interfaces fail
Application Output
The application provides detailed console output including:
- Network interface discovery and socket creation status
- Connection acceptance with client IP and port information
- Interface identification for each connection
- Data reception logging
- Connection closure notifications
- Error condition reporting
Requirements
- NetBurner hardware platform
- NetBurner SDK with TCP networking support
- Multi-interface network configuration (optional for full functionality)
Notes
- System diagnostics are enabled by default (should be disabled for production)
- Socket file descriptors are automatically managed and cleaned up
- The server continues running indefinitely until manually stopped