TCP客户端连接冻结程序

时间:2015-02-25 21:10:45

标签: c# .net asynchronous tcpclient freeze

我想在ip +端口上有异步连续ping请求。 问题是,当目标服务器脱机时,整个程序在请求时间内冻结。但这不应该发生。

这是我的ping函数,在启动程序时调用一次。 它应该每3秒执行一次ping请求。

    public async void Start(string ip)
    {
        Stopwatch watch = new Stopwatch();

        while (true)
        {
            watch.Restart();
            using (TcpClient tcp = new TcpClient())
            {
                //try to connect to a closed port
                IAsyncResult res = tcp.ConnectAsync("127.0.0.1", 1234);

                //during the following while-loop the whole program freezes
                //although this is an asynchronous function
                while (watch.ElapsedMilliseconds < 1000) 
                {
                    if (tcp.Connected)
                    {
                        break;
                    }
                }
                watch.Stop();
                if (tcp.Connected)
                {
                    StatusControl.Text = watch.ElapsedMilliseconds.ToString() + " ms";
                }
                else
                {
                    StatusControl.Text = "Offline";
                }
            }
            //wait 3secs for the next request
            await Task.Delay(3000);
        }
    }

2 个答案:

答案 0 :(得分:1)

如果您希望应用程序保持响应而不挂起,则无法构建自己的循环并保持线程占用。相反,让C#使用await关键字为您管理任务。不要循环和检查超时,只需使用SendTimeout在请求上设置超时。这是一个例子:

    public async void Start(string ip)
    {
        label1.Text = "Begin";
        Stopwatch watch = new Stopwatch();

        while (true)
        {
            watch.Restart();
            using (TcpClient tcp = new TcpClient())
            {
                //try to connect to a closed port
                // First set a timeout value
                tcp.SendTimeout = 1000;
                try
                {
                    await tcp.ConnectAsync("127.0.0.1", 1234);
                }
                catch (SocketException)
                {
                    Debug.Assert(!tcp.Connected);
                }

                watch.Stop();
                if (tcp.Connected)
                {
                    label1.Text = watch.ElapsedMilliseconds.ToString() + " ms";
                }
                else
                {
                    label1.Text = "Offline";
                }
            }
            //wait 3secs for the next request
            await Task.Delay(3000);
        }
    }
}

答案 1 :(得分:0)

如果你想对端口发出异步ping请求以及超时,这段代码就是为我做的:

    private async Task<long> Ping(string host, int port, int timeOut)
    {
        long elapsed = -1;
        Stopwatch watch = new Stopwatch();

        using (TcpClient tcp = new TcpClient())
        {
            try
            {
                using (CancellationTokenSource cts = new CancellationTokenSource())
                {
                    StartConnection(host, port, tcp, watch, cts);
                    await Task.Delay(timeOut, cts.Token);
                }
            }
            catch {}
            finally
            {
                if (tcp.Connected)
                {
                    tcp.GetStream().Close();
                    elapsed = watch.ElapsedMilliseconds;
                }
                tcp.Close();
            }
        }

        return elapsed;
    }

    private async void StartConnection(string host, int port, TcpClient tcp, Stopwatch watch, CancellationTokenSource cts)
    {
        try
        {
            watch.Start();
            await tcp.ConnectAsync(host,port);
            watch.Stop();
            cts.Cancel();
        }
        catch {}
    }

它只是在时间结束后立即处理tcpClient,并在服务器脱机或出现问题时返回-1。