Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

3.2 Run a TCP echo simulator

Create a local TCP server that returns each line it receives. This provides the device endpoint for the IOC from the preceding lesson.

Install socat using the package instructions. The scripts also support tcpsvd where it is installed.

Create the two scripts

  1. From the jeonglee_Demo/ application root, create a simulator directory:

    mkdir simulator
    cd simulator
    
  2. Save this launcher as tcpserver.bash:

    #!/usr/bin/env bash
    # SPDX-License-Identifier: GPL-2.0-or-later
    # Run a line-based device handler for each local TCP connection.
    set -euo pipefail
    
    PORT=9399
    HANDLER=connection_handler.sh
    [[ $# -le 1 ]] || { printf 'Usage: %s [PORT]\n' "$0" >&2; exit 2; }
    if [[ $# -eq 1 ]]; then
        PORT="$1"
    fi
    [[ "$PORT" =~ ^[0-9]{1,5}$ ]] || { printf 'Invalid port\n' >&2; exit 2; }
    PORT="$((10#$PORT))"
    (( PORT >= 1 && PORT <= 65535 )) || { printf 'Invalid port\n' >&2; exit 2; }
    
    SCRIPT_DIR="$(cd -- "$(dirname -- "$0")" && pwd)"
    HANDLER_SCRIPT="$SCRIPT_DIR/$HANDLER"
    [[ -x "$HANDLER_SCRIPT" ]] || { printf 'Handler is not executable: %s\n' "$HANDLER_SCRIPT" >&2; exit 1; }
    
    if command -v socat >/dev/null; then
        printf 'Attempting to start socat echo server on 127.0.0.1:%s\n' "$PORT"
        exec socat "TCP-LISTEN:$PORT,bind=127.0.0.1,reuseaddr,fork" "EXEC:'$HANDLER_SCRIPT'"
    elif command -v tcpsvd >/dev/null; then
        printf 'Attempting to start tcpsvd echo server on 127.0.0.1:%s\n' "$PORT"
        exec tcpsvd -l localhost -c 1 -vvE 127.0.0.1 "$PORT" "$HANDLER_SCRIPT"
    fi
    printf 'Install socat to run this simulator.\n' >&2
    exit 1
    
  3. Save this handler as connection_handler.sh in the same directory:

    #!/usr/bin/env bash
    # SPDX-License-Identifier: GPL-2.0-or-later
    # Return each complete input line without changing its contents.
    while IFS= read -r received_text; do
        printf '%s\n' "$received_text"
    done
    
  4. Make both files executable:

    chmod +x tcpserver.bash connection_handler.sh
    

The launcher finds the handler beside itself. It prefers socat, listens on loopback only, and runs a handler for each connection.

Run and test the simulator

  1. In terminal 1, start the server from simulator/:

    ./tcpserver.bash
    

    It must report port 9399. Leave it running.

  2. In terminal 2, connect a client:

    socat - TCP:127.0.0.1:9399
    
  3. Type each line and press Enter:

    First!
    123456
    ByeBye!
    

    The server must return each line unchanged. Press Ctrl+D to close the client.

Verification

Confirm the listener while the server runs:

ss -ltn 'sport = :9399'

Stop the server with Ctrl+C and repeat the command. Its listener must be absent. Leave the server stopped; the next lesson starts it explicitly. Continue with Test IOC communication with the simulator.