Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save curtishall/70e32a57e99e5a191b40d63ba0c3a551 to your computer and use it in GitHub Desktop.

Select an option

Save curtishall/70e32a57e99e5a191b40d63ba0c3a551 to your computer and use it in GitHub Desktop.
$sw = [System.Diagnostics.Stopwatch]::StartNew()
$client = New-Object System.Net.Sockets.TcpClient
try {
$client.Connect("10.101.240.5", 4518)
Write-Host "Connected at $($sw.ElapsedMilliseconds) ms"
$stream = $client.GetStream()
$stream.ReadTimeout = 15000
$buf = New-Object byte[] 4096
try {
$n = $stream.Read($buf, 0, $buf.Length)
Write-Host "Server sent $n bytes at $($sw.ElapsedMilliseconds) ms"
if ($n -gt 0) { [Text.Encoding]::ASCII.GetString($buf, 0, [Math]::Min($n, 200)) }
} catch {
Write-Host "No data within timeout at $($sw.ElapsedMilliseconds) ms"
}
Start-Sleep -Seconds 5
Write-Host "Still connected at $($sw.ElapsedMilliseconds) ms"
} catch {
Write-Host "Connect failed: $_"
} finally {
$client.Close()
Write-Host "Closed at $($sw.ElapsedMilliseconds) ms"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment