Edge AI & hardware
Spring Boot TCP for IoT
TCP server and client patterns for device communication.
Spring Integration examples that track connected devices and send or receive data over TCP, with REST APIs and retries.
Why I built it
A small reference for the TCP patterns that keep coming up in device work: tracking connections, handling messages and exposing device state over REST.
Architecture
How it fits together
Devices
TCP clients
Spring Boot
Spring Integration TCP
Connection tracker
connected and disconnected
API
REST endpoints
device status
Documentation
From the README
A robust TCP server implementation built with Spring Boot that tracks connected and disconnected devices while providing a RESTful API to access device information.
๐ Overview
This project implements a TCP server that allows devices to connect, exchange messages, and provides a REST API to monitor the status of all devices. The server tracks both connected and disconnected devices, making it ideal for IoT applications and network monitoring.
โจ Features
- TCP server implementation using Spring Integration
- Real-time device connection tracking
- Message processing with automatic response capability
- RESTful API for device monitoring
- Event-based architecture for connection management
๐ Getting Started
Prerequisites
- Java 1.8 or higher
- Maven 3.6+
- Spring Boot 2.7+ (compatible with Spring Boot 3.x)
Installation
- Clone the repository:
git clone https://github.com/yourusername/spring-tcp-server.git
cd spring-tcp-server
- Build the project:
mvn clean install
- Run the application:
mvn spring-boot:run
๐ง Configuration
The server can be configured through the application.properties file:
# REST API port
server.port=8080
# TCP server port
tcp.server.port=8081
๐ API Endpoints
The following REST endpoints are available:
| Endpoint | Method | Description |
|---|---|---|
/api/devices | GET | List all devices (connected and disconnected) |
/api/devices/connected | GET | List only connected devices |
/api/devices/disconnected | GET | List only disconnected devices |
๐ TCP Communication
Devices can connect to the TCP server on the configured port (default: 8081). The server:
- Tracks when devices connect and disconnect
- Processes incoming messages
- Sends responses back to the connected devices
๐งฉ Architecture
The application follows a clean architecture with separation of concerns:
TcpServerConfig: Configures the TCP serverTcpMessageHandler: Processes incoming TCP messagesTcpConnectionEventHandler: Manages connection eventsDeviceService: Tracks device stateDeviceController: Exposes REST endpoints
๐ Testing
You can test the TCP server using any TCP client like Telnet:
telnet localhost 8081
For the REST API, use tools like cURL or Postman:
curl http://localhost:8080/api/devices
Edge AI & hardware