40834

Why IAsyncResult report all port as opened?

Question:

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

Answer1:

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).

bool portOpen; for (int portNo = 1; portNo < (fasttScan ? 1025 : 65537); portNo++) { TcpClient client = new TcpClient { SendTimeout = (fasttScan ? 2 : 10), ReceiveTimeout = (fasttScan ? 2 : 10) }; var tcpClientASyncResult = client.BeginConnect(ipAddress, portNo, asyncResult => { portOpen = false; try { client.EndConnect(asyncResult); portOpen = true; } catch (SocketException) { } catch (NullReferenceException) { } catch (ObjectDisposedException) { } catch (Exception ex) { Console.WriteLine(ex.Message); // ? unknown socket failure ? } if (portOpen) Console.WriteLine($"{ipAddress}:{portNo}:{portOpen}"); client.Dispose(); client = null; }, null); tcpClientASyncResult.AsyncWaitHandle.WaitOne(); }

Recommend

  • Does System.Timers.Timer terminates on aborting its working Thread?
  • How Invoke Some method inside of none Control Class
  • Get all windows from all threads
  • iOS/Swift: in which function between viewDidLoad and viewWillAppear am I supposed to query a databas
  • Why java statement evaluation is happening like these ?
  • Custom thread pool supporting async actions
  • How does Skobbler prioritize which POIs to show?
  • Log4j cannot find properties file in a simple application
  • Display java JPanel in a JFrame
  • How can I set a binding to a Combox in a UserControl?
  • How to determine if there are bytes available to be read from boost:asio:serial_port
  • Sort List of Strings By Version
  • Problems compiling files using JOGL
  • Parsing a CSV string while ignoring commas inside the individual columns
  • Swing - Get new component under mouseReleased
  • MVC3 Razor - ListBox pre-select not working
  • Set the selected item in dropdownlist in MVC3
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • ilmerge with a PFX file
  • Spring Data JPA custom method causing PropertyReferenceException
  • Can I display google adwords (AdView) in javafx on android
  • Spray.io: When (not) to use non-blocking route handling?
  • Validaiting emails with Net.Mail MailAddress
  • sending/ receiving email in Java
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • Cannot Parse HTML Data Using Android / JSOUP
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • JTable with a ScrollPane misbehaving
  • Android Google Maps API OnLocationChanged only called once
  • How does Linux kernel interrupt the application?
  • unknown Exception android
  • sending mail using smtp is too slow
  • Checking variable from a different class in C#
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?