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;
}
}