简体   繁体   English

从catch System.Net.Sockets.SocketException返回字符串

[英]Return string from catch System.Net.Sockets.SocketException

I have a button that should convert a list of hostnames to the matching IP addresses in a textbox. 我有一个按钮,应该将主机名列表转换为文本框中的匹配IP地址。 How can I just return string "No host is known" for the hostname that is really not known to the textbox? 我如何才能为文本框真正不知道的主机名返回字符串“未知主机”?

I use try-catch block in which I catch System.Net.Sockets.SocketException . 我使用try-catch块来捕获System.Net.Sockets.SocketException But as far that I know, catch block cannot return any string value. 但是据我所知,catch块不能返回任何字符串值。 So, I usually catch the exception by output a messagebox. 因此,我通常通过输出消息框来捕获异常。 But this time, I just want it to display a string in the specified textbox. 但是这次,我只希望它在指定的文本框中显示一个字符串。 This is the code that I've tried:- 这是我尝试过的代码:

private void btnConvertHosttoIP_Click(object sender, Eventrgs e)
{
    try
    {
        string ips = null;
        List<string> ipList = new List<string>();
        string[] hostList = Regex.Split(txtHost.Text, "\r\n");
        foreach (var h in hostList)
        {
            // Check DNS
            if (h.Contains(".xxxx.com"))
            {
                hostName = h;
            }
            else
            {
                string code = txtHost.Text.Substring(0, 3);
                if (code == "ABC" || code == "CDE")
                    hostName = h + ".ap.xxx.com";
                else
                    hostName = "Unknown domain name";
            }

            IPHostEntry host = Dns.GetHostEntry(hostName);
            IPAddress[] ipaddr = host.AddressList;

            // Loop through the IP Address array and add the IP address to Listbox
            foreach (IPAddress addr in ipaddr)
            {
                if (addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                {
                    ipList.Add(addr.ToString());
                }
            }
        }
        foreach (var ip in ipList)
        {
            ips += ip + Environment.NewLine;
        }
        txtIP.Text = ips;
    }
    catch (System.Net.Sockets.SocketException ex)
    {
        MessageBox.Show(ex.Message);
    }
}

I want the unknown host to be displayed in the textbox only not as an exception. 我希望未知主机仅作为例外显示在文本框中。 Is it possible or any other suggestions? 有可能或其他建议吗?

You can modify your code as below. 您可以按以下方式修改代码。

try
{
    string ips = null;
    List<string> ipList = new List<string>();
    string[] hostList = Regex.Split(txtHost.Text, "\r\n");
    foreach (var h in hostList)
    {
        // Check DNS
        if (h.Contains(".xxxx.com"))
        {
            hostName = h;
        }
        else
        {
            string code = txtHost.Text.Substring(0, 3);
            if (code == "ABC" || code == "CDE")
                hostName = h + ".ap.xxx.com";
            else
                hostName = "Unknown domain name";
        }
        try
       {
        IPHostEntry host = Dns.GetHostEntry(hostName);
        IPAddress[] ipaddr = host.AddressList;

        // Loop through the IP Address array and add the IP address to Listbox
        foreach (IPAddress addr in ipaddr)
        {
            if (addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
            {
                ipList.Add(addr.ToString());
            }
        }
      } 
      catch (System.Net.Sockets.SocketException ex)
     {
         ipList.Add("Invalid Host");
     }
    }
    foreach (var ip in ipList)
    {
        ips += ip + Environment.NewLine;
    }
    txtIP.Text = ips;
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Net.Sockets.SocketException - System.Net.Sockets.SocketException 来自此System.Net.Sockets.SocketException的源或目标的IP? - Is the IP from the source or target in this System.Net.Sockets.SocketException? postgres 连接上的 System.Net.Sockets.SocketException - System.Net.Sockets.SocketException on postgres connection System.Net.Sockets.SocketException 灾难 - System.Net.Sockets.SocketException Disaster .NET服务System.Net.Sockets.SocketException:&#39;未知此类主机&#39; - .NET Service System.Net.Sockets.SocketException: 'No such host is known' System.Net.Sockets.SocketException (0x80004005): 没有这样的主机是已知的 - System.Net.Sockets.SocketException (0x80004005): No such host is known 2008R2服务器上的System.Net.Sockets.SocketException - System.Net.Sockets.SocketException on 2008R2 Server EventHub 发送消息,但 Application Insights 上的 System.Net.Sockets.SocketException - EventHub send message, but System.Net.Sockets.SocketException on Application Insights 使用Azure时Visual Studio中的System.Net.Sockets.SocketException - System.Net.Sockets.SocketException in Visual Studio while using Azure System.Net.Sockets.SocketException:&#39;提供了无效的参数&#39; - System.Net.Sockets.SocketException: 'An invalid argument was supplied'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM