We have a requirement to get the printer IP Address configured in the default printer driver in Control Panel in our UWP app.
I was able to retrieve the "System.DeviceInterface.PrinterPortName" by fetching interface class GUID and passing this above property for retrieval.
But I couldn't get "System.Devices.IpAddress" similarly.
Code pasted below for PortName.
I badly need the IP address as the port name is user's choice and could be modified to any name removing the IP address.
Kindly help sharing working code to retrieve the IP Address using above property or any other way in UWP app.
Below is Working Code for Port Name, Kindly help to fetch IP Address of the same port similarly.
string aqsFilter = "System.Devices.InterfaceClassGuid:=\"{0ecef634-6ef0-472a-8085-5ad023ecbccd}\"";
string[] propertiesToRetrieve = new string[] { "System.DeviceInterface.PrinterPortName"};
DeviceInformationCollection deviceInfoCollection = await DeviceInformation.FindAllAsync(aqsFilter, propertiesToRetrieve);
foreach (DeviceInformation deviceInfo in deviceInfoCollection)
{
if (deviceInfo.IsDefault == true)
{
string strPortName = (string)deviceInfo.Properties["System.DeviceInterface.PrinterPortName"];
if (!string.IsNullOrEmpty(strPortName))
{
strPortName = await ParsePortName(strPortName);
if (!string.IsNullOrEmpty(strPortName))
{
_strIPAddress = strPortName;
}
}
break;
}
}
This is not endorsed because the IP address can change and so it is unreliable.
That being said, if your printer is installed using wsd, it is technically supported
E.g.,
DEVPKEY_PNPX_IpAddress DEVPROP_TYPE_STRING_LIST 32 "10.137.192.202"
But there is no way to reliably use this without a lot of various scenario checks since the IP address may change.
Furthermore, looking at this example, you are not hitting the DAF providers but looking for devices. You are using 0ecef634-6ef0-472a-8085-5ad023ecbccd which is the printer class guid. It also does not look like IP address is propagated in the PnP Explorer property bag so the IP address is not accessible.
Related
Is there a way to uniquely identify a user that has logged on my application based on his device/machine ?
In a lot of cases, the IP is enough, but in case when the client has multiple machines behind a NAT, then the same IP is exposed, so I can't tell them apart. it should have same id irrespective of browser.
for e.g. If the user logs in on his account with computer A, then log in on the same account with computer B that share the same router, I need to get id apart those two devices.
I don't know if this is possible, but it would be life saving if it is.
I was faced with this problem where I wanted to ask for Google Authenticator on sign in but only once for each device used by a user. I used a function to give me a device id based on the hostname, the MAC address, and the IP address. I know the MAC address isn't always reliable so I thought combining the data into one string might be a way round it. Our application is limited to <100 users and most of them access from the office or home so I feel it should be good enough.
I put the IP address function (which gets the IPV4) into a separate function as I check whether they are in office (on 192.168..) or external before checking the device ID. The list of device ID's associated with a user is stored in a SQL table and checked after username/password entry but before log in is completed to decide whether to request a 2FA code.
Here's the code:
dim thisDeviceId as String=GetClientDeviceId()
public Function GetClientDeviceId() As string
Dim mac As String = String.Empty
For Each netInterface In NetworkInterface.GetAllNetworkInterfaces()
If _
netInterface.NetworkInterfaceType = NetworkInterfaceType.Wireless80211 OrElse
netInterface.NetworkInterfaceType = NetworkInterfaceType.Ethernet Then
Dim address = netInterface.GetPhysicalAddress()
mac = BitConverter.ToString(address.GetAddressBytes())
End If
Next
return string.Format("{0}-{1}-{2}",dns.GetHostEntry(HttpContext.current.Request.ServerVariables("REMOTE_ADDR")).HostName, mac,GetClientDeviceIpAddress())
End Function
public Function GetClientDeviceIpAddress() As string
Dim ipv4Address As String = String.Empty
For Each currentIpAddress As IPAddress In Dns.GetHostAddresses(Dns.GetHostName())
If currentIpAddress.AddressFamily.ToString() = System.Net.Sockets.AddressFamily.InterNetwork.ToString() Then
ipv4Address = currentIpAddress.ToString()
Exit For
End If
Next
return ipv4Address
End Function
Even though it's not bulletproof and could be improved upon it might help someone.
I am not new here but this is my first question.
I have searched a lot and quite frankly can't understand how this is supposed to work.
I get data periodically (temperature) to my ESP32 and while having it set as a WiFi client, connect to my router and somehow store this data on my Laptop(or somewhere else, like a local/web site, don't know if that's possible/better).
How is the connection supposed to work? I have installed XAMPP and run the Apache and MySQL servers and I tried to connect to my Laptop with some sketches from Arduino using the ESP32 libraries
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
const char* host = "192.168.1.109"; //The local IP of my Laptop
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
but it doesn't connect.
Can someone please explain to me how this connection is supposed to take form or is this question too vague? I really just wanna know the "how-things-should-work-together" in this situation.
Thank you in advance.
OK, so after a lot of research and trying, I managed to work it out. I can now send an HTTP request (like GET or POST) from my ESP32 to a local server that is running on my laptop using XAMP and get a response. I can also connect to my local IP from my mobile phone (which is also in the same WiFi network).
Just for anyone else who wants to connect to a location in a server hosted on a PC in a local network, the steps are:
Create a local server on your PC, laptop whatever using an application like XAMPP (I have Windows 10 so WAMP would also work), download, install, open and start Apache.
Make sure that the Firewall lets your requests pass through (for me it was open by default, but I had seen elsewhere Firewall being an issue)
Go to your network settings, select the network that your devices(ESP32, phone, etc.)are connected and change its profile to Private, meaning that you trust this network, making your PC discoverable and able to accept requests. (That is really simple but took me hours to find)
Now, in order to connect from your phone to your PC, open a browser and enter the local IP (that is the IP that is given to your PC from the router as a local network name) of your PC to a browser and that's it, you are connected.
If you installed and ran XAMP, when connecting to your local IP(from same PC or other local device), it will forward you to 192.168.x.x/dashboard. If you want to create new workspaces and files, browse the XAMP folder in the installed location and inside the '/htdocs' subfolder do your testing.
For the ESP32 communication in Arduino(basic steps, not full code):
#include <WiFi.h>
#include <HTTPClient.h>
String host = "http://192.168.x.x/testfolder/";
String file_to_access = "test_post.php";
String URL = host + file_to_access;
void setup(){
WiFi.begin(ssid, password); //Connect to WiFi
HTTPClient http;
bool http_begin = http.begin(URL);
String message_name = "message_sent";
String message_value = "This is the value of a message sent by the ESP32 to local server
via HTTP POST request";
String payload_request = message_name + "=" + message_value; //Combine the name and value
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpResponseCode = http.sendRequest("POST", payload_request);
String payload_response = http.getString();
}
In the test_post.php (located in "C:\xampp\htdocs\testfolder\") file I used a simple script to echo a message received using a POST request, so it's only 'readable' from POST requests. Connecting to it from your browser will give you the "Sorry, accepting..." message.
<?php
$message_received = "";
if ($_SERVER["REQUEST_METHOD"] == "POST"){
$message_received = $_POST["message_sent"];
echo "Welcome ESP32, the message you sent me is: " . $message_received;
}
else {
echo "Sorry, accepting only POST requests...";
}
?>
Finally, using Serial prints, the output is:
Response Code: 200
Payload: Welcome ESP32, the message you sent me is: This is the value of a message sent by the ESP32 to local server via HTTP POST request
There it is, hope that this helps someone.
I'm trying to make a program that sends SNMP queries to some switches in the network.
Using the Net-snmp tools, I can send get requests to the switch using its name, and it works fine. But SNMP4J requires an IP address in CommunityTarget, so I get an IllegalArgumentException.
This is the relevant part of the code:
TransportMapping transport = new DefaultUdpTransportMapping();
transport.listen();
CommunityTarget comtarget = new CommunityTarget();
comtarget.setCommunity(new OctetString("public"));
comtarget.setVersion(SnmpConstants.version1);
comtarget.setAddress(new UdpAddress("switchName")); // exception happens here
comtarget.setRetries(2);
comtarget.setTimeout(1000);
How can I work around this?
You can get the IP address by using DNS resolution, like this answer says:
InetAddress address = InetAddress.getByName(switchName);
System.out.println(address.getHostAddress());
I am working on an ASP.NEt mvc web application, that stores data regarding network devices.but I need to know if there is a global format for these numbers:-
IP address
MAC address
VLanID
subnetmask
And is there any data annotations I can use to implement data validation for these numbers?
Thanks
For IP Address:
IPAddress ip = new IPAddress((long)ips);
return ip.ToString();
To store IP address, read this
For MacAddress, i dont know any existing formats. but you can take it from string.
string macAddStr = "00E0EE00EE00";
string macAddStrNew = macAddStr;
int insertedCount = 0;
for(int i = 2; i < macAddStr.Length; i=i+2)
macAddStrNew = macAddStrNew.Insert(i+insertedCount++, ":");
I actually tried with the following code, which gave me a result of the local machine Ip address, name and the server Ip address
var hostNamesList = NetworkInformation.GetHostNames();
HostName serverHost = new HostName("cptdomain.ctl.local");
StreamSocket clientSocket = new Windows.Networking.Sockets.StreamSocket();
// Try to connect to the remote host
await clientSocket.ConnectAsync(serverHost, "http");
foreach (var entry in hostNamesList)
{
if (entry.Type == HostNameType.DomainName)
{
hostName.Text = "Machine Name : " + entry.DisplayName;
IPAddress.Text = "Machine IP : " + clientSocket.Information.LocalAddress.DisplayName;
ServerAddress.Text = "Server IP : " + clientSocket.Information.RemoteAddress.DisplayName;
}
}
}
But actually my requirement is, how to get the list of IP addresses and name of the computers that are connected to the server in LAN for Windows Store Apps.
Please guide me in this to get the proper solution for running my store app.
Note: Here I was working with windows8 store apps with WinRT
This is how you do it in a full fat c# application, but I don't think you can do this on a winrt application. At a minimum you will have to add the private network privilege.