revo09
August 2nd, 2009, 09:59 AM
I am trying to listen to a range of udp ports to see if they can be opened: for example from 27001 to 27005 and if the port can be opened to assign it to an array, in order
Just like this :
let's say that: port 27001, 27002,27005 can be opened, but 27003 and 27004 cannot;
then assign:
ports[0] = 27001;
ports[1] = 27002;
ports[2] = 27005;
For example
UdpClient listener;
int[] ports = new int[100];
for (int i = 27001; i<=27005; i++)
{
try
{
listener = new UdpClient(port_to_listen); // the next lines will not execute if the port cannot open because the catch event will fire
listener.Close();
portStatus.Text += (port_to_listen + " OK\r\n");
}
catch (System.Net.Sockets.SocketException ex)
{
if (ex.ErrorCode == 10048)
portStatus.Text += (port_to_listen + " Not available"+"\r\n");
}
}
what do i need to add here to assign them? or if you know a better way please share.
Just like this :
let's say that: port 27001, 27002,27005 can be opened, but 27003 and 27004 cannot;
then assign:
ports[0] = 27001;
ports[1] = 27002;
ports[2] = 27005;
For example
UdpClient listener;
int[] ports = new int[100];
for (int i = 27001; i<=27005; i++)
{
try
{
listener = new UdpClient(port_to_listen); // the next lines will not execute if the port cannot open because the catch event will fire
listener.Close();
portStatus.Text += (port_to_listen + " OK\r\n");
}
catch (System.Net.Sockets.SocketException ex)
{
if (ex.ErrorCode == 10048)
portStatus.Text += (port_to_listen + " Not available"+"\r\n");
}
}
what do i need to add here to assign them? or if you know a better way please share.