Xamarin forms GET DNS server from WIFI connection - c#

I am using xamarin forms and i have one problem,
i am trying to get the DNS server Adresse from WIFI, and a im using below code :
NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface networkInterface in networkInterfaces)
{
if (networkInterface.OperationalStatus == OperationalStatus.Up)
{
IPInterfaceProperties ipProperties = networkInterface.GetIPProperties();
IPAddressCollection dnsAddresses = ipProperties.DnsAddresses;
foreach (IPAddress dnsAdress in dnsAddresses)
{
Debug.WriteLine(dnsAdress);
}
}
}
The problem is when i use this code on Microsoft Emulator "UWP" everything work fine see picture bellow using UWP device
but nothing work when i use my android mobile see picture bellow
using android device
NB: all devices are connect on the same wifi

I had done a simple to test your code and got the same null. And then I read the IPInterfaceProperties.DnsAddresses Property document, it seems that it doesn't support the xamarin.android.
You can check the it:https://learn.microsoft.com/en-us/dotnet/api/system.net.networkinformation.ipinterfaceproperties.dnsaddresses?view=net-6.0
In addition, I also search this but can not find any solution. So I try to use the android native method to get the dns address with the following code. So you can try to use the dependency service to get the dns address for android.
ConnectivityManager cm = (ConnectivityManager)GetSystemService(ConnectivityService);
//NetworkInfo has been deprecated, but I can not find the new api to to this
NetworkInfo activeNetworkInfo = cm.ActiveNetworkInfo;
foreach (Network network in cm.GetAllNetworks())
{
NetworkInfo networkInfo = cm.GetNetworkInfo(network);
if (networkInfo.GetType() == activeNetworkInfo.GetType())
{
LinkProperties lp = cm.GetLinkProperties(network);
foreach (InetAddress addr in lp.DnsServers)
{
var a = addr.GetAddress();
}
}
}

Related

How to get MAC Address of client?

I am trying to get the MAC address of the client pc but it shows the mac address of the IIS server where my project is hosted.
protected void Page_Load(object sender, EventArgs e)
{
NetworkInterface[] anics = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in anics)
{
if (amacaddress == String.Empty)
{
IPInterfaceProperties properties = adapter.GetIPProperties();
amacaddress = adapter.GetPhysicalAddress().ToString();
lblname.Visible = true;
string ip = Request.UserHostAddress;
lblname.Text = "MAC Address is :- " + amacaddress + " "+ ip;
}
}
}
Yeah. That is similar to asking for getting the IMSI of a phone from a phone call - not possible, you call a phone number, the rest is implementation detail. MAC Addresses pretty much never travel more than one ethernet domain (next switch/router). They are not pat of the IP protocol layer. As such, you can not get them from the http request, which ultimatly is a TCP thus an IP connection. YOu will have to execute (C#, not javascript) code on the client to possibly get the local MAC AddressES - that is plural, there may be more than one (as in: 2 local network cards, a wireless adapter = 3 mac addresses).

How do I determine which network adapter my computer is using?

I am using Pcat.Net. How do I determine which network adapter my computer is using C#, .Net, Pcat.net? GetMacAddress() extension method returns just the mac address of the network adapter, LivePacketDevice.AllLocalMachine also does not return information that can identify if the network adapter is being used by my computer.
You can try using NetworkInterface.GetAllNetworkInterfaces Method. You can get more details from below link:
https://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface.getallnetworkinterfaces(v=vs.110).aspx
I used the following code to determine if my computer was connected to the internet.
var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
var connectedNetworkInterfaces = networkInterfaces.Where(
counter=> counter.OperationalStatus == OperationalStatus.Up &&
(counter.NetworkInterfaceType != NetworkInterfaceType.Loopback || counter.NetworkInterfaceType != NetworkInterfaceType.Tunnel));
if(connectedNetworkInterfaces.Count() <1)
{
throw new Exception("The computer does not seem to be connected to the internet.");
}
var connectedNetworkInterface = connectedNetworkInterfaces.First();

C# Get Network Adapter information from a given adapter

I need to get the IP Address, Subnet mask, gateway, DNS and check if DHCP is enabled from a given adapter.
I have this code that loads my Ethernet and Wireless Adapters:
public void LoadAdapters()
{
if (cmb1_adaptadores.Items.Count < 1)
{
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in adapters)
{
var ipProps = adapter.GetIPProperties();
IPInterfaceProperties properties = adapter.GetIPProperties();
if (adapter.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{
cmb1_adaptadores.Items.Add(adapter.Description);
}
}
}
cmb1_adaptadores.SelectedIndex = 0;
}
I've found many sample codes with foreach and loops to get every adapter ip, dns, etc.
I need an easier thing, but I don’t know how to approach it.
Using the adapter’s description from my combobox, I want to fill some labels with the adapters info, I mean:
If (adapter.description == cmb1_adaptadores.SelectedItem)
{
labelIP1 = adapter...
labelGATEWAY1 = adapter...
}
Of course it will be more complicated than a simple if, but this way every time a new item is selected, this method will run using the description selected in the combobox to reference the adapter.

Find ip of certain windows phone device in your local network

I'm trying to build C# app (desktop,windows, it doesn't really matter for now), with which I would like to connect to my windows phone, using sockets, to transfer some data... I know how this can be achieved,.
When connecting through sockets I don't want to manually enter windows phone device's IP address. So I want to know if it is possible to send some HTTP request from Windows phone app with some message, and fetch that message on computer, to be sure which IP is windows phone's IP?
In other words how to know which IP address belongs to Windows phone's IP address from Bunch of Ip addresses of devices on network?
Fallow this Link from Here
Add NameSpace :
using Windows.Networking;
public static string FindIPAddress()
{
List<string> ipAddresses = new List<string>();
var hostnames = NetworkInformation.GetHostNames();
foreach (var hn in hostnames)
{
//IanaInterfaceType == 71 => Wifi
//IanaInterfaceType == 6 => Ethernet (Emulator)
if (hn.IPInformation != null &&
(hn.IPInformation.NetworkAdapter.IanaInterfaceType == 71
|| hn.IPInformation.NetworkAdapter.IanaInterfaceType == 6))
{
string ipAddress = hn.DisplayName;
ipAddresses.Add(ipAddress);
}
}
if (ipAddresses.Count < 1)
{
return null;
}
else if (ipAddresses.Count == 1)
{
return ipAddresses[0];
}
else
{
//if multiple suitable address were found use the last one
//(regularly the external interface of an emulated device)
return ipAddresses[ipAddresses.Count - 1];
}
}
Edit:
If I am wrong You want to get particular Windows Phone IP address from bunch of IP addresses. AFAIK you can't get like this. But I suggest you to Append some string when getting IP of Windows Phone like below which will vary from other Ip addresses by looking it self.
string IPadd = FindIPAddress();
string WPIPadd = IPadd + " - Windows phone "
MessageDialog.show(WPIPadd);
If you find any thing new apart from this Let us know. Thanks

How to find a list of wireless networks (SSID's) in Java, C#, and/or C?

Is there a toolkit/package that is available that I could use to find a list of wireless networks (SSID's) that are available in either Java, C#, or C for Windows XP+? Any sample code would be appreciated.
For C#, take a look at the Managed Wifi API, which is a wrapper for the Native Wifi API provided with Windows XP SP2 and later.
I have not tested this code, but looking at the Managed Wifi API sample code, this should list the available SSIDs.
WlanClient client = new WlanClient();
foreach ( WlanClient.WlanInterface wlanIface in client.Interfaces )
{
// Lists all available networks
Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList( 0 );
foreach ( Wlan.WlanAvailableNetwork network in networks )
{
Console.WriteLine( "Found network with SSID {0}.", GetStringForSSID(network.dot11Ssid));
}
}
static string GetStringForSSID(Wlan.Dot11Ssid ssid)
{
return Encoding.ASCII.GetString( ssid.SSID, 0, (int) ssid.SSIDLength );
}
ArrayList<String>ssids=new ArrayList<String>();
ArrayList<String>signals=new ArrayList<String>();
ProcessBuilder builder = new ProcessBuilder(
"cmd.exe", "/c", "netsh wlan show all");
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while (true) {
line = r.readLine();
if (line.contains("SSID")||line.contains("Signal")){
if(!line.contains("BSSID"))
if(line.contains("SSID")&&!line.contains("name")&&!line.contains("SSIDs"))
{
line=line.substring(8);
ssids.add(line);
}
if(line.contains("Signal"))
{
line=line.substring(30);
signals.add(line);
}
if(signals.size()==7)
{
break;
}
}
}
for (int i=1;i<ssids.size();i++)
{
System.out.println("SSID name == "+ssids.get(i)+" and its signal == "+signals.get(i) );
}
Well, you didn't specify the OS so, for Linux I will suggest Wireless Tools for Linux by Jean Tourrilhes (http://www.hpl.hp.com/personal/Jean_Tourrilhes/Linux/Tools.html). The iwlist() command displays a lot of information about the available networks. The source code is in C. Another way is to write your own code in C using libpcap for capturing the beacon frames and extracting SSID from them (in monitor mode only). I haven't tested my sniffing code yet so I won't paste it here but it is pretty simple job.

Categories