Find the next TCP port in .NET

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

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

Is there an easy way to find the next open TCP port in .NET?

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

+59
c # networking tcp wcf
Sep 26 '08 at 6:33
source share
6 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; } 
+113
Sep 29 '08 at 22:15
source share

Use port number 0. The TCP stack will allocate the next free one.

+18
Sep 26 '08 at 6:35
source share

First open the port, then specify the correct port number for another process.

Otherwise, it is possible that some other process will open the port first, and you still have another.

+9
Sep 26 '08 at 8:00
source share

This is a decision comparable to TheSeeker's accepted answer. Although I think it is more readable

 using System; using System.Net; using System.Net.Sockets; private static readonly IPEndPoint DefaultLoopbackEndpoint = new IPEndPoint(IPAddress.Loopback, port: 0); public static int GetAvailablePort() { using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { socket.Bind(DefaultLoopbackEndpoint); return ((IPEndPoint)socket.LocalEndPoint).Port; } } 
+5
Mar 21 '18 at 13:55
source share

If you just want to specify the starting port and let it return you the next available TCP port, use code similar to the following:

 public static int GetAvailablePort(int startingPort) { var portArray = new List<int>(); var properties = IPGlobalProperties.GetIPGlobalProperties(); // Ignore active connections var connections = properties.GetActiveTcpConnections(); portArray.AddRange(from n in connections where n.LocalEndPoint.Port >= startingPort select n.LocalEndPoint.Port); // Ignore active tcp listners var endPoints = properties.GetActiveTcpListeners(); portArray.AddRange(from n in endPoints where n.Port >= startingPort select n.Port); // Ignore active UDP listeners endPoints = properties.GetActiveUdpListeners(); portArray.AddRange(from n in endPoints where n.Port >= startingPort select n.Port); portArray.Sort(); for (var i = startingPort; i < UInt16.MaxValue; i++) if (!portArray.Contains(i)) return i; return 0; } 
+4
Jul 29 '17 at 3:37
source share

If you want to get a free port in a certain range, to use it as a local port / endpoint:

 private int GetFreePortInRange(int PortStartIndex, int PortEndIndex) { DevUtils.LogDebugMessage(string.Format("GetFreePortInRange, PortStartIndex: {0} PortEndIndex: {1}", PortStartIndex, PortEndIndex)); try { IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties(); IPEndPoint[] tcpEndPoints = ipGlobalProperties.GetActiveTcpListeners(); List<int> usedServerTCpPorts = tcpEndPoints.Select(p => p.Port).ToList<int>(); IPEndPoint[] udpEndPoints = ipGlobalProperties.GetActiveUdpListeners(); List<int> usedServerUdpPorts = udpEndPoints.Select(p => p.Port).ToList<int>(); TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections(); List<int> usedPorts = tcpConnInfoArray.Where(p=> p.State != TcpState.Closed).Select(p => p.LocalEndPoint.Port).ToList<int>(); usedPorts.AddRange(usedServerTCpPorts.ToArray()); usedPorts.AddRange(usedServerUdpPorts.ToArray()); int unusedPort = 0; for (int port = PortStartIndex; port < PortEndIndex; port++) { if (!usedPorts.Contains(port)) { unusedPort = port; break; } } DevUtils.LogDebugMessage(string.Format("Local unused Port:{0}", unusedPort.ToString())); if (unusedPort == 0) { DevUtils.LogErrorMessage("Out of ports"); throw new ApplicationException("GetFreePortInRange, Out of ports"); } return unusedPort; } catch (Exception ex) { string errorMessage = ex.Message; DevUtils.LogErrorMessage(errorMessage); throw; } } private int GetLocalFreePort() { int hemoStartLocalPort = int.Parse(DBConfig.GetField("Site.Config.hemoStartLocalPort")); int hemoEndLocalPort = int.Parse(DBConfig.GetField("Site.Config.hemoEndLocalPort")); int localPort = GetFreePortInRange(hemoStartLocalPort, hemoEndLocalPort); DevUtils.LogDebugMessage(string.Format("Local Free Port:{0}", localPort.ToString())); return localPort; } public void Connect(string host, int port) { try { // Create socket Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); var localPort = GetLocalFreePort(); // Create an endpoint for the specified IP on any port IPEndPoint bindEndPoint = new IPEndPoint(IPAddress.Any, localPort); // Bind the socket to the endpoint socket.Bind(bindEndPoint); // Connect to host socket.Connect(IPAddress.Parse(host), port); socket.Dispose(); } catch (SocketException ex) { // Get the error message string errorMessage = ex.Message; DevUtils.LogErrorMessage(errorMessage); } } public void Connect2(string host, int port) { try { // Create socket var localPort = GetLocalFreePort(); // Create an endpoint for the specified IP on any port IPEndPoint bindEndPoint = new IPEndPoint(IPAddress.Any, localPort); var client = new TcpClient(bindEndPoint); //client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); //will release port when done // Connect to the host client.Connect(IPAddress.Parse(host), port); client.Close(); } catch (SocketException ex) { // Get the error message string errorMessage = ex.Message; DevUtils.LogErrorMessage(errorMessage); } } 
+1
Aug 16 '16 at 8:20
source share



All Articles