Open GoogleCodeExporter opened 9 years ago
The library uses this to determine the local IPv4 address. I have not used Mono
so I don't know any workaround, if you find one please post it here.
Original comment by lidg...@gmail.com
on 19 Dec 2014 at 3:48
I found some solutions on StackOverflow
http://stackoverflow.com/questions/6803073/get-local-ip-address-c-sharp
the last solution works fine (tested on .NET 3.5 on Windows 8 and Mono 2.10 on
Mac & Linux), even with multiple network adapters enabled:
// class NetUtility.cs
/// <summary>
/// Gets my local IPv4 address (not necessarily external) and subnet mask
/// </summary>
public static IPAddress GetMyAddress(out IPAddress mask)
{
mask = null;
#if __ANDROID__
try
{
Android.Net.Wifi.WifiManager wifi = (Android.Net.Wifi.WifiManager)Android.App.Application.Context.GetSystemService(Android.App.Activity.WifiService);
if (!wifi.IsWifiEnabled) return null;
var dhcp = wifi.DhcpInfo;
int addr = dhcp.IpAddress;
byte[] quads = new byte[4];
for (int k = 0; k < 4; k++)
{
quads[k] = (byte) ((addr >> k * 8) & 0xFF);
}
return new IPAddress(quads);
}
catch // Catch Access Denied errors
{
return null;
}
#endif
// acquire local IP address by creating connection and taking it's address
// (which is preferred outbound IP address)
// http://stackoverflow.com/questions/6803073/get-local-ip-address-c-sharp
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0))
{
socket.Connect("10.0.2.4", 65530);
IPEndPoint endPoint = (IPEndPoint)socket.LocalEndPoint;
return endPoint.Address;
}
}
Original comment by vladimir...@gmail.com
on 22 Dec 2014 at 3:50
Original issue reported on code.google.com by
vladimir...@gmail.com
on 19 Dec 2014 at 3:33