PowerShell
  • Introduction
  • Chapter1: Write a basic powershell script
    • 1.1.Parameters
  • Chapter2: Remote controlling
    • 2.1.Introduction
    • 2.2.System Requirement
    • 2.3.Environment Setting
    • 2.4.Remote login
    • 2.5.Run remote program
    • 2.6.Remote interaction patterns
  • Chapter3: Some remote interactions for Windows 7\/ 10
    • 3.1.USB throughtput interactions
    • 3.2.mSATA throughtput interactions
    • 3.3.UART-echo interactions
    • 3.4.UART-throughput interactions
    • 3.5.UART-loopback interactions
    • 3.6.Ethernet-IIS interactions
    • 3.7.Ethernet-Wake on LAN interactions
    • 3.8.Ethernet-throughput interactions
    • 3.9.Ethernet-static ip interactions
    • 3.10.Ethernet-VPN interactions
    • 3.11.run once interactions
    • 3.12.Time zone interactions
    • 3.13.Firewall interactions
    • 3.14.Watchdog interactions
    • 3.15.File System interactions
    • 3.16.NTP interactions
    • 3.17.SNMP interactions
    • 3.18.FBWF interactions
    • 3.19.EWF interactions
    • 3.20.UWF interactions
  • Chapter4: Common Type
  • Chapter5: Common Operation
  • Chapter6: Socket Programming
Powered by GitBook
On this page
  • This section contain the following items:
  • 1.TCP server/client

Was this helpful?

Chapter6: Socket Programming

This section contain the following items:

  • 1.TCP server/client

1.TCP server/client

  • 1.TCP server

     Function Receive-TCPMessage { 
        param ( [ValidateNotNullOrEmpty()] 
        [int] $Port
    ) 
    try { 

    $endpoint = new-object System.Net.IPEndPoint([ipaddress]::any,$port) 
    $listener = new-object System.Net.Sockets.TcpListener $EndPoint
    $listener.start() 

    $data = $listener.AcceptTcpClient() # will block here until connection 
    $bytes = New-Object System.Byte[] 1024
    $stream = $data.GetStream() 

    while (($i = $stream.Read($bytes,0,$bytes.Length)) -ne 0){
        $EncodedText = New-Object System.Text.ASCIIEncoding
        $data = $EncodedText.GetString($bytes,0, $i)
        if($data.replace("`n"," ").replace("`r"," ") -eq  "kill  "){
            $ProcessActive = Get-Process uartecho -ErrorAction SilentlyContinue
            echo $ProcessActive
            if($ProcessActive -eq $null)
            {
                return 0
            }
            else
            {
                #do something
            }
        }else {
            return 0
        }

    }

    $stream.close()
    $listener.stop()
}catch [exception]{}
}
  • 2.TCP client

          Function Send-TCPMessage { 
              param ( [ValidateNotNullOrEmpty()] 
                  [string] $EndPoint, 
                  [int] $Port, 
                  $Message
              ) 
    
              $UTF8 = [System.Text.Encoding]::UTF8
              #$IP = [System.Net.Dns]::GetHostAddresses($EndPoint) 
              $Address = [System.Net.IPAddress]::Parse($IP) 
              $Socket = New-Object System.Net.Sockets.TCPClient($Address,$Port) 
              $data = $UTF8.GetBytes($Message)
              $Stream = $Socket.GetStream() 
              $Writer = New-Object System.IO.StreamWriter($Stream)
              $Message | %{
                  $Writer.WriteLine($_)
                  $Writer.Flush()
              }
              $Stream.Close()
              $Socket.Close()
              }
PreviousChapter5: Common Operation

Last updated 5 years ago

Was this helpful?