NetBurner 3.5.6
PDF Version
TCP Keepalive

TCP Keep-Alive Server

Overview

This NetBurner application demonstrates a TCP server implementation with keep-alive functionality. The server listens for incoming TCP connections and uses keep-alive packets to detect when clients become unresponsive or disconnect unexpectedly.

Keep-alive is implemented with the TcpGetLastRxTime() and TcpSendKeepAlive() functions. TcpGetLastRxTime() returns the number of time ticks since the last time a packet was received. TcpSendKeepAlive() sends a keep-alive packet to the client, which is used when no data is has been transmitted within the timeout period.

The general concept is to call TcpGetLastRxTime(), call TcpSendKeepAlive(), wait a bit, then call TcpSendKeepAlive() a second time and verify the number of time ticks is different. If not, no packets were received and the client is not responding. Make sure to allow time for the client to respond to the keep alive packet. Do not call TcpGetLastRxTime more often than once every second to avoid performance issues.

Features

  • TCP server listening on port 23 (Telnet)
  • Automatic keep-alive packet transmission
  • Client connection monitoring and detection of unresponsive clients
  • Connection cleanup for dead connections
  • Debug output to console/serial port

How It Works

Keep-Alive Implementation

The keep-alive functionality is implemented using two key NetBurner functions:

Keep-Alive Process

The example creates a TCP server task that listens on port 23 by default. The server blocks with "ReadWithTimeOut" until data is received or the timer times out. If a timeout occurs the time of the last received packet is recorded and a keep-alive packet is sent. Then, the server goes back to waiting for a read again. If ReadWithTimeOut times out a second time, the value of the last received TCP packet is checked again. If the client is still active the value of lastRxTime will be different than the previous value (due to the keep-alive packet that was sent). If the client did not respond to the keep-alive packet the number will remain the same and the client is assumed to be non-responsive, and the connection is closed.

  1. Initial Read: Server waits for incoming data using ReadWithTimeout() with a 1-second timeout
  2. First Timeout: If no data is received within the timeout period:
    • Record the timestamp of the last received packet
    • Send a keep-alive packet to the client
    • Continue waiting for data
  3. Second Timeout: If timeout occurs again:
    • Check if the last received packet timestamp has changed
    • If timestamp is different: Client responded to keep-alive (connection is alive)
    • If timestamp is unchanged: Client is unresponsive, close the connection

Connection Flow

Client Connects -> Welcome Message -> Data Loop -> Keep-Alive Check -> Connection Management

Configuration

Default Settings

  • Listen Port: 23 (Telnet)
  • Read Timeout: 1 second
  • Buffer Size: 4096 bytes
  • Maximum Pending Connections: 5

Customization

To modify the listening port, change the TCP_LISTEN_PORT constant:

#define TCP_LISTEN_PORT 23 // Change to desired port number

Testing the Application

Using Telnet

From a Windows command prompt:

telnet <NetBurner_IP_Address>

From Linux/Mac terminal:

telnet <NetBurner_IP_Address> 23

Using PuTTY

  1. Open PuTTY
  2. Select "Raw" or "Telnet" connection type
  3. Enter the NetBurner device IP address
  4. Set port to 23
  5. Click "Open"

Expected Behavior

  1. Upon connection, you'll see: "Welcome to the NetBurner TCP Server"
  2. The server will display your connection information
  3. Any data you type will be echoed back with byte count information
  4. If you stop sending data, keep-alive packets will be sent automatically
  5. Status messages appear on the NetBurner's debug/console serial port

Code Structure

Main Components

  • UserMain(): Initializes the system and creates the TCP server task
  • TcpServerTask(): Main server loop that handles client connections
  • Keep-Alive Logic: Integrated within the data reading loop

Key Variables

  • fdListen: Listening socket file descriptor
  • fdNet: Client connection file descriptor
  • LastRxTime: Timestamp of last received packet
  • keepAliveSent: Flag to track keep-alive state
  • RXBuffer: Buffer for incoming data

Performance Considerations

  • Do not call TcpGetLastRxTime() more frequently than once per second to avoid performance issues
  • Allow sufficient time for clients to respond to keep-alive packets before determining they are unresponsive
  • The server can handle multiple sequential connections but processes them one at a time

Debug Output

The application provides detailed debug information including:

  • Connection establishment notifications
  • Data reception logs with byte counts
  • Keep-alive packet transmission notifications
  • Client response status
  • Connection closure notifications

All debug output is sent to the NetBurner's debug/console serial port.