简体   繁体   中英

how to get the IP of my device and the server IP (windows-CE)

如何在windows-CE中获取我的IP和服务器的IP(使用C#)?

Your question is vague. I don't know which adapter or type of address you want, so I assume you want the IPv4 address of the first local adapter. That would look something like this:

var ip = (from a in Dns.GetHostEntry(Dns.GetHostName()).AddressList
     where a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork
      select a).First().ToString();

Getting the IP address of a given server would require that you know it's hostname and then you'd do a lookup (ie Dns.GetHostEntry ) on that name. It would look a lot like the code above, except you'd use the server host name instead of the Dns.GetHostName() call.

I know this question is old, but I've run into this exact problem recently. Since we already use OpenNETCF, I used this library to query my NICs and their associated addresses. This was on Windows Mobile 6.5 (CE 5.2).

var ifs = OpenNETCF.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
foreach (var nic in ifs)
{
  IPAddress myip = nic.CurrentIpAddress;
}

This will work even if DNS is not configured.

It involves some P/Invoke magic. Because it is a lot of code and what I have is partially proprietary, I can't post all of it, but I can give some hints.

// MSDN: http://msdn.microsoft.com/en-us/library/aa365917(v=vs.85).aspx
[DllImport("iphlpapi.dll", SetLastError = true)]
internal static extern int GetAdaptersInfo(IntPtr pAdapterInfo, ref int pOutBufLen);

[DllImport("iphlpapi.dll", SetLastError = true)]
internal static extern int GetAdapterIndex(string AdapterName, ref int IfIndex);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM