public static int FindOpenPort(ProtocolType type, int start = 30000, bool even = true) { //Only Tcp or Udp :) if (type != ProtocolType.Udp && type != ProtocolType.Tcp) { return(-1); } int port = start; //Get the IpGlobalProperties System.Net.NetworkInformation.IPGlobalProperties ipGlobalProperties = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties(); //Can't get any information if (ipGlobalProperties == null) { return(port = -1); } //We need endpoints to ensure the ports we want are not in use IEnumerable <IPEndPoint> listeners = null; //Get the endpoints if (type == ProtocolType.Udp) { listeners = ipGlobalProperties.GetActiveUdpListeners(); } else if (type == ProtocolType.Tcp) { listeners = ipGlobalProperties.GetActiveTcpListeners(); } //Enumerate the ones that are = or > then port and increase port along the way foreach (IPEndPoint ep in listeners.Where(ep => ep.Port >= port)) { if (port == ep.Port) { port++; } else if (ep.Port == port + 1) { port += 2; } } int remainder = port % 2; //If we only want even ports and we found an even one return it if (even && remainder == 0 || !even && remainder != 0) { return(port); } //We found an even and we wanted odd or vice versa return(++port); }
/// <summary> /// Determine if the specified end point is available. /// </summary> /// <param name="ipEndPoint">The IP end point to check.</param> /// <returns>true if the specified end point is available; otherwise, false.</returns> public static bool IsIPEndPointAvailable(IPEndPoint ipEndPoint) { bool isIPEndPointAvailable = true; System.Net.NetworkInformation.IPGlobalProperties ipGlobalProperties = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties(); IPEndPoint[] activeUdpListeners = ipGlobalProperties.GetActiveUdpListeners(); foreach (IPEndPoint activeUdpListener in activeUdpListeners) { if ((activeUdpListener.Address == ipEndPoint.Address) && (activeUdpListener.Port == ipEndPoint.Port)) { isIPEndPointAvailable = false; break; } } return(isIPEndPointAvailable); }
public static int FindOpenPort(System.Net.Sockets.ProtocolType type, int start = 30000, bool even = true) { //Only Tcp or Udp :) if (type != System.Net.Sockets.ProtocolType.Udp && type != System.Net.Sockets.ProtocolType.Tcp) { return(-1); } int port = start; //Get the IpGlobalProperties System.Net.NetworkInformation.IPGlobalProperties ipGlobalProperties = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties(); //Can't get any information if (ipGlobalProperties == null) { return(port = -1); } //We need endpoints to ensure the ports we want are not in use System.Collections.Generic.IEnumerable <System.Net.IPEndPoint> listeners = System.Linq.Enumerable.Empty <System.Net.IPEndPoint>(); switch (type) { case System.Net.Sockets.ProtocolType.Udp: listeners = ipGlobalProperties.GetActiveUdpListeners(); break; case System.Net.Sockets.ProtocolType.Tcp: listeners = ipGlobalProperties.GetActiveTcpListeners(); break; default: throw new System.NotSupportedException("The given ProtocolType is not supported"); } //Enumerate the ones that are = or > then port and increase port along the way foreach (System.Net.IPEndPoint ep in listeners) { if (ep.Port <= port) { continue; } if (port == ep.Port) { port++; } else if (ep.Port == port + 1) { port += 2; } } //If we only want even ports and we found an even one return it if (even && Binary.IsEven(port) || false == even && Binary.IsOdd(port)) { return(port); } //We found an even and we wanted odd or vice versa return(++port); }
public static int FindOpenPort(System.Net.Sockets.ProtocolType type, int start = 30000, bool even = true, System.Net.IPAddress localIp = null) { //As IP would imply either or Only Tcp or Udp please. if (type != System.Net.Sockets.ProtocolType.Udp && type != System.Net.Sockets.ProtocolType.Tcp) { return(-1); } //Start at the given port number int port = start; //Get the IpGlobalProperties System.Net.NetworkInformation.IPGlobalProperties ipGlobalProperties = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties(); //Can't get any information if (ipGlobalProperties == null) { return(port = -1); } //We need endpoints to ensure the ports we want are not in use System.Collections.Generic.IEnumerable <System.Net.IPEndPoint> listeners;// = System.Linq.Enumerable.Empty<System.Net.IPEndPoint>(); //Try to get the active listeners of the type of protocol specified. try { //Determine if Udp or Tcp listeners are being checked. switch (type) { case System.Net.Sockets.ProtocolType.Udp: listeners = ipGlobalProperties.GetActiveUdpListeners(); break; case System.Net.Sockets.ProtocolType.Tcp: listeners = ipGlobalProperties.GetActiveTcpListeners(); break; default: throw new System.NotSupportedException("The given ProtocolType is not supported"); } } catch (System.NotImplementedException) { //When the method is not implemented then use ProbeForOpenPorts. return(ProbeForOpenPort(type, start, even, localIp)); } //Enumerate the listeners that are == then port and increase port along the way foreach (System.Net.IPEndPoint ep in listeners) { //If the port is less than the port in question continue. if (ep.Port < port) { continue; } //Ensure correctly filtering to the given IP if (localIp != null && ep.Address != localIp) { continue; } if (port == ep.Port) { port++; //Increment the port } else if (ep.Port == port + 1) { port += 2; //Increment by 2, probably not needed. Trying to find a port pair is beyond the scope of this function. } //Only look until the max port is reached. if (port > ushort.MaxValue) { return(-1); } } //If we only want even ports and we found an even one return it if (even && Binary.IsEven(port) || false == even && Binary.IsOdd(port)) { return(port); } //We found an even and we wanted odd or vice versa //Only increase the port if not ushort.MaxValue return(port == ushort.MaxValue ? port : ++port); }