Created
April 7, 2017 18:05
-
-
Save gjyoung1974/66038a95aa338b8242a62ace2953a68a to your computer and use it in GitHub Desktop.
TCP Port listener & echo server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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