Socat - Data Stream Alchemy

Nicola Coretti

2026-02-11

Why Socat?

The Digital Gardena Adapter

stux

Demo

Serial over TCP over UDP

  1. Serial <-> TCP-Server
  2. TCP-Client <-> UDP-Server
  3. UDP-Server <-> UDP-Client

Test Server for Custom Protocol(s)

Binary Protocol

+-----------+-------------------+-----------------------+
| **Byte**  | **Description**   | **Values**            |
+-----------+-------------------+-----------------------+
| Byte 0    | Transformation    | 0x00: None            |
|           |                   | 0x01: Upper           |
|           |                   | 0x02: Lower           |
+-----------+-------------------+-----------------------+
| Byte 1    | Length            | 0-255 (Single byte)   |
+-----------+-------------------+-----------------------+
| Bytes 2+  | String Data       | Raw ASCII/UTF-8 bytes |
+-----------+-------------------+-----------------------+

Example Packet

Input: “hello” → Uppercase

Field Value Hex
Transformation Uppercase 0x01
Length 5 0x05
String “hello” 68 65 6C 6C 6F

JSON Decoder

Input:

Payload: 0x01 0x05 0x68 0x65 0x6C 0x6C 0x6F

Output:

{
  "Transformation": "Upper",
  "String": "hello"
}

JSON Encoder

Input:

{
  "Transformation": "Upper",
  "String": "hello"
}

Output:

Payload: 0x01 0x05 0x68 0x65 0x6C 0x6C 0x6F

Server Application

  1. Decode (decode.py)
  2. Transform (server.py)
  3. Encode (encode.py)

Basics

Command

socat [options] <ADDRESS> <ADDRESS>

Address

<TYPE>:<ADDRESS-SPEC>[,OPTIONS]

types

  • FILE
  • EXEC
  • SHELL
  • SYSTEM
  • TCP, TCP-LISTEN
  • UDP, UDP-LISTEN, UDP-RECV
  • OPENSSL

Advanced

Debug Options

  • -d, -d2, -d3, -d4
  • -v, -x, -r

Address Options

Option Groups

FD, OPEN, SOCKET, UNIX, TCP, …

Helpful Options

  • raw, rawer
  • fork
  • mode, user, group
  • reuseaddr

Expert

Phases

  1. Init Phase
  2. Open Phase
  3. Transfer Phase
  4. Closing Phase

Bi-Directional

(Default)

Unidirectional

Bi-Directional + Split

(Dualaddress)

Questions ?

TL;DR

imageflip