Skip to content

Instantly share code, notes, and snippets.

@gjyoung1974
Created April 7, 2017 18:05
Show Gist options
  • Save gjyoung1974/66038a95aa338b8242a62ace2953a68a to your computer and use it in GitHub Desktop.
Save gjyoung1974/66038a95aa338b8242a62ace2953a68a to your computer and use it in GitHub Desktop.
TCP Port listener & echo server
# A stupid TCP Listener
# telnet or netcat to the port
# type something and hit enter
# the typed bytes are echoed by this script
function listen-port ($port) {
$endpoint = new-object System.Net.IPEndPoint ([system.net.ipaddress]::any, $port)
$listener = new-object System.Net.Sockets.TcpListener $endpoint
$listener.start()
do {
$client = $listener.AcceptTcpClient() # will block here until connection
$stream = $client.GetStream();
$reader = New-Object System.IO.StreamReader $stream
do {
$line = $reader.ReadLine()
write-host $line -fore cyan
} while ($line -and $line -ne ([char]4))
$reader.Dispose()
$stream.Dispose()
$client.Dispose()
} while ($line -ne ([char]4))
$listener.stop()
}
listen-port(8989)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment