How to Monitor Incoming TCP Connections with Socat: Verbose Debugging Techniques


2 views

Many developers encounter this frustrating scenario: you've set up socat with maximum verbosity (-vvvv), yet it fails to display incoming TCP connection events. The fundamental issue lies in socat's default logging behavior - it focuses on data transfer rather than connection state changes.

Here are three proven approaches to monitor incoming connections:

# Solution 1: Using fork with system logging
socat -d -d -d -lu tcp-l:9000,fork exec:'/bin/bash -c "echo \'Connection from $SOCAT_PEERADDR\'"'

# Solution 2: Combined with netstat monitoring
while true; do
  netstat -ant | grep 9000
  sleep 1
done &
socat tcp-l:9000,fork -

# Solution 3: Using SYSTEM address with logger
socat tcp-l:9000,fork SYSTEM:'echo "Connection from $SOCAT_PEERADDR" >> /var/log/socat_connections.log'

The -lu (log unbuffered) option combined with -d flags provides more immediate output, but still won't show pure connection events. For full connection visibility, you need to combine socat with other tools:

# Advanced monitoring with strace
strace -e trace=network -f socat tcp-l:9000,fork -

# Or using tcpdump in parallel
tcpdump -i lo -nn port 9000 &
socat tcp-l:9000,fork -

For production environments, consider this comprehensive solution that logs both connections and data:

#!/bin/bash
LOG_FILE="/var/log/socat_$(date +%s).log"
{
  echo "Starting socat monitor at $(date)"
  socat -lu -d -d -d \
    tcp-l:9000,reuseaddr,fork \
    system:'echo "INCOMING: $SOCAT_PEERADDR:$SOCAT_PEERPORT" >> '$LOG_FILE' \
    && cat >> '$LOG_FILE
} 2>&1 | tee -a $LOG_FILE

When socat's logging limitations become restrictive, these alternatives offer better connection visibility:

  • nc (netcat) with -v or -vv flags
  • socat forks like spocat with enhanced logging
  • Custom solutions using Python's socket or asyncio modules

You've set up socat to forward traffic between ports 9000 and 9001 with maximum verbosity (-vvvv), but you're not seeing connection establishment messages in the output. This is a common pain point when debugging network applications.

socat's default logging focuses on data transfer rather than connection events. The verbosity flags (-v) primarily control how much data is shown, not connection state changes.

To monitor incoming connections, use the -lf (log file) option combined with timestamping:

socat -d -d -lf /var/log/socat_connections.log \
  tcp4-listen:9000,reuseaddr,fork \
  tcp4-listen:9001,reuseaddr

This will log connection events like:

2023/11/15 14:23:18 socat[1234] N listening on AF=2 0.0.0.0:9000
2023/11/15 14:23:22 socat[1234] N accepting connection from AF=2 192.168.1.100:54321 on AF=2 192.168.1.5:9000

When socat's native logging isn't sufficient, combine it with system monitoring:

# Monitor connections in real-time
socat tcp4-listen:9000,fork tcp4:localhost:9001 &
watch -n 1 'ss -tulp | grep 9000'

For development environments, this combination provides maximum visibility:

socat -d -d -d -t 5 -lu /dev/stdout \
  tcp4-listen:9000,reuseaddr,fork \
  tcp4:localhost:9001

Key options:
- -d -d -d: Triple debug level
- -t 5: Timeout after 5 seconds of inactivity
- -lu: Log to stdout with microsecond timestamps

For production systems, integrate with syslog:

socat -d -d -ls -lf /dev/stdout \
  tcp4-listen:9000,reuseaddr,fork,syslog=local0.info \
  tcp4:localhost:9001

Configure your syslog daemon (rsyslog/syslog-ng) to handle these messages separately.