-
-
Save ariel-co/eb56f1dede9184c0bc44919a6b00ae85 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 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
| $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