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
Related
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();
}
}
}
How do I get ipv4 of another device connected same wifi network?
I used below code (C# - Unity):
string ipv4 = "";
string hostName = "anp.local";
var host = Dns.GetHostEntry(hostName);
foreach (var ip in host.AddressList)
{
Debug.Log("IP: " + ip.ToString());
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
ipv4 = ip.ToString();
}
}
I got a wrong result: 125.235.4.59 on Window and Android.
Expect result should be 192.168.1.100 (work on Ipad)
My goal is able to connect another device in same wifi network on Window/Android/IOS platforms and I planed using ipv4 for it.
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).
I have the need to know my actual local IP address (i.e. not the loopback address) from a Windows 8 WinRT/Metro app. There are several reasons I need this. The simplest is that in the UI of the app I'd like to show some text like "Your local network IP address is: [IP queried from code]".
We also use the address for some additional network comms. Those comms are perfectly valid because it all works if I look at the IP address in the Control Panel, then hard-code it into the app. Asking the user in a dialog to go look at the address and manually enter it is something I really, really want to avoid.
I would think it wouldn't be a complex task to get the address programmatically, but my search engine and StackOverflow skills are coming up empty.
At this point I'm starting to consider doing a UDP broadcast/listen loop to hear my own request and extract the address from that, but that really seems like a hackey kludge. Is there an API somewhere in the new WinRT stuff that will get me there?
Note that I said "WinRT app. That means the typical mechanisms like Dns.GetHostEntry or NetworkInterface.GetAllInterfaces() aren't going to work.
After much digging, I found the information you need using NetworkInformation and HostName.
NetworkInformation.GetInternetConnectionProfile retrieves the connection profile associated with the internet connection currently used by the local machine.
NetworkInformation.GetHostNames retrieves a list of host names. It's not obvious but this includes IPv4 and IPv6 addresses as strings.
Using this information we can get the IP address of the network adapter connected to the internet like this:
public string CurrentIPAddress()
{
var icp = NetworkInformation.GetInternetConnectionProfile();
if (icp != null && icp.NetworkAdapter != null)
{
var hostname =
NetworkInformation.GetHostNames()
.SingleOrDefault(
hn =>
hn.IPInformation != null && hn.IPInformation.NetworkAdapter != null
&& hn.IPInformation.NetworkAdapter.NetworkAdapterId
== icp.NetworkAdapter.NetworkAdapterId);
if (hostname != null)
{
// the ip address
return hostname.CanonicalName;
}
}
return string.Empty;
}
Note that HostName has properties CanonicalName, DisplayName and RawName, but they all seem to return the same string.
We can also get addresses for multiple adapters with code similar to this:
private IEnumerable<string> GetCurrentIpAddresses()
{
var profiles = NetworkInformation.GetConnectionProfiles().ToList();
// the Internet connection profile doesn't seem to be in the above list
profiles.Add(NetworkInformation.GetInternetConnectionProfile());
IEnumerable<HostName> hostnames =
NetworkInformation.GetHostNames().Where(h =>
h.IPInformation != null &&
h.IPInformation.NetworkAdapter != null).ToList();
return (from h in hostnames
from p in profiles
where h.IPInformation.NetworkAdapter.NetworkAdapterId ==
p.NetworkAdapter.NetworkAdapterId
select string.Format("{0}, {1}", p.ProfileName, h.CanonicalName)).ToList();
}
About the accepted answer, you just need this:
HostName localHostName = NetworkInformation.GetHostNames().FirstOrDefault(h =>
h.IPInformation != null &&
h.IPInformation.NetworkAdapter != null);
You can get the local IP Address this way:
string ipAddress = localHostName.RawName; //XXX.XXX.XXX.XXX
Namespaces used:
using System.Linq;
using Windows.Networking;
using Windows.Networking.Connectivity;
Is there a way to get a list of all SSID's and their mac address of reachable signals in my area?
I tried the Nativ WlanApi in my c# code. What I get is the list of all ssid's, but for
getting their mac address, I don't have any idea.
This is the code I using for getting the list:
private void show_all_ssids_Click(object sender, EventArgs e)
{
WlanClient client = new WlanClient();
foreach ( WlanClient.WlanInterface wlanIface in client.Interfaces )
{
// Lists all available networks
Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList( 0 );
this.ssidList.Text = "";
foreach ( Wlan.WlanAvailableNetwork network in networks )
{
//Trace.WriteLine( GetStringForSSID(network.dot11Ssid));
this.ssidList.Text += GetStringForSSID(network.dot11Ssid) + "\r\n";
}
}
}
static string GetStringForSSID(Wlan.Dot11Ssid ssid)
{
return Encoding.ASCII.GetString(ssid.SSID, 0, (int)ssid.SSIDLength);
}
I hope there is a way.
In order to get a MAC address you would need to connect to that wireless network. Once you are connected you should be able to get the MAC address of machines on the immediate network using the same methods that you might for traditional wired networks - I believe that the best way of doing that would be by parsing the output of the arp -a command.
this is the solution:
Dim networksBss As Wlan.WlanBssEntry() = SelectedWifiAdapter.GetNetworkBssList()
For car = 0 To networksBss(i).dot11Bssid.Length - 1
If Len(Hex(networksBss(i).dot11Bssid(car))) = 1 Then ThisScan(i).MAC = ThisScan(i).MAC & "0"
ThisScan(i).MAC = ThisScan(i).MAC & Hex(networksBss(i).dot11Bssid(car)) & ":"
Next
anyway i'm still looking for a way to find details (strenght) of networks with SSID="" associating it with the proper MAC.