NetBurner 3.5.6
PDF Version
TCP Multi-Socket Server

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

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. Creates a listening socket on port 23
  3. 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

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