对于ipv6地址,Socket.connect()方法失败

时间:2013-10-12 13:22:21

标签: .net

这是我的代码,我将使用其ipv6地址连接到谷歌,但socket.connect()方法最后是抛出超时异常,我需要一些解决方案,我应该在哪里更改我的代码,< / p>

        Uri google = new Uri("http://www.google.com");

        IPAddress[] ips = Dns.GetHostEntry(google.DnsSafeHost).AddressList;
        IPAddress ipAddress = ips[0];
        foreach (IPAddress ip in ips)
        {
            if (ip.AddressFamily == AddressFamily.InterNetworkV6)
                ipAddress = ip;
        }

        IPEndPoint endPoint = new IPEndPoint(ipAddress, 80);

        Socket socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
        socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
        socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 60 * 1000);
        socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 60 * 1000);
        socket.Connect(endPoint);

1 个答案:

答案 0 :(得分:0)

那么,您确定要连接到有效且在线且能够连接的IP地址吗? 超时并不意味着IPv6存在问题。