Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell TCP Server

I would like to ask you, how it is possible to handle multiple connection threads.

I have implemented TCP server in the following way:

$endpoint = New-Object System.Net.IPEndPoint ([System.Net.IPAddress]::Any, 8989)
$listener = New-Object System.Net.Sockets.TcpListener $endpoint
$listener.Start()

do {
    $client = $listener.AcceptTcpClient() # will block here until connection
    $stream = $client.GetStream();
    $reader = New-Object System.IO.StreamReader $stream
    do {
        $line = $reader.ReadLine()
        Write-Host $line -fore cyan
    } while ($line -and $line -ne ([char]4))

    $reader.Dispose()
    $stream.Dispose()
    $client.Dispose()
} while ($line -ne ([char]4))
$listener.Stop()

This code can handle just one thread in time. Can you give me an advice on how to create a TCP server in PowerShell that can handle multiple clients?

like image 325
Krivers Avatar asked Jun 13 '17 10:06

Krivers


People also ask

What is the PowerShell command to view TCP connections?

The Get-NetTCPConnection cmdlet gets current TCP connections. Use this cmdlet to view TCP connection properties such as local or remote IP address, local or remote port, and connection state.

How do I create a TCP server?

On the Program Navigation tree, click PLC Communications>Physical>CPU Ethernet>TCP Servers. Click Add New. Define a port: click the fields to name the server, assign a port, set the Number of Connections, and check Keep Alive.


1 Answers

For handling multiple clients you need multiple threads and for that you need to use runspaces. Below is the working code which accepts multiple clients and do the processing of each client in separate thread (runspace)

$Global:Listener = [HashTable]::Synchronized(@{})
$Global:CnQueue = [System.Collections.Queue]::Synchronized((New-Object System.collections.queue))
$Global:space = [RunSpaceFactory]::CreateRunspace()
$space.Open()
$space.SessionStateProxy.setVariable("CnQueue", $CnQueue)
$space.SessionStateProxy.setVariable("Listener", $Listener)
$Global:newPowerShell = [PowerShell]::Create()
$newPowerShell.Runspace = $space
$Timer = New-Object Timers.Timer
$Timer.Enabled = $true
$Timer.Interval = 1000
Register-ObjectEvent -SourceIdentifier MonitorClientConnection -InputObject $Timer -EventName Elapsed -Action {
    While($CnQueue.count -ne 0) {
        $client = $CnQueue.Dequeue()
        $newRunspace = [RunSpaceFactory]::CreateRunspace()
        $newRunspace.Open()
        $newRunspace.SessionStateProxy.setVariable("client", $client)
        $newPowerShell = [PowerShell]::Create()
        $newPowerShell.Runspace = $newRunspace
        $process = {
            $stream = $client.GetStream();
            $reader = New-Object System.IO.StreamReader $stream
            [console]::WriteLine("Inside Processing")
            # You have client here so do whatever you want to do here.
            # This is a separate thread so if you write blocking code here, it will not impact any other part of the program
        }
        $jobHandle = $newPowerShell.AddScript($process).BeginInvoke()
        #jobHandle you need to save for future to cleanup
    }
}
$listener = {
    $Listener['listener'] = New-Object System.Net.Sockets.TcpListener("127.0.0.1", "1234")
    $Listener['listener'].Start()
    [console]::WriteLine("Listening on :1234")
    while ($true) {
        $c = $Listener['listener'].AcceptTcpClient()
        If($c -ne $Null) {
            [console]::WriteLine("{0} >> Accepted Client " -f (Get - Date).ToString())
            $CnQueue.Enqueue($c)
        }
        Else {
            [console]::WriteLine("Shutting down")
            Break
        }
    }
}
$Timer.Start()
$Global:handle = $newPowerShell.AddScript($listener).BeginInvoke()

For more detailed example please go here

like image 164
Dhirajtech86 Avatar answered Nov 04 '22 11:11

Dhirajtech86