Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the next TCP port in .NET

I want to create a new net.tcp://localhost:x/Service endpoint for a WCF service call, with a dynamically assigned new open TCP port.

I know that TcpClient will assign a new client side port when I open a connection to a given server.

Is there a simple way to find the next open TCP port in .NET?

I need the actual number, so that I can build the string above. 0 does not work, since I need to pass that string to another process, so that I can call back on that new channel.

like image 662
TheSeeker Avatar asked Sep 26 '08 06:09

TheSeeker


People also ask

How do I find my TCP port?

On a Windows computerPress the Windows key + R, then type "cmd.exe" and click OK. Enter "telnet + IP address or hostname + port number" (e.g., telnet www.example.com 1723 or telnet 10.17. xxx. xxx 5000) to run the telnet command in Command Prompt and test the TCP port status.

How check port is open or not in C#?

A port forward on the router cannot be tested from inside the LAN, you need to connect from the WAN (internet) side to see if a port forward is working or not. GRC | ShieldsUP! If you want to check with your own code, then you need to make sure the TCP/IP connection is rerouted via an external proxy or setup a tunnel.

Is TCP a 22 port?

An established TCP connection toward port 22, the SSH default port, is needed to perform the attack. The attacker must have valid credentials to login to the system via SSH or SFTP. The following products have been confirmed to be vulnerable: Cisco ASR 5000/5500/5700 Series devices running StarOS after 17.7.


1 Answers

Here is what I was looking for:

static int FreeTcpPort() {   TcpListener l = new TcpListener(IPAddress.Loopback, 0);   l.Start();   int port = ((IPEndPoint)l.LocalEndpoint).Port;   l.Stop();   return port; } 
like image 105
TheSeeker Avatar answered Sep 22 '22 09:09

TheSeeker