SocketException: address incompatible with requested protocol

前端 未结 2 1192
逝去的感伤
逝去的感伤 2020-12-09 14:58

I was trying to run a .Net socket server code on Win7-64bit machine .
I keep getting the following error:

System.Net.Sockets.SocketException: A

相关标签:
2条回答
  • 2020-12-09 15:00

    On Windows Vista (and Windows 7), Dns.GetHostEntry also returns IPv6 addresses. In your case, the IPv6 address (::1) is first in the list.

    You cannot connect to an IPv6 (InterNetworkV6) address with an IPv4 (InterNetwork) socket.

    Change your code to create the socket to use the address family of the specified IP address:

    Socket serverSocket =
        new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                            ↑
    

    Note: There's a shortcut to obtain the IP address of localhost: You can simply use IPAddress.Loopback (127.0.0.1) or IPAddress.IPv6Loopback (::1).

    0 讨论(0)
  • 2020-12-09 15:25

    Edit C:\Windows\System32\drivers\etc\hosts and add the line "127.0.0.1 localhost" (if its not there, excluding quotes)

    0 讨论(0)
提交回复
热议问题