Verifying that one system can connect to another system via network is a common task for managing applications. A business application that is not interconnected to some other entity is hard to find.
This is a quick way to test some basic network connections on Windows without much dependencies. All you need is PowerShell and .NET which should already be included on most Windows hosts.
You create a custom
TcpListener
(which is an instance of a .NET class) to open up a port for TCP/IP connections.Then, you connect to this port from a remote system to verify the connection at a basic level.
Reference: https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.tcplistener
Create and start a custom listener (e.g. the server):
PS server> $listener = [System.Net.Sockets.TcpListener]2383
PS server> $listener.Start()
# Verify that ports are opened
PS server> Get-NetTCPConnection -State Listen -LocalPort 2383
LocalAddress LocalPort RemoteAddress RemotePort State AppliedSetting OwningProcess
------------ --------- ------------- ---------- ----- -------------- -------------
0.0.0.0 2383 0.0.0.0 0 Listen 840
Test the connection from a remote system (e.g. the client):
PS client> Test-NetConnection <server> -Port 2383
ComputerName : server
RemoteAddress : 10.0.0.2
RemotePort : 2383
InterfaceAlias : 10.0.0.100
SourceAddress : 10.0.0.100
TcpTestSucceeded : True
Stop the listener when you are done:
PS server> $listener.Stop()
# Verify that ports are closed
PS server> Get-NetTCPConnection -State Listen -LocalPort 2383
Get-NetTCPConnection : No matching MSFT_NetTCPConnection objects found by CIM query for instances of the ROOT/StandardCimv2/MSFT_NetTCPConnection class on
the CIM server: SELECT * FROM MSFT_NetTCPConnection WHERE ((LocalPort = 2383)) AND ((State = 2)). Verify query parameters and retry.
+ CategoryInfo : ObjectNotFound: (MSFT_NetTCPConnection:String) [Get-NetTCPConnection], CimJobException
+ FullyQualifiedErrorId : CmdletizationQuery_NotFound,Get-NetTCPConnection