Last active
August 29, 2015 14:20
-
-
Save pavank/1c16c7253f775270bcb8 to your computer and use it in GitHub Desktop.
Simple TCP Socket listener for testing
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
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