【问题标题】:Why IAsyncResult report all port as opened?为什么 IAsyncResult 将所有端口报告为已打开?
【发布时间】:2017-05-22 15:13:51
【问题描述】:

我有这个方法在一个线程中运行,但是当我测试它时报告所有端口都是打开的。似乎方法: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();
            }
        }
}

【问题讨论】:

    标签: c# android xamarin mono xamarin.android


    【解决方案1】:

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

    【讨论】:

    • 谢谢,这是一个很好的例子,我在我的项目中进行了测试,并成功地根据我的需求进行了一些调整......谢谢兄弟
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    • 2016-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    相关资源
    最近更新 更多