With complex infrastructure, firewalls are often bothersome.
Is it our application or the network?
Powershell TcpListener class can help us.
Here is a sample script opening a specific port and printing incoming data along with connection information.
The Code
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | #region Hardcoded Parameters $iPortToTest = 6666 #endregion #region Internal Parameters $_Listener #endregion #region Internal Function Function Open-TCPPort { [ CmdletBinding ()] Param ( [ Parameter ( Mandatory = $true , Position =0)] [ValidateNotNullOrEmpty()] [int] $Port ) Process { Try { # Start Listener $endpoint = new-object System.Net.IPEndPoint( [ipaddress] ::any, $Port ) $listener = new-object System.Net.Sockets.TcpListener $endpoint $listener .start() Write-Verbose ( "[$(Get-Date)] Listener started on port {0} " -f $Port ) -Verbose Return $listener } Catch { $mess = "Couldn't start listener : " + $Error [0] Write-Error -Message $mess -ErrorAction Stop } } } Function Receive-TCPMessage { [ CmdletBinding ()] Param ( [ Parameter ( Mandatory = $true , Position =0)] [ValidateNotNullOrEmpty()] [System.Net.Sockets.TcpListener] $listener ) Process { Try { # Accept connection $data = $listener .AcceptTcpClient() Write-Verbose ( "[$(Get-Date)] New Connection from {0} Source port <{1}>" -f $data .Client.RemoteEndPoint.Address, $Data .Client.RemoteEndPoint.Port) -Verbose # Stream setup $stream = $data .GetStream() $bytes = New-Object System.Byte[] 1024 # Read Data from stream and write it to host while (( $i = $stream .Read( $bytes ,0, $bytes .Length)) -ne 0){ $EncodedText = New-Object System.Text.ASCIIEncoding $data = $EncodedText .GetString( $bytes ,0, $i ) Write-Output $data } } Catch { $mess = "Receive Message failed with: `n" + $Error [0] Write-Error -Message $mess -ErrorAction Stop } Finally { # Close stream $stream .close() } } } #endregion Internal Function #---------------------------------------------- #region Main Block #---------------------------------------------- $_Listener = Open-TCPPort( $iPortToTest ) try{ while ( $true ) { try{ # Test for pending connection if ( $_Listener .Pending()){ Receive-TCPMessage ( $_Listener ) } Start-Sleep -Milliseconds 50 } catch { $mess = "Receive Message failed with: `n" + $Error [0] Write-Error -Message $mess -ErrorAction Stop } } } finally { $_Listener .stop() } #---------------------------------------------- #endregion Main Block #---------------------------------------------- |
The usage
Set the port you want to listen to in script parameter: $iPortToTest.
Run the script. A message should display the port is now listening.

Connection request will be displayed (machine, source port and incoming data).
A simple test, in local using putty:


