Skip to content

Instantly share code, notes, and snippets.

@pavank
Last active August 29, 2015 14:20
Show Gist options
  • Save pavank/1c16c7253f775270bcb8 to your computer and use it in GitHub Desktop.
Save pavank/1c16c7253f775270bcb8 to your computer and use it in GitHub Desktop.
Simple TCP Socket listener for testing
function TCPSocketListener ([string]$IPAddress , [int]$Port ,[int] $MinstoListen)
{
Write-Host ("Starting TCP Socket Listener on $IPAddress..")
try {
$listener = new-object System.Net.Sockets.TcpListener([System.Net.IPAddress]::Parse($IPAddress), $Port)
$listener.start()
$StartTime = Get-Date
While($true)
{
$CurrentTime = Get-Date
Write-Host ("Waiting for connection on port:$Port .....")
If(($CurrentTime-$StartTime).Minutes -gt $MinstoListen)
{
Write-Host("Time Elapsed.Stopping Socket..")
break
}
Start-Sleep -Seconds 1
}
}
Catch [System.Exception]
{
Write-Host ("Exception occured")
}
Finally
{
$listener.Stop()
write-host "Connection closed."
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment