Reputation: 14953
how do i get my IP and the server's IP in windows-CE (using C#)?
Upvotes: 0
Views: 6878
Reputation: 1483
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.
Upvotes: 0
Reputation: 67178
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 (i.e. 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.
Upvotes: 3
Reputation: 2791
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);
Upvotes: 0