Is there an easy way to create a simple Powershell TCP listener? I would
like to create one for tcp port receive testing.
Thanks in advance,
function listen-port ($port) {
$endpoint = new-object System.Net.IPEndPoint ([ipaddress]::any,
$port)
$listener = new-object System.Net.Sockets.TcpListener $ep
$listener.start()
$listener.AcceptTcpClient() # will block here until connection
$listener.stop()
}
ps> listen-port 12345
Now, in another instance of powershell (or cmd.exe):
C:\> telnet localhost 12345
and that's it! the function will spit out connection info and return
control to you.
hope this helps,
- Oisin
PowerShell MVP
http://www.nivot.org/
oops,
$listener = new-object System.Net.Sockets.TcpListener $ep
should be
$listener = new-object System.Net.Sockets.TcpListener $endpoint
sorry!
- Oisin
Unable to find type [ipaddress]: make sure that the assembly containing this
type is loaded.
At line:2 char:60
+ $endpoint = new-object System.Net.IPEndPoint ([ipaddress]: <<<<
:any,$port)
New-Object : Constructor not found. Cannot find an appropriate constructor
for type System.Net.Sockets.TcpListener.
At line:3 char:25
+ $listener = new-object <<<< System.Net.Sockets.TcpListener $endpoint
You cannot call a method on a null-valued expression.
At line:4 char:18
+ $listener.start( <<<< )
You cannot call a method on a null-valued expression.
At line:5 char:28
+ $listener.AcceptTcpClient( <<<< ) # will block here until connection
You cannot call a method on a null-valued expression.
At line:6 char:17
+ $listener.stop( <<<< )
It should be
function listen-port ($port)
{
$endpoint = new-object System.Net.IPEndPoint
[system.net.ipaddress]::any, $port)
$listener = new-object System.Net.Sockets.TcpListener $endpoint
$listener.start()
$listener.AcceptTcpClient() # will block here until connection
$listener.stop()
}
Steven MurawskiC
The function should be:
function listen-port ($port) {
$endpoint = new-object System.Net.IPEndPoint
([system.net.ipaddress]::any, $port)
$listener = new-object System.Net.Sockets.TcpListener $endpoint
$listener.start()
$listener.AcceptTcpClient() # will block here until connection
$listener.stop()
}
Steven Murawski
Host - Powershell Basics
http://powershell-basics.com
> > - Oisin- Hide quoted text -
>
> - Show quoted text -
They have a new beat available for testing
http://www.nsoftware.com/powershell/netcmdlets/v2.aspx
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk
Thanks Steven,
I'm using PowerShell v2, and it appears to import System.Net so for
me, [ipaddress] works. :)
- Oisin
Having thought about this a bit more, it seems this might be a bit of
a needless breaking change in v2. It's a pretty insidious kind of
break too. While I find the import really useful in terms of saving
the extra typing, it's easy enough to avoid the newer cmdlets in order
to keep scripts backwards compatible, but this kind of thing is
extremely difficult to weed out.
Thoughts, anyone?
- Oisin
Hi Kiron - I'm not using the same v2 as you - it's an internal build
(mvp privelege). It looks like this feature was added after the first
public CTP.
- Oisin
[C:\]: listen-port 12345
New-Object : Constructor not found. Cannot find an appropriate constructor
for type System.Net.IPEndPoint.
At line:2 char:26
+ $endpoint = new-object <<<< System.Net.IPEndPoint
IPAddressToString : 0.0.0.0
Address : 0
AddressFamily : InterNetwork
ScopeId :
IsIPv6Multicast : False
IsIPv6LinkLocal : False
IsIPv6SiteLocal : False
12345
New-Object : Constructor not found. Cannot find an appropriate constructor
for type System.Net.Sockets.TcpListener.
At line:4 char:26
+ $listener = new-object <<<< System.Net.Sockets.TcpListener $endpoint
You cannot call a method on a null-valued expression.
At line:5 char:19
+ $listener.start( <<<< )
You cannot call a method on a null-valued expression.
At line:6 char:29
+ $listener.AcceptTcpClient( <<<< ) # will block here until connection
You cannot call a method on a null-valued expression.
At line:7 char:18
+ $listener.stop( <<<< )
I get the same error with the function if I do not enclose the
parameters for the $endpoint variable in parenthesis like below:
$endpoint = New-Object ([System.Net.IPEndPoint]::any, $port)
Steven Murawski
Host - PowerShell Basics
http://www.powershell-basics.com
The problem is that the line is getting wrapped - it should be:
$endpoint = new-object System.Net.IPEndPoint `
([system.net.ipaddress]::any, $port)
I just placed a back-tick line continuation character above there.
- Oisin