In the last article, we created the Test-tcpport function in the Psnet package to detect whether the specified IP port is open, and most people think of it as having to send and receive TCP message packets via PowerShell. This article will describe how to create functions receive-and tcpmessagesend-tcpmessage for TCP message packs in the Psnet package. To take the idea of the Psnet toolset we created in the previous article, after we have defined the name of the function, we create the corresponding. ps1 file for the two functions to be placed in the $env:psspace/psnet/tcpop/.
Next, add the following two statements to the $ENV:PSSPACE/PSNET/PSNET.PSM1 to introduce the two function files in the toolset:
Copy Code code as follows:
. $env:P Sspace/psnet/tcpop/receive-tcpmessage.ps1
. $env:P Sspace/psnet/tcpop/send-tcpmessage.ps1
The following code is then added to the. ps1 file that is created, respectively:
Copy Code code as follows:
===== FileName: receive-tcpmessage.ps1=====
Function Receive-tcpmessage
{
Param ([Validatenotnullorempty ()]
[INT] $Port)
Try
{
$EndPoint = New-object System.Net.IPEndPoint ([System.net.ipaddress]::loopback, $Port)
$Socket = New-object System.Net.Sockets.TCPListener ($EndPoint)
$Socket. Start ()
$Socket = $Socket. AcceptTcpClient ()
$EncodedText = New-object System.Text.ASCIIEncoding
$Stream = $Socket. GetStream ()
$Buffer = New-object system.byte[] $Socket. receivebuffersize
while ($Bytes = $Stream. Read ($Buffer, 0, $Buffer. Length))
{
$Stream. Write ($Buffer, 0, $Bytes)
Write-output $EncodedText. GetString ($Buffer, 0, $Bytes)
}
$Socket. Close ()
$Socket. Stop ()
}
catch{}
}
Copy Code code as follows:
===== FileName: send-tcpmessage.ps1=====
Function Send-tcpmessage
{
Param ([Validatenotnullorempty ()]
[String] $EndPoint,
[INT] $Port,
[String] $Message)
$IP = [System.net.dns]::gethostaddresses ($EndPoint)
$Address = [System.Net.IPAddress]::P arse ($IP)
$Socket = New-object System.Net.Sockets.TCPClient ($Address, $Port)
$Stream = $Socket. GetStream ()
$Writer = New-object System.IO.StreamWriter ($Stream)
$Writer. AutoFlush = $true
$Writer. NewLine = $true
$Writer. Write ($Message)
$Socket. Close ()
}
After saving the code to the corresponding file in the specified directory, start two PowerShell processes, and import the psnet Module separately:
Copy Code code as follows:
Import-module $env:P sspace\psnet
Either specify parameters when you start PowerShell, or create a batch script with the following statements to start loading the PowerShell process for the specified module.
Copy Code code as follows:
Start%windir%\system32\windowspowershell\v1.0\powershell.exe-noexit-command "Import-module '%PSSpace%\PSNet '"
First in one of the PowerShell windows, the specified port listens with the Receive-tcpmessage function on the development port, waiting for data to be accepted
Copy Code code as follows:
Send data to the above port in another window:
Copy Code code as follows:
Send-tcpmessage 127.0.0.1 8080 "This a message Send from psnet!"
You will find that the process sends a message, and the previous process receives the corresponding message, and if there are spaces in the message that need double quotes, include ensuring that the PowerShell interpretation engine knows this is a complete parameter. These two small functions, though simple in function, and the message is sent in ASCII, this is to show you the effect of sending and receiving, there is still a problem in the real environment, but these two functions will play a very important role in the subsequent functions, Using PowerShell to send TCP and receive TCP messages will be used and will be improved in subsequent articles.