2

in my program in c#, i need to connect to a website by a specified ip.for this I use ServicePoint.BindIPEndPointDelegate,but it doesn't work. I searched and found this:

when server supports ipv6, we try to establish connection with server using its ipv6 address. But an ipv4 address is specified in the callback. Since the socket has already been created with AddressFamily.InterNetworkv6 when the delegate is called, the bind to an IPv4 address fails. So the delegate is called again. But the same endpoint is returned so the bind fails again. This repeats Integer.MaxValue times and then OverflowException is thrown. Note Integer.MaxValue is a really big value and it looks as though the code has gone to an infinite loop.

now how can I solve this problem? I need to connect with specified ip. the code :

 private void button1_Click(object sender, EventArgs e)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.scopus.com");

        request.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint(BindIPEndPointCallback);

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream data = response.GetResponseStream();
        StreamReader reader = new StreamReader(data);
        var result = reader.ReadToEnd();

        reader.Close();
        response.Close();
        txtResult.Text = result;
    }

    public static IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
    {

        IPAddress address=IPAddress.Parse("77.81.99.66");
        if (retryCount == 5)
            return null;
        return new IPEndPoint(address, 7070);

    }
mansureh
  • 39
  • 7

0 Answers0