Why IAsyncResult report all port as opened?(为什么 IAsyncResult 将所有端口报告为已打开?)
问题描述
我有这个方法在一个线程中运行,但是当我测试它时报告所有端口都是打开的.似乎方法: var result = client.BeginConnect(host, port, null, null); 在 var success = result.AsyncWaitHandle.WaitOne 中传递结果时效果不佳(tcpTimeout); ... 
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 ?
我已经尝试过 client.ConnectAsync(host,port).Wait(TcpTimeout); 但这也没有按预期工作......
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();
            }
        }
}
推荐答案
执行EndConnect时根据非异常判断端口是否打开.
Determine if the port is open based upon a non-Exception when executing EndConnect.
串口扫描示例:
注意:如果您希望同时扫描多个端口,请使用一些 Linq 将您的端口列表分成组并执行 Parallel.ForEach(4 个并发效果很好,并且不会压倒 Android 网络堆栈).
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();
}
                        这篇关于为什么 IAsyncResult 将所有端口报告为已打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么 IAsyncResult 将所有端口报告为已打开?
				
        
 
            
        - 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
 - 如何用自己压缩一个 IEnumerable 2022-01-01
 - C#MongoDB使用Builders查找派生对象 2022-09-04
 - Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
 - 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
 - 输入按键事件处理程序 2022-01-01
 - 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
 - MoreLinq maxBy vs LINQ max + where 2022-01-01
 - C# 中多线程网络服务器的模式 2022-01-01
 - WebMatrix WebSecurity PasswordSalt 2022-01-01
 
						
						
						
						
						
				
				
				
				