NetBurner 3.5.8
PDF Version
TCP Multi-Socket Server

Example Path: examples/TCP/TcpMultiSocketServer

TCP Multi-Socket Server

Overview

This application demonstrates a TCP server implementation that can handle multiple simultaneous client connections using the NetBurner embedded platform. The server uses the select() function to efficiently manage multiple TCP connections concurrently without blocking.

Features

  • Multi-client support: Handles up to 10 simultaneous TCP connections
  • Non-blocking I/O: Uses select() to efficiently manage multiple file descriptors
  • Connection management: Automatically accepts new connections and manages client disconnections
  • Simple protocol: Clients can send 'Q' to quit their connection
  • Debug output: Status messages displayed on debug serial port
  • Live web view: A built-in web page shows the current connection table in real time

Configuration

The application uses the following configurable parameters:

  • Listen Port: 23 (Telnet port)
  • Maximum Connections: 10 simultaneous clients
  • Read Buffer Size: 1024 bytes per connection

These values can be modified by changing the corresponding #define statements in main.cpp.

How It Works

Server Initialization

  1. Initializes the NetBurner system and waits for network connectivity
  2. Starts the web server so the live connection page is reachable at http://<device_ip>/
  3. Creates a listening socket on port 23
  4. Displays the device's IP addresses for client connection reference

Connection Management

The server uses a file descriptor array (fdArray) to track active client connections. The main loop:

  1. Sets up file descriptor sets for select() monitoring
  2. Calls select() to block until activity occurs on any socket
  3. Processes new connections when clients connect to the listening port
  4. Handles client data when existing connections have data to read
  5. Manages errors by closing problematic connections

Client Interaction

  • Welcome message: New clients receive a greeting upon connection
  • Quit command: Clients can send 'Q' to cleanly disconnect
  • Echo behavior: The server reads client data and logs it to the debug port
  • Connection limits: If the server is full (10 connections), new clients are rejected with an error message

Web Interface

Point a browser at http://<netburner_ip_address>/ to see a live dashboard of the server's connection state. The page is a self-contained static HTML file (plain CSS, no framework or CDN); its connection table is filled in on the device by the <!--CPPCALL ShowConnectionsWeb --> tag. That tag hands the HTTP socket to ShowConnectionsWeb() in main.cpp, which walks the same fdArray[] the server loop uses and prints each slot's state and client IP:port straight into the page with fdprintf().

The table refreshes itself: a small script on the page polls connections.html – the same callback served on its own – every 1.5 seconds and swaps in the fresh table, so the view stays live without a full-page reload. With scripting disabled the page still renders the table once when it loads. Open a few Telnet sessions (below) and watch the slots fill in and clear.

Testing the Application

Using Telnet

The easiest way to test this application is using multiple Telnet sessions:

telnet <netburner_ip_address> 23

You can open up to 10 simultaneous Telnet connections to test the multi-client functionality.

Expected Behavior

  1. Successful connection: You'll see "Welcome to the NetBurner Multi-Socket TCP Server! 'Q' to quit."
  2. Server full: If you try to connect when 10 clients are already connected, you'll see "I am sorry, but the server is full"
  3. Quit command: Type 'Q' to cleanly disconnect
  4. Debug output: Connection status, received data, and errors are logged to the serial debug port

Code Structure

Key Functions

  • UserMain(): Main application entry point and event loop
  • select(): Monitors multiple file descriptors for activity
  • accept(): Accepts new client connections
  • read(): Reads data from client connections
  • close(): Closes file descriptors and cleans up connections

File Descriptor Management

The application maintains an array of file descriptors (fdArray) where:

  • Index 0-9: Active client connections (0 = unused slot)
  • Separate fdListen: The listening socket for new connections

Error Handling

  • Connection errors: Automatically detected and connections are closed
  • Read errors: Treated as connection problems and handled gracefully
  • Listen socket errors: The listening socket is closed and reopened

Troubleshooting

  • Cannot connect: Verify the NetBurner device has network connectivity and check IP address
  • Connection refused: Ensure no other service is using port 23
  • Unexpected disconnections: Check debug serial output for error messages
  • Server appears full: Existing connections may not have closed properly; restart the application