Echo Server
A simple TCP echo server written in Rust using Tokio, designed for testing and development purposes.
Features
- Async TCP Server: Built with Tokio for high-performance async I/O
- Connection Management: Handles multiple concurrent connections with configurable limits
- Graceful Shutdown: Responds to SIGINT/SIGTERM signals and supports programmatic shutdown
- Configurable Timeouts: Read and write timeouts to prevent hanging connections
- Configurable Buffer Size: Adjustable buffer size for different use cases
- Comprehensive Testing: Extensive test suite covering various scenarios
- Binary Data Support: Handles both text and binary data
- Unicode Support: Full UTF-8 support for international characters
- High Concurrency: Can handle thousands of concurrent connections
Architecture
The echo server follows a simple but robust architecture:
Core Components
EchoServer: Main server that manages the TCP listener and connection handlingConfig: Configuration struct for server settings (bind address, connection limits, timeouts, buffer size)EchoClient: Test client for connecting to and testing the server- Connection Handler: Async task that handles individual client connections with timeouts
Design Principles
- SOLID Principles: Clean separation of concerns with single responsibility
- Async-First: All I/O operations are non-blocking using Tokio
- Error Handling: Comprehensive error handling with context information and timeouts
- Resource Management: Automatic cleanup of resources using Rust's ownership system
- Thread Safety: Atomic connection counting for concurrent access
Configuration
The server can be configured through the Config struct:
let config = Config ;
Configuration Options
bind_addr: Socket address to bind the server tomax_connections: Maximum number of concurrent connectionsbuffer_size: Buffer size for reading/writing dataread_timeout: Read timeout for connections (None for no timeout)write_timeout: Write timeout for connections (None for no timeout)
Usage
Running the Server
# Run on default port 8080
# Run on specific port
# Run in release mode for better performance
Testing the Server
# Run all tests
# Run specific test
# Run tests with output
# Run tests with logging
RUST_LOG=info
Manual Testing
You can test the server manually using netcat or telnet:
# Start the server
# In another terminal, connect with netcat
|
# Or use telnet
Testing
The echo server includes comprehensive tests covering:
Unit Tests
- Basic Echo: Simple text echo functionality
- Multiple Messages: Multiple messages over single connection
- Binary Data: Binary data handling
- Large Data: Data larger than internal buffers
- Unicode: International character support
Integration Tests
- Concurrent Clients: Multiple simultaneous connections
- Connection Limits: Server behavior under connection limits
- Graceful Shutdown: Proper shutdown handling
- Timeout Configuration: Timeout behavior testing
- Stress Testing: High-load scenarios
Test Coverage
# Run tests with coverage (requires cargo-tarpaulin)
# Run specific test categories
Performance
The echo server is optimized for:
- Low Latency: Async I/O for minimal response times
- High Throughput: Efficient handling of multiple connections
- Memory Efficiency: Minimal memory footprint per connection
- CPU Efficiency: Non-blocking operations prevent CPU waste
- Scalability: Can handle thousands of concurrent connections
Use Cases
This echo server is useful for:
- Network Testing: Testing network connectivity and protocols
- Load Testing: Stress testing network infrastructure
- Development: Quick testing of network clients
- Learning: Understanding TCP server implementation
- Benchmarking: Performance testing of network stacks
Development
Building
# Debug build
# Release build
# Check for issues
Adding New Tests
To add new tests, add them to src/tests.rs:
async
License
This project is licensed under the MIT License - see the LICENSE file for details.