How to Create a Simple TCP Listener (Like Telnet Server) for Raw Socket Testing on Windows/Linux


2 views

While telnet is perfect for connecting to TCP ports, many developers need the reverse functionality - a lightweight way to listen on ports for testing raw TCP connections without running full services. This is invaluable for:

  • Network protocol debugging
  • Testing client applications
  • Verifying firewall rules
  • Quick inter-machine communication

The simplest Linux solution uses netcat (nc):

# Listen on port 12345 (requires netcat-traditional)
nc -l -p 12345

# For BSD-style netcat:
nc -l 12345

For persistent listening with logging:

while true; do 
  nc -l -p 12345 >> connection.log
done

Windows has several native options:

:: PowerShell listener
$listener = [System.Net.Sockets.TcpListener]12345
$listener.Start()
$client = $listener.AcceptTcpClient()
$stream = $client.GetStream()
[byte[]]$bytes = 0..65535|%{0}
while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0){
    $data = [System.Text.Encoding]::ASCII.GetString($bytes,0, $i)
    Write-Host $data
}
$client.Close()

Or using the built-in Telnet server (requires enabling):

dism /online /Enable-Feature /FeatureName:TelnetServer
net start TlntSvr

For maximum portability, this Python script works everywhere:

import socket

HOST = ''  # Listen on all interfaces
PORT = 12345

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen()
    conn, addr = s.accept()
    with conn:
        print(f"Connected by {addr}")
        while True:
            data = conn.recv(1024)
            if not data:
                break
            print(f"Received: {data.decode()}")
            conn.sendall(data)  # Echo back

For multi-connection handling (Linux):

socat TCP-LISTEN:12345,fork,reuseaddr -

Windows persistent service (save as .bat):

@echo off
:start
netcat -l -p 12345
goto start

Remember these critical points when running listeners:

  • Never run as root/administrator
  • Use firewalls to restrict source IPs
  • Consider TLS for sensitive data
  • Close unused ports immediately

For more advanced scenarios:

  • socat - Swiss army knife for sockets
  • ncat - Nmap's enhanced netcat
  • PowerCat - PowerShell netcat alternative

While telnet is excellent for connecting to remote TCP ports, sometimes we need the reverse functionality - creating a simple listener that can accept incoming raw TCP connections without running a full service. This is particularly useful for:

  • Debugging network protocols
  • Creating temporary communication channels
  • Testing firewall rules
  • Educational purposes

Linux Solution: netcat (nc)

The swiss army knife of networking tools:


# To listen on port 12345 (verbose mode)
nc -lvkp 12345

# With IPv6 support
nc -6 -lvkp 12345

Windows Solution: PowerShell

Modern Windows systems can use this PowerShell script:


$port = 12345
$endpoint = New-Object System.Net.IPEndPoint([IPAddress]::Any, $port)
$listener = New-Object System.Net.Sockets.TcpListener($endpoint)
$listener.Start()

Write-Host "Listening on port $port..."

while($true) {
    $client = $listener.AcceptTcpClient()
    $stream = $client.GetStream()
    $reader = New-Object System.IO.StreamReader($stream)
    $writer = New-Object System.IO.StreamWriter($stream)
    
    while($client.Connected) {
        $input = $reader.ReadLine()
        Write-Host "Received: $input"
        $writer.WriteLine("Echo: $input")
        $writer.Flush()
    }
}
Tool Platform Advantages
socat Linux/Unix Extremely flexible, supports SSL
ncat Cross-platform Nmap's enhanced version of netcat
PuTTY Windows Can act as raw socket listener

Persistent Listener with Logging


# Linux/macOS (bash)
while true; do 
    (nc -l -p 12345 | tee -a connection.log) | nc -l -p 12345
done

Bidirectional Communication


# Using socat for full duplex communication
socat TCP-LISTEN:12345,fork EXEC:'/bin/cat'

Windows Command Line Alternative

For older Windows versions without PowerShell:


@echo off
:start
echo Listening on port 12345...
(echo Response from listener) | nc -l -p 12345
goto start

Remember that these simple listeners:

  • Have no authentication
  • Transmit data in clear text
  • May expose your system to attacks

Always use them in controlled environments and close them when not needed.

For frequent use, create a bash function for Linux/macOS:


function tcp_listen() {
    port=${1:-8080}
    echo "Listening on port $port"
    nc -lvkp "$port"
}

Or a PowerShell function for Windows:


function Start-TcpListener {
    param(
        [int]$Port = 12345
    )
    $endpoint = New-Object System.Net.IPEndPoint([IPAddress]::Any, $Port)
    $listener = New-Object System.Net.Sockets.TcpListener($endpoint)
    $listener.Start()
    Write-Host "Listening on port $Port..."
    $client = $listener.AcceptTcpClient()
    # ... rest of the handling code
}