Created
January 2, 2020 19:04
-
-
Save jdgregson/4a9aa8a57afa7bf41667e57ba60898e2 to your computer and use it in GitHub Desktop.
Simple example of listening on a port and reading from a socket in PowerShell.
This file contains 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
$listener = [System.Net.Sockets.TcpListener]9999 | |
$listener.Start() | |
while ($true) { | |
$client = $listener.AcceptTcpClient() | |
$rEndpoint = $client.client.RemoteEndPoint | |
$data = "" | |
$stream = $client.GetStream() | |
$buffer = New-Object System.Byte[] 1024 | |
while ($client.Connected -and $stream.DataAvailable -and | |
($i = $stream.Read($buffer, 0, $buffer.Length)) -ne 0) { | |
$EncodedText = New-Object System.Text.ASCIIEncoding | |
$data += $EncodedText.GetString($buffer, 0, $i) | |
} | |
Write-Host "$rEndpoint`: $data" | |
$stream.Close() | |
$client.Close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment