ユーザ用ツール

サイト用ツール


powershell:tcpserver

文書の過去の版を表示しています。


TCPサーバー

一つ上へ

# TCPサーバー作成
 
$bind_ip = [System.Net.IPAddress]::Parse("192.168.0.19")
$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"
}
powershell/tcpserver.1763505185.txt.gz · 最終更新: 2025/11/19 07:33 by mikoto