All projects

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

Open on GitHub

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

  1. Clone the repository:
git clone https://github.com/yourusername/spring-tcp-server.git
cd spring-tcp-server
  1. Build the project:
mvn clean install
  1. 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:

EndpointMethodDescription
/api/devicesGETList all devices (connected and disconnected)
/api/devices/connectedGETList only connected devices
/api/devices/disconnectedGETList only disconnected devices

๐Ÿ“Š TCP Communication

Devices can connect to the TCP server on the configured port (default: 8081). The server:

  1. Tracks when devices connect and disconnect
  2. Processes incoming messages
  3. Sends responses back to the connected devices

๐Ÿงฉ Architecture

The application follows a clean architecture with separation of concerns:

  • TcpServerConfig: Configures the TCP server
  • TcpMessageHandler: Processes incoming TCP messages
  • TcpConnectionEventHandler: Manages connection events
  • DeviceService: Tracks device state
  • DeviceController: 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

More like this