IP地址检索

时间:2009-06-26 08:42:42

标签: c# ip-address

执行时此行显示错误:

((IPEndPoint)(TcpClient.Client.RemoteEndPoint)).Address;
eroor是:

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);

3 个答案:

答案 0 :(得分:1)

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

如果您需要更多帮助,则需要发布前面的代码行,以便我们可以看到您要执行的操作。

答案 1 :(得分:0)

你有 TcpClient 的实例吗?

答案 2 :(得分:0)

由于编译器错误声明您需要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);