如何正确使用BindIPEndPointDelegate? [英] How to properly use BindIPEndPointDelegate?

查看:361
本文介绍了如何正确使用BindIPEndPointDelegate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用于BindIPEndPointDelegate的代码:

here's the code I'm using for BindIPEndPointDelegate:

    ServicePoint servicePoint,
    IPEndPoint remoteEndPoint,
    int retryCount)
      {
          if (lastIpEndpoint != null)
          {
              return lastIpEndpoint;
          }
          var candidates =
              GetAddresses(remoteEndPoint.AddressFamily);
          if (candidates == null || candidates.Count() == 0)
          {
              throw new NotImplementedException();
          }

          return
              lastIpEndpoint = new IPEndPoint(candidates[rnd.Next(candidates.Count())], remoteEndPoint.Port);
      };

    static IPAddress[] GetAddresses(AddressFamily af)
    {
        System.Net.IPHostEntry _IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
        return (from i in _IPHostEntry.AddressList where i.AddressFamily == af select i).ToArray();
    }

无论我做什么,我都会收到以下错误:

No matter what I do, I'm getting following error:

System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: The requested address is not valid in its context 173.194.39.68:80
   at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)
   --- End of inner exception stack trace ---
   at System.Net.HttpWebRequest.GetResponse()
   at Webs.Web.SimpleGetResponseString(String uri, String& userAgent, List`1 redirectpath) 
   atWebs.Web.Get(String uri) 
   at test.Program.Main(String[] args)
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

代码有什么问题?

推荐答案

BindIPEndPointDelegate用于选择本地 EndPoint来发送请求,而不是选择远程请求。

BindIPEndPointDelegate is for choosing a local EndPoint for the request to originate from, not for choosing the remote one.

这很有帮助当您需要来自某个IP地址或某个端口范围的请求时。

This is helpful when you need your requests coming from a certain ip address or a certain port range.

这篇关于如何正确使用BindIPEndPointDelegate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆