如何避免“SocketException:使用了与请求的协议不兼容的地址。”

时间:2016-11-29 02:41:00

标签: c# .net websocket network-programming websocket-sharp

我在项目中使用websocket-sharp作为网络服务器。在Windows上,我创建了2 HttpServer这样的

HttpServer s1 = new HttpServer("http://[::0]:8080")     // IPv6
HttpServer s2 = new HttpServer("http://0.0.0.0:8080");  // IPv4

这适用于我测试的所有机器。

但是,用户报告从第一行收到此错误

SocketException: An address incompatible with the requested protocol was used.

System.Net.Sockets.Socket..ctor (AddressFamily family, SocketType type, ProtocolType proto)
WebSocketSharp.Net.EndPointListener..ctor (System.Net.IPAddress address, Int32 port, Boolean secure, System.String certificateFolderPath, WebSocketSharp.Net.ServerSslConfiguration sslConfig, Boolean reuseAddress)
WebSocketSharp.Net.EndPointManager.getEndPointListener (System.Net.IPAddress address, Int32 port, Boolean secure, WebSocketSharp.Net.HttpListener listener)
WebSocketSharp.Net.EndPointManager.addPrefix (System.String uriPrefix, WebSocketSharp.Net.HttpListener listener)

为什么?

我们认为它必须与没有IPv6的机器有关但事实并非如此。该计算机上的ipconfig报告

Wireless LAN adapter Wi-Fi 2:

Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Intel(R) Dual Band Wireless-AC 7265 #2
Physical Address. . . . . . . . . : 5C-E0-C5-D7-EA-6A
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Link-local IPv6 Address . . . . . : fe80::48f3:fe4f:7b1:5cf4%19(Preferred)
IPv4 Address. . . . . . . . . . . : 10.198.123.252(Preferred)

最重要的是,在我自己的机器上尝试重新发布问题时,我尝试从网络属性中禁用IPv6。我的机器没有错误。我还尝试使用these scripts from Microsoft禁用IPv6。我已经验证他们关闭了IPv6,ipconfig显示没有与IPv6相关的数据。我的机器上仍然没有错误。

用户提到他们的计算机上有VMware。他们没有在VMware中运行我的应用程序,但VMware在计算机上,因此安装了网络驱动程序。

所以我安装了VMware,但仍然没有错误。还要安装VirtualBox,但仍然没有错误。

他们还提到它可以在他们的有线桌面上工作但不能在他们的wifi笔记本电脑上工作,所以我出去拿了一个USB WiFi适配器并关闭了有线访问,但仍然没有错误。还尝试了另一台笔记本电脑以及Windows 8.1和Windows 10(与用户相同)

我看到this q&a引用了相同的错误消息,但它使用了不同的低级API

所以我的问题是

  1. 为什么它只在一台机器上失败(相对于没有失败的100s)

  2. 如何检测何时执行第一行是安全的?

0 个答案:

没有答案
相关问题