【问题标题】:Ip address retrievingIP地址检索
【发布时间】:2009-06-26 08:42:41
【问题描述】:

执行时此行显示错误:

((IPEndPoint)(TcpClient.Client.RemoteEndPoint)).Address;

错误是:

An object reference is required for the nonstatic field, method, or property
System.Net.Sockets.TcpClient.Client.get  ...

这个错误有什么解决办法?

代码如下所示。

//Assume myList is an ArrayList
IPAddress tempAddress = ((IPEndPoint)(TcpClient.Client.RemoteEndPoint)).Address;
myList.Add(tempAddress);

【问题讨论】:

  • 您要检索的 IP 地址是什么? ...本地机器?

标签: c# ip-address


【解决方案1】:

发生错误是因为属性 RemoteEndPoint 是 TCPClient 的实例成员。这意味着您必须先实例化一个 TCPClient(您必须“新建它”)才能访问 RemoteEndPoint。

如果您需要更多帮助,您需要发布前面的代码行,以便我们可以看到您正在尝试做什么。

【讨论】:

  • 我解决了上述问题,但得到了新问题,如图所示。 “对象引用未设置为对象的实例。” 此错误显示在这一行 IPAddress ipAddress = ((IPEndPoint)(tClient.Client.RemoteEndPoint)).Address;
【解决方案2】:

你有 TcpClient 的实例吗?

【讨论】:

    【解决方案3】:

    由于编译器错误状态,您需要一个 IPEndPoint 的实例来访问 Address 属性。

    TcpClient tcpClient = new TcpClient();
    IPAddress ipAddress = Dns.GetHostEntry ("www.contoso.com").AddressList[0];
    IPEndPoint ipEndPoint = new IPEndPoint (ipAddress, 11004);
    IPAddress tempAddress = ipEndPoint.Address;
    myList.Add(tempAddress);
    

    【讨论】:

      猜你喜欢
      • 2013-09-13
      • 1970-01-01
      • 2014-03-04
      • 2010-12-05
      • 1970-01-01
      • 2018-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多