Ping回复时间太长/不能正常工作

时间:2014-07-15 13:22:32

标签: c# visual-studio-2012 ping

使用Ping与PingReply相关联来检查IP地址的状态及其端口和导入的文本列表如何启动代码以跳过当前的代码并转到下一个?

    PingReply reply = ping.Send("IP", "PORT");

具体地

    PingReply reply = ping.Send("174.69.75.251", "41968");

根本没有回复,它只是冻结了应用程序,因此如果成功,你就无法检查回复状态。


转到代理列表我想检查它们是否有效且能够连接到webBrowser1控件,所以我有以下代码发送IP地址和端口请求以检查它是否会接受联系。


这是循环的全部代码和所有内容,我已经添加了两个人建议的内容,并使用/ * * / heres排除了TCPClient的代码:

private void button2_Click(object sender, EventArgs e)
{
    numberProx = Convert.ToInt32(textBox1.Lines.Length.ToString());
    proxyList = textBox1.Text.Split(new Char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);

    while (i < numberProx)
    {
        string currentProxy = proxyList[i++].ToString();
        try
        {/*
            TcpClient reply2 = new TcpClient();
            reply2.ConnectAsync(currentProxy.Split(':')[0],
                Convert.ToInt32(currentProxy.Split(':')[1]));

            if (reply2.Connected)
            {
                textBox2.AppendText(currentProxy + "\n");
            }
            else
            {
                textBox3.AppendText(currentProxy + "\n");
            }*/

            //PingReply reply = proxy.Send(currentProxy.Split(':')[0], Convert.ToInt32(currentProxy.Split(':')[1]));

            PingReply reply = await proxy.SendPingAsync("174.69.75.251", 5000);
            if (reply.Status == IPStatus.Success)
            {
                textBox2.AppendText(currentProxy + "\n");
            }
            else if (reply.Status == IPStatus.TimedOut)
            {

            }
            else if (reply.RoundtripTime >= 5000)
            {
                textBox3.AppendText(currentProxy + "\n");
            }
            else
            {
                textBox3.AppendText(currentProxy + "\n");
            }
        }
        catch (PingException ex)
        {
            MessageBox.Show("Error: " + ex.Message);
        }
    }
}

这就是包括循环和递增整数在内的所有内容,以匹配名为proxyList的字符串[]中的代理数量。 我想要做的是查看代理是否能够在webBrowser控件中工作,而不会冻结表单/ UI。

2 个答案:

答案 0 :(得分:1)

您可以使用接受Ping.Send的重载来指定超时。这需要在超时之前等待的毫秒数。

如果您在UI应用程序中并且这导致您的UI线程冻结,您可以使用asynchronous方法并等待结果。这将允许您的UI在发送请求时保持响应。

答案 1 :(得分:1)

ping请求无法测试应用程序PORT。为此,你有telnet。

'ping.Send'采用的参数是:

ping.Send('IP_ADDRESS', 'TIMEOUT');

就像在MSDN Documentation

中所说的那样
相关问题