NetBurner 3.5.6
PDF Version
TCP Keepalive Multiple

TCP Multi-Socket Server with Keep-Alive

A robust TCP server implementation that handles multiple concurrent connections with automatic dead connection detection using TCP keep-alive mechanisms.

Overview

This application demonstrates a production-ready TCP server that can:

  • Handle up to 10 simultaneous TCP connections
  • Automatically detect and close dead/disconnected clients
  • Implement TCP keep-alive to prevent resource waste from zombie connections
  • Use efficient I/O multiplexing with select() for scalable connection management

Features

Multi-Socket Connection Handling

  • Concurrent Connections: Supports up to 10 simultaneous TCP connections
  • Non-blocking I/O: Uses select() for efficient multiplexing of multiple sockets
  • Dynamic Connection Management: Automatically accepts new connections and manages connection slots

Keep-Alive Mechanism

  • Idle Detection: Monitors connections for inactivity (default: 5 seconds)
  • Automatic Probing: Sends keep-alive packets to idle connections
  • Dead Connection Cleanup: Closes connections that don't respond to keep-alive probes (timeout: 3 seconds)
  • Resource Protection: Prevents server resource exhaustion from zombie connections

Connection Monitoring

  • Real-time Status: Displays connection events, data transfers, and keep-alive activities
  • Client Information: Shows client IP addresses and port numbers
  • Debug Output: Comprehensive logging of connection lifecycle events

Configuration

Server Settings

#define listenPort (23) // TCP port (Telnet)
#define maxConnections (10) // Max simultaneous connections
#define readBufferSize (1024) // Read buffer size per connection

Keep-Alive Timers

#define keepAliveTimeout (5 * TICKS_PER_SECOND) // 5 seconds idle timeout
#define keepAliveResponseTimeout (3 * TICKS_PER_SECOND) // 3 seconds response timeout

Usage

Starting the Server

  1. Compile and deploy the application to your NetBurner device
  2. The server automatically starts listening on port 23 (Telnet)
  3. Monitor the console for connection status and debug information

Connecting Clients

# Connect using telnet
telnet <device_ip> 23
# Or using netcat
nc <device_ip> 23

Client Commands

  • Send any data: Resets the keep-alive timer and echoes confirmation
  • Send 'Q' or 'q': Gracefully disconnects from the server
  • Stay idle: Triggers keep-alive mechanism after 5 seconds

How It Works

Connection Lifecycle

  1. Accept: New connections are accepted and assigned to available slots
  2. Monitor: Each connection is monitored for data activity using select()
  3. Keep-Alive: Idle connections receive keep-alive probes
  4. Cleanup: Dead or disconnected clients are automatically removed

Keep-Alive Process

  1. Idle Detection: Server tracks last received data timestamp for each connection
  2. Probe Transmission: After 5 seconds of inactivity, a TCP keep-alive packet is sent
  3. Response Monitoring: Server waits up to 3 seconds for any TCP-level response
  4. Connection Termination: Connections that don't respond are closed and removed

TCP Stack Integration

Testing Scenarios

Normal Operation

# Connect and send data
telnet <device_ip> 23
> hello
Data received
> Q
Goodbye!

Keep-Alive Testing

  1. Connect to the server but don't send any data
  2. After 5 seconds, observe keep-alive probe message in server console
  3. Continue to stay idle for 3+ more seconds
  4. Connection will be automatically closed

Dead Connection Testing

  1. Connect to the server
  2. Physically disconnect the network cable or crash the client
  3. After 8 seconds total (5 + 3), the server will detect and close the dead connection

Error Handling

  • Socket Errors: Automatically detected and connections are closed
  • Accept Failures: Gracefully handled with appropriate error messages
  • Buffer Overflows: Protected by fixed buffer sizes
  • Resource Limits: New connections rejected when server is full

Use Cases

Production Applications

  • Long-lived Connections: Services that maintain persistent client connections
  • Resource Management: Systems with limited connection resources
  • Network Monitoring: Applications requiring dead connection detection
  • Embedded Systems: Resource-constrained environments needing efficient connection handling

Network Scenarios

  • Unreliable Networks: Environments with frequent network interruptions
  • Mobile Clients: Connections that may drop without proper closure
  • Firewall Traversal: Maintaining connections through NAT/firewall devices
  • Load Balancing: Backend services requiring connection health monitoring

Performance Considerations

  • Memory Usage: Each connection uses approximately 1KB for read buffers plus keep-alive tracking
  • CPU Overhead: Minimal - keep-alive checks only run once per second
  • Network Traffic: Keep-alive probes are small TCP packets sent only when needed
  • Scalability: Can be extended to support more connections by increasing maxConnections

Dependencies

Customization

Timeout Values

Adjust the keep-alive timing constants for your specific application requirements:

  • Shorter timeouts for faster dead connection detection
  • Longer timeouts for reduced network overhead

Connection Limits

Modify maxConnections based on available system resources and application needs.

Troubleshooting

Common Issues

  • **"Server Full" message**: All connection slots are occupied
  • Immediate disconnections: Check network connectivity and firewall settings
  • Keep-alive not working: Verify TCP stack support and timing configuration

Debug Output

The application provides detailed console output for monitoring:

  • Connection establishment and termination
  • Keep-alive probe transmission and responses
  • Error conditions and socket states