它显示Socket异常未处理,并显示没有这样的主机已知 [英] It shows Socket exception was unhandled and displays that no such host is known

查看:73
本文介绍了它显示Socket异常未处理,并显示没有这样的主机已知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public DestCode()

{

String strHostName;



string lip =;

strHostName = Dns.GetHostName();

IPHostEntry local = Dns.GetHostEntry(strHostName);

foreach(local.AddressList中的IPAddress ipaddress)

{

lip + = ipaddress.ToString();

}

IPAddress [] ipAddress = Dns。 GetHostAddresses(lip);

IPEndPoint ipEnd = new IPEndPoint(ipAddress [0],504);

// ipEnd = new IPEndPoint(IPAddress.Any,504);

sock = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.IP);

sock.Bind(ipEnd);

}

public DestCode()
{
String strHostName;

string lip = "";
strHostName = Dns.GetHostName();
IPHostEntry local = Dns.GetHostEntry(strHostName);
foreach (IPAddress ipaddress in local.AddressList)
{
lip += ipaddress.ToString();
}
IPAddress[] ipAddress = Dns.GetHostAddresses(lip);
IPEndPoint ipEnd = new IPEndPoint(ipAddress[0], 504);
//ipEnd = new IPEndPoint(IPAddress.Any, 504);
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
sock.Bind(ipEnd);
}

推荐答案

您的代码正在尝试将主机名解析为地址,然后完全脱离墙。使用调试员。它很容易向你展示问题所在!!!



这部分:

Your code is trying to resolve a host name to addresses and then doing something completely "off the wall". USE THE DEBUGGER. It would have EASILY shown you what the problem is!!!

This part:
strHostName = Dns.GetHostName();
IPHostEntry local = Dns.GetHostEntry(strHostName);



获取您正在运行的计算机的主机名,然后获取Dns拥有的所有IP地址在名字上。



然后你做了一件令人费解的傻事。你浏览地址列表并将它们连接成一个字符串。


gets the hostname of the machine you're running on, then gets all of the IP addresses Dns has on the name.

Then you do something inexplicably stupid. You go through the address list and concatenate them all together into one string.

foreach (IPAddress ipaddress in local.AddressList)
{
    lip += ipaddress.ToString();
}



??????????????????



所以,我的一台机器的地址列表如下:


????????????????????

So, a list of addresses like this for one of my machines:

fe80::41e0:8932:ac26:3a53%10
fe80::1989:c5e:f944:1ec4%19
fe80::c9bf:c154:b0c5:71d7%20
192.168.1.194
192.168.83.1
192.168.183.1"



变量中变为此。


becomes this in your lip variable.

"fe80::41e0:8932:ac26:3a53%10fe80::1989:c5e:f944:1ec4%19fe80::c9bf:c154:b0c5:71d7%20192.168.1.194192.168.83.1192.168.183.1"



然后你尝试将那堆垃圾解析为主机名。显然,这不起作用,因为该字符串代表垃圾,而不是任何有效的地址。



我不知道你为什么试图解决这些地址。这样做真的没有意义。


and then you try and resolve that pile of garbage to a host name. Obviously, that won't work since that string represents garbage, not a valid address for anything.

I have no idea why you're trying to resolve these addresses. There's really no point in doing that.


这篇关于它显示Socket异常未处理,并显示没有这样的主机已知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆