private/Connect-TCPClient.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
Function Connect-TCPClient { <# .SYNOPSIS Connects to a TCP server using the specified server (hostname or ip) and specified port and then returns a TCP client object. .DESCRIPTION Internal function. This function will create a connect to a TCP server on the specified port. The function will return a TCPClient object. .EXAMPLE Connect-TCPClient -Server 'bob' -Port 80 Connect to the TCP Service on server bob, at port 80 .OUTPUTS Returns a System.Net.Sockets.TCPClient #> [CmdletBinding()] [OutputType([System.Net.Sockets.TcpClient])] param ( # Hostname or IP address of the server. [Parameter(Mandatory = $true, HelpMessage = 'Hostname or IP address of server')] [ValidateNotNullOrEmpty()] [String] $Server, # Port of the server (1-65535) [Parameter(Mandatory = $true, HelpMessage = 'Port of the server (1-65535)')] [ValidateNotNullOrEmpty()] [ValidateRange(1, 65535)] [UInt16] $Port ) # Create a TCP client Object Try { $TcpClient = New-Object -TypeName System.Net.Sockets.TcpClient $TcpClient.Connect($Server, $Port) Write-debug -message ('TCP Connection to {0}:{1} established' -f $server, $port) } Catch { Throw $_ } $TcpClient } |