-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayground.ps1
44 lines (38 loc) · 1.42 KB
/
playground.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
# Define the server address and port
$serverAddress = "127.0.0.1"
$port = 6379
# Define the messages with proper escaping
$messages = @(
"*3`r`n`$3`r`nSET`r`n`$5`r`napple`r`n`$9`r`nblueberry`r`n", # SET command
"*3`r`n`$4`r`nWAIT`r`n`$1`r`n1`r`n`$3`r`n500`r`n" # WAIT command
)
# Create the TCP client and connect
$tcpClient = New-Object System.Net.Sockets.TcpClient
try {
$tcpClient.Connect($serverAddress, $port)
if ($tcpClient.Connected) {
Write-Output "Connected to $serverAddress on port $port"
$stream = $tcpClient.GetStream()
# Loop through each message in the array
foreach ($message in $messages) {
# Convert the message to bytes
$messageBytes = [System.Text.Encoding]::ASCII.GetBytes($message)
# Send the message
Write-Output "Sending message: $message"
$stream.Write($messageBytes, 0, $messageBytes.Length)
# Optional: Read and print the response
$buffer = New-Object byte[] 1024
$bytesRead = $stream.Read($buffer, 0, $buffer.Length)
$response = [System.Text.Encoding]::ASCII.GetString($buffer, 0, $bytesRead)
Write-Output "Response: $response"
}
# Close the stream
$stream.Close()
} else {
Write-Output "Could not connect to the server."
}
} catch {
Write-Output "Error: $_"
} finally {
$tcpClient.Close()
}