forked from jagilber/powershellScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-tcp-listener.ps1
55 lines (45 loc) · 1.47 KB
/
test-tcp-listener.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# powershell test tcp listener for troubleshooting
# $client.Client.Shutdown([net.sockets.socketshutdown]::Both)
# do a final client connect to free up close
param(
[int]$port = 9999,
[int]$count = 0,
[string]$hostName = 'localhost',
[bool]$isClient
)
function main() {
try {
$listener = [net.sockets.tcpListener]$port;
$listener.Start();
$script:server = $listener.AcceptSocket();
while ($iteration -lt $count -or $count -eq 0) {
Write-Host "$(get-date) listening on port $port";
#$server = $listener.AcceptTcpClient();
$server = $listener.AcceptSocket();
$responseString = "$(get-date) client connected on port $port";
$responseString += $server | ConvertTo-Json -Depth 99
$responseString += $listener | ConvertTo-Json -Depth 99
$responseString += "waiting for client send bytes to be sent..."
[byte[]]$sendBytes = [text.encoding]::ASCII.GetBytes($responseString);
$i = $script:server.Send($sendBytes);
Write-Host "$i : $responseString"
[byte[]]$buffer = @(0) * 65536
$server.receive($buffer)
$i = $server.Send($buffer);
Write-Host "$i : $(([text.encoding]::ASCII.GetString($buffer)).Trim())"
$buffer.Clear()
$server.Close();
$iteration++
}
$script:server.Close();
}
finally {
if ($server) {
$server.Dispose();
}
if ($listener) {
$listener.Stop();
}
}
}
main