我在线程中运行此方法,但是当我对其进行测试时,将所有端口报告为开放.似乎该方法:var result = clIEnt.BeginConnect(host,port,null,null);在var success = result.AsyncWaitHandle.WaitOne(tcpTimeout)中传递结果时,效果不佳…
知道如何解决吗?
我已经尝试过clIEnt.ConnectAsync(host,port).Wait(TcpTimeout);但这也无法按预期工作….
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(); } }}
解决方法:
执行EndConnect时,根据非异常确定端口是否打开.
串行端口扫描的示例:
注意:如果您希望同时扫描多个端口,请使用一些linq将您的端口列表分成几组并执行Parallel.ForEach(并发4效果很好,并且不会淹没AndroID网络堆栈).
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();}
总结 以上是内存溢出为你收集整理的为什么IAsyncResult报告所有端口为打开状态?全部内容,希望文章能够帮你解决为什么IAsyncResult报告所有端口为打开状态?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)