我是PowerShell编程的新手,我有以下脚本,只要数据可用,就从套接字流中读取,然后发送“q”以退出连接。 我在win 2003服务器std,Power shell V2.0上运行它。 当我执行pgm时,输出是来自pgm的文本,它是读取和缓冲。它几乎看起来像以下是输出:
在23号端口连接到aardwolf.org
while($stream.DataAvailable)
{
$read = $stream.Read($buffer, 0, 1024)
write-Host $read
if($read -gt 0)
{
$outputBuffer += ($encoding.GetString($buffer, 0, $read))
}
}## Read the user's command, quitting if they hit
以下是代码:
# Interact with a service on a remote TCP port
param( [string] $remoteHost = "aardwolf.org",
[int] $port = 23 )
## Open the socket, and connect to the computer on the specified port
write-host "Connecting to $remoteHost on port $port"
$socket = new-object System.Net.Sockets.TcpClient($remoteHost, $port)
if($socket -eq $null) { return; }
$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter($stream)
$buffer = new-object System.Byte[] 1024
$encoding = new-object System.Text.AsciiEncoding
while($true)
{
## Allow data to buffer for a bit
start-sleep -m 500
$outputBuffer = ""
## Read all the data available from the stream, writing it to the ## screen when done.
if($stream.CanRead)
{
while($stream.DataAvailable)
{
$read = $stream.Read($buffer, 0, 1024)
write-Host $read
if($read -gt 0)
{
$outputBuffer += ($encoding.GetString($buffer, 0, $read))
}
}## Read the user's command, quitting if they hit
}
write-Host $outputBUffer
$command = "q"
$writer.WriteLine($command)
$writer.Flush()
break
}
##关闭流
if($ writer -ne $ null)
{
$ writer.Close()
$ stream.Close()
}
需要一些帮助来解决问题。