SoFunction
Updated on 2025-03-10

PowerShell script development: sending and receiving TCP message packages

In the previous article, we created the Test-TCPPort function in the PSNet package to detect whether the specified port of the specified IP is open. After detecting the port, most people may think of sending and receiving TCP message packets through PowerShell. This article will describe how to create functions Receive- and TCPMessageSend-TCPMessage for TCP message packets in the PSNet package. In order to follow the idea of ​​the PSNet tool set we created in the previous article, after determining the naming of the function, create the .ps1 file corresponding to the above two functions and place it in $env:PSSpace/PSNet/TCPOp/.

Next, add the following two statements in $env:PSSpace/PSNet/PSNet.psm1 to introduce the above two function files in the tool set:

Copy the codeThe code is as follows:

. $env:PSSpace/PSNet/TCPOp/Receive-TCPMessage.ps1
. $env:PSSpace/PSNet/TCPOp/Send-TCPMessage.ps1

Then add the following code to the created .ps1 file:

Copy the codeThe code is as follows:

=====File name: Receive-TCPMessage.ps1=====
Function Receive-TCPMessage
{
    param ( [ValidateNotNullOrEmpty()]
    [int] $Port )
    try
    {
        $EndPoint = New-Object ([]::Loopback,$Port)
        $Socket = New-Object ($EndPoint)
        $()
        $Socket = $()
        $EncodedText = New-Object
        $Stream = $()
        $Buffer = New-Object [] $
        while( $Bytes = $($Buffer,0,$) )
        {
            $($Buffer,0,$Bytes)
            Write-Output $($Buffer,0,$Bytes)
        }
        $()
        $()
    }
    catch{}
}

Copy the codeThe code is as follows:

=====File name: Send-TCPMessage.ps1=====
Function Send-TCPMessage
{
    param ( [ValidateNotNullOrEmpty()]
    [string] $EndPoint,
    [int] $Port,
    [string] $Message )

    $IP = []::GetHostAddresses($EndPoint)
    $Address = []::Parse($IP)
    $Socket = New-Object ($Address,$Port)
    $Stream = $()
    $Writer = New-Object ($Stream)
    $ = $true
    $ = $true
    $($Message)
    $()
}

After saving the code to the corresponding file in the specified directory, start two PowerShell processes and import PSNet Module respectively:

Copy the codeThe code is as follows:

Import-Module $env:PSSpace\PSNet

Either specify parameters when starting PowerShell, or create a batch script to start the PowerShell process that loads the specified Module.

Copy the codeThe code is as follows:

start %windir%\System32\WindowsPowerShell\v1.0\ -noExit -Command "Import-Module ‘%PSSpace%\PSNet' "

First, in one of the PowerShell windows, specify the port and use the Receive-TCPMessage function to listen on the port, waiting for data to be accepted.

Copy the codeThe code is as follows:

Receive-TCPMessage 8080

Send data to the above port in another window:

Copy the codeThe code is as follows:

Send-TCPMessage 127.0.0.1 8080 “This a Message Send from PSNet!”

You will find that after this process sends a message, the previous process can receive the corresponding message. If there are spaces in the message, you need to use double quotes to ensure that the PowerShell interpretation engine knows that this is a complete parameter. Although these two small functions are simple, and the messages in them are sent using ASCII. This is to demonstrate the effects of sending and receiving to everyone. There are still problems in the real actual environment, but these two functions will play a very important role in subsequent functions. Using PowerShell to send TCP and receive TCP messages will be used, and will also be improved in subsequent articles.