i have this method running in a thread , but when i test it report all ports as open. it seems that the method : var result = client.BeginConnect(host, port, null, null);
don't working well when passing the results in var success = result.AsyncWaitHandle.WaitOne(tcpTimeout);
...
Any idea how to solve that ?
I have tried client.ConnectAsync(host,port).Wait(TcpTimeout);
but this is not working as expected too ....
public void start()
{
Thread thread1 = new Thread(new ThreadStart(RunScanTcp));
thread1.IsBackground = true;
thread1.Name = "THREAD ME EMER : " + i;
thread1.Priority = System.Threading.ThreadPriority.Highest;
thread1.Start();
}
public void RunScanTcp()
{
while (((port = portList.NextPort()) != -1) && (nderprit != true))
{
TcpClient client = new TcpClient();
count = port;
tcp_count = tcp_count + 1;
Thread.Sleep(10);
try
{
var mre = new ManualResetEvent(false);
Console.WriteLine("Current port count : " + port);
var result = client.BeginConnect(host, port, null, null);
var success = result.AsyncWaitHandle.WaitOne(tcpTimeout);
if (success)
{
Console.WriteLine("PORT IS OPEN : " + port);
received_tcp = received_tcp + 1;
Activity.RunOnUiThread(() =>
{
mre.Set();
});
mre.WaitOne();
client.Close();
}
else
{
client.Close();
}
}
catch (Exception)
{
client.Close();
}
}
}
Determine if the port is open based upon a non-Exception when executing
EndConnect
.Example of a serial port scan:
Note: Use some Linq to break your port list into groups and perform a Parallel.ForEach if you wish to scan multiple ports at the same time (a concurrency of 4 works well and does not overwhelm the Android network stack).