Microsoft.Protocols.TestSuites.MS_OXCNOTIF.S01_ReceivePendingNotifications.GetValidUDPPort C# (CSharp) Method

GetValidUDPPort() private method

Get the valid UDP port on local machine. The default port number is 1025.
private GetValidUDPPort ( ) : int
return int
        private int GetValidUDPPort()
        {
            IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
            IPEndPoint[] udpEndpoints = properties.GetActiveUdpListeners();
            IPEndPoint[] tcpEndpoints = properties.GetActiveTcpListeners();

            int portNumber = int.Parse(Common.GetConfigurationPropertyValue("NotificationPort", this.Site));
            int newPort = portNumber;

            // To check if the portNumber is used by other applications (both UDP and TCP).
            // After the loop, get the valid port number newPort, which will be used in the test case. 
            do
            {
                portNumber = newPort;
                foreach (IPEndPoint point in udpEndpoints)
                {
                    if (point.Port == portNumber)
                    {
                        newPort++;
                        break;
                    }
                }

                if (newPort == portNumber)
                {
                    foreach (IPEndPoint point in tcpEndpoints)
                    {
                        if (point.Port == portNumber)
                        {
                            newPort++;
                            break;
                        }
                    }
                }
            }
            while (newPort != portNumber);

            Site.Log.Add(LogEntryKind.Debug, "The valid UDP port is {0}", newPort);
            return newPort;
        }
    }