简体   繁体   English

使用了与请求的协议不兼容的地址

[英]An address incompatible with the requested protocol was used

I am running network related Windows project in Visual Studio 2012 and Operating System is Windows 7. I am getting the following error: 我在Visual Studio 2012中运行与网络相关的Windows项目,并且操作系统为Windows7。出现以下错误:

An address incompatible with the requested protocol was used 使用了与请求的协议不兼容的地址

My code is: 我的代码是:

public DestCode()
{
   IPHostEntry ipEntry = Dns.GetHostEntry(Environment.MachineName);
   IPAddress IpAddr = ipEntry.AddressList[0];
   ipEnd = new IPEndPoint(IpAddr, 5001);
   sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
   sock.Bind(ipEnd);
}

It's very likely that ipEntry.AddressList[0] is never the right thing to do. ipEntry.AddressList[0]绝对不是正确的选择。 A single machine may have a few IP addresses configured, some of which are auto-configured, and would not normally be used by an application. 一台机器可能配置了几个IP地址,其中一些是自动配置的,通常不会被应用程序使用。 There could be IPv4 addresses, IPv6 addresses, link-scoped addresses, site-scoped addresses, and then somewhere, maybe finally a global-scoped address (one that can be used on the public internet). 可能有IPv4地址,IPv6地址,链接作用域地址,站点作用域地址,然后是某个地方,最后可能是全局作用域地址(可以在公共Internet上使用的地址)。

For instance, this is what my current machine has: 例如,这是我当前的计算机具有的功能:

在此处输入图片说明

You'll note that the list seems to prefer to sort IPv6 addresses to lower indexes. 您会注意到,该列表似乎更喜欢对IPv6地址进行排序以降低索引。 It is very likely that the same is occurring on your machine. 您的计算机上很可能发生了同样的情况。

You create your socket using AddressFamily.InterNetwork , which indicated IPv4, but then you bind with an address that is very likely IPv6: 您可以使用表示为IPv4的AddressFamily.InterNetwork创建套接字,但是随后绑定的地址很可能是IPv6:

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

If you wanted to make the code 'work' with as few changes, then provide the address family of the address you want to bind to when creating the socket, as so: 如果要使代码“工作”时所做的更改很少,那么请提供创建套接字时要绑定到的地址的地址族,如下所示:

new Socket(ipEnd.AddressFamily, SocketType.Stream, ProtocolType.IP)
sock.Bind(ipEnd);

However, using ipEntry.AddressList[0] is probably the wrong thing to do, since you'll never know which address you'll get. 但是,使用ipEntry.AddressList[0]可能是错误的做法,因为您永远不会知道将获得哪个地址。 So how do you get your address? 那么,您如何获得地址? Well, that depends on you. 好吧,这取决于你。 If you want to just loopback for testing or doing some IPC, use 127.0.0.1 ; 如果您只想回送进行测试或进行某些IPC,请使用127.0.0.1 ; if you want to send and receive on any address, bind to 0.0.0.0 , which means the 'any' address in this particular context. 如果要在任何地址上发送和接收,请绑定到0.0.0.0 ,这意味着在此特定上下文中为“任意”地址。 Otherwise, it's typical to take that sort of configuration from the user, especially in the case that the user knows what interface he wants it to listen on. 否则,通常需要从用户那里进行这种配置,尤其是在用户知道他希望其监听的界面的情况下。 In my case, there's no one good interface to pick automatically, and I surely would want you to ask me. 就我而言,没有一个好的界面可以自动选择,我当然希望您问我。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM