Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 多线程a';原始和#x27;PowerShell服务器_.net_Multithreading_Sockets_Powershell - Fatal编程技术网

.net 多线程a';原始和#x27;PowerShell服务器

.net 多线程a';原始和#x27;PowerShell服务器,.net,multithreading,sockets,powershell,.net,Multithreading,Sockets,Powershell,我正在尝试在PowerShell中创建一个多线程服务器,原因是我希望服务器同时接受来自不同客户端的连接 我假设下面的代码可以工作,但除了它不是我想要的多线程之外,它还有其他的特性。首先,不管我是否在线程上调用Start()方法,它都会以$ClientHandlerCode的方式运行代码 #Functions function send ($writer, $message) { $writer.WriteLine($message) $writer.Flush() } func

我正在尝试在PowerShell中创建一个多线程服务器,原因是我希望服务器同时接受来自不同客户端的连接

我假设下面的代码可以工作,但除了它不是我想要的多线程之外,它还有其他的特性。首先,不管我是否在线程上调用
Start()
方法,它都会以
$ClientHandlerCode
的方式运行代码

#Functions
function send ($writer, $message) {
    $writer.WriteLine($message)
    $writer.Flush()
}

function initialize($writer) {
    $message = "Work in progress!"
    send $writer $message
}

function Parse ($input, $writer){
    send $writer "Don't be so impatient, will you?"
}

$ClientHandlerCode = {
    (param [object]$client)
    function ClientHandler([object]$client)
    {
        $stream = $client.GetStream()
        $reader = New-Object System.IO.StreamReader $stream
        $writer = New-Object System.IO.StreamWriter $stream
        $clienthost = ($client.Client.RemoteEndPoint).ToString().Split(":")[0]
        Write-Host "Client $clienthost connected." -ForegroundColor Cyan
        initialize $writer
        do {
                try{ Parse $reader.ReadLine() $writer } catch {  }
           } while ($client.Connected)
        Write-Host "Client $clienthost disconnected." -ForegroundColor DarkCyan
        $reader.Dispose()
        $stream.Dispose()
        $client.Dispose()
    }
    ClientHandler $client
}

#Main execution
$server = New-Object System.Net.IPEndPoint ([System.Net.IPAddress]::any, 2648)
$listener = New-Object System.Net.Sockets.TcpListener $server
$listener.Start()
Write-Host "Powershell Bulletin Board System started. Listening...`r`n"
do {
        $client = $listener.AcceptTcpClient()
        try {
            $ClientHandler = New-Object System.Threading.Thread((&$ClientHandlerCode $client));
            $ClientHandler.Start() #It doesn't matter if I call this or not.
            } 
        catch {}
    } while (1)
$listener.Stop()
所以对我来说,这归结为两个问题:为什么线程是自己启动的,为什么连接到我的服务器的每个客户端都没有自己的线程


编辑:好吧,把它弄坏了。线程现在根本无法启动。

。您可能想看看像线程这样的工具在PowerShell中无法工作,因为新线程中没有可用的PowerShell引擎来支持它。您最好使用运行空间来完成您想要做的事情。我在PowerShell中创建了一个聊天服务器,其中的代码可能会帮助您:谢谢各位,我将对此进行调查。您可能想看看像线程这样的工具在PowerShell中无法工作,因为新线程中没有可用的PowerShell引擎来支持它。您最好使用运行空间来完成您想要做的事情。我在PowerShell中做了一个聊天服务器,其中的代码可能会帮助您:谢谢各位,我会研究这个。