Skip to content

Instantly share code, notes, and snippets.

@ariel-co
Forked from jdgregson/Start-TCPListener.ps1
Created December 24, 2025 01:40
Show Gist options
  • Select an option

  • Save ariel-co/eb56f1dede9184c0bc44919a6b00ae85 to your computer and use it in GitHub Desktop.

Select an option

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.
$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