一つ上へ
# TCPサーバー作成
$bind_ip = [System.Net.IPAddress]::Parse("127.0.0.1")
$bind_port = 8888
$listener = [System.Net.Sockets.TcpListener]::new($bind_ip, $bind_port)
$listener.Start()
Write-Host "Listening on $bind_ip : $bind_port"
try {
while ($true) {
$client = $listener.AcceptTcpClient()
Write-Host "Client connectrd from $($client.Client.RemoteEndPoint)"
$stream = $client.GetStream()
$buffer = New-Object byte[] 1024
$bytesRead = $stream.Read($buffer, 0, $buffer.Length)
$message = [System.Text.Encoding]::UTF8.GetString($buffer, 0, $bytesRead)
Write-Host "Received: $message"
$response = [System.Text.Encoding]::UTF8.GetBytes("ACK")
$stream.Write($response, 0, $response.Length)
$stream.Close()
$client.Close()
}
} finally {
$listener.Stop()
Write-Host "TCP server stopped"
}