Click to See Complete Forum and Search --> : [RESOLVED] Listen to UDP ports


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.

RaleTheBlade
August 2nd, 2009, 12:29 PM
If something is already listening on those UDP ports they cannot be opened by something else.

revo09
August 2nd, 2009, 01:19 PM
If something is already listening on those UDP ports they cannot be opened by something else.

but i dont need to open the used ones, i jus need to see which ports can be opened within a range, and to do that i will have to open and close them, if they cant be opened the catch event will fire up.


got it solved :D

this is it:


UdpClient listener;

int y = 0;
int startport = 27010;
ArrayList myAL = new ArrayList();
myAL.Clear();


for (y = 1; y <= 50; y++)
{
try
{
listener = new UdpClient(startport + y);
listener.Close();
myAL.Add(startport + y);
}
catch (System.Net.Sockets.SocketException ex)
{

}
}



foreach (int a in myAL)
portStatus.Text = a.ToString() + " OK\r\n";