I wrote a Windows Service which monitors devices on our LAN by (among other things) pinging them (using Ping.Send(); .NET 4.6.1). For a small number of PCs (3), I "occasionally" (once/day?) will get a PingException from Send(<ipaddr>, 5000), with InnerException.Message == "No such host is known". The next time the Send() is executed (~60 seconds later), it succeeds. I am using an IP address, as opposed to a name, so it's not a DNS issue.
I talked to the network admins about this issue, but they don't believe anything is wrong with the physical hardware. What other problems could this error be indicating?
Ping.Send() has various parameters which includes a parameter type of string than can either be a valid IP address or valid host-name. I suspect that your using one of the string parameters and sometimes passing an invalid IP (extra space, invalid IP etc...) and the Send() method conditionally resolves that you must be passing a host-name hence the exception regarding DNS.
Rather than send a string, why not utilize the parameter of type IPAddress as you've already stated that it should always be an IP. You can do this by attempting to parse the string into an IPAddress as shown below:
if (IPAddress.TryParse("**IP String**", out var ip))
{
using (var pong = new Ping())
{
pong.Send(ip);
//Etc...
}
}
Note that you will still need to fix your invalid data whichever way you look at it.
Related
I'm new to this networking stuff to configure and I'm trying to understand the following code.
var Server = new UdpClient();
var multicastIp = IPAddress.Parse(_connectionParams[0]);
IPAddress localIp;
if (IPAddress.TryParse(_connectionParams[1], out localIp))
Server.JoinMulticastGroup(multicastIp, localIp);
else
Server.JoinMulticastGroup(multicastIp);
var endPoint = new IPEndPoint(multicastIp, int.Parse(_connectionParams[2]));
Based on my understanding, Multicasting is sending the data to the multicast ip (like 233.7.6.5) through router and the receiver might need to join the group to receive data.
Server.JoinMulticastGroup(multicastIp, localIp);
On the above line, What is the use of localIp here? providing localip will unicast the data to particular ip? or something else that I need to understand.
No clues in Microsoft documentation: https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.udpclient.joinmulticastgroup?view=netframework-4.8#System_Net_Sockets_UdpClient_JoinMulticastGroup_System_Net_IPAddress_System_Net_IPAddress_
Server.JoinMulticastGroup(multicastIp, localIp);
In IPv4, a localIp can be provided as the way to look up a physical interface, if you don't choose a local interface or select the wildcard INADDR_ANY(0.0.0.0), the system gets to decide which of your interfaces it will join the group on, which might not be what you want on a system with many physical interfaces.
An OS is only allowed to create its own rules in multicast specifications for very narrow things, i.e. selecting the default interface in this case, so you can rely on any systems documentation you find helpful to determine what steps take what input, for example: linux's tldp documentation.
I been trying reading performance counter of a Clustered MSMQ. I've gone through several post to find out a way to read this value and I've seen solution which says that to read counter value one need to create a RegistryKey named as "NetNameForPerfCounters". This is to be created under HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\MSMQ\PARAMETER\NetNameForPerfCounters and as a value I've specified the network name of my cluster. But all in vain it is not reading anything.
var category = new PerformanceCounterCategory("MSMQ Queue", "<Clustered IP>")
Console.WriteLine(category.GetInstanceNames().Count().ToString());
This always return count as 0.
You'd think that IP address would work as machineName, but I'm not sure it does. I find that using an actual machine name (".", or "acomputername") works but using an IP address fails.
I find the same in PowerShell; using IP fails:
Get-Counter -Counter (Get-Counter -ListSet "MSMQ Queue" -Computer 127.0.0.1 ).PathsWithInstances
But the same line with -computer "machinename" succeeds and shows my queues.
For larger networks, I think this means you'd need #"domainname\machinename".
https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.diagnostics/get-counter
Anyone know if it can recover the ( hostname/machine name ) with a local IP address ? I know that the resolution reverse 'hostname -> ip' works perfectly with DeviceNetworkInformation.ResolveHostNameAsync (endpoint, OnNameResolved, null).
Is it possible to do the same thing but in reverse? Given the IP of a Local Machine and retrieve its hostname? A thousand thanks
Here is my code , but it's doesn't work the host is always called "192.168.1.5" like the ip . And it's should return "computer0001"
DeviceNetworkInformation.ResolveHostNameAsync("192.168.1.5", OnNameResolved, null);
private void OnNameResolved(NameResolutionResult result)
{
IPEndPoint[] endpoints = result.IPEndPoints;
if (endpoints != null)
{
if (endpoints.Length > 0)
{
//Host always return ip adress and not the machine name
host = endpoints[0].ToString();
}
}
}
Not sure if there is specific API on Windows Phone for that purpose.
I suggest you looking into similar question 'DNS lookup from custom DNS server in c#'.
If you're okay with going only WP8, then you should study this part of MSDN - Supported Win32 APIs for Windows Phone 8.
For example, getpeername function might return you information you're looking for. Or you might create new HostName object passing IP address there and trying to read DisplayName, CanonicalName or RawName properties of it.
Make sure you test with both emulator and real device as WP8 emulator has a little bit complex network configuration.
Would be great if you can update us if you have any success with the task.
string myHost = System.Net.Dns.GetHostName();
string myIP =
System.Net.Dns.GetHostByName(myHost).AddressList[0].ToString();
MessageBox.Show(myIP);
TextReader read = new StreamReader(//Text file with network address);
var ip = read.ReadLine();
read.Close();
if (ip == //Need help with this part)
MessageBox.Show("You are on the network");
else
MessageBox.Show("You are not on the network");
I can get my computers address but I need to compare it to a network address and if it falls under that network address to show that it is.
If your program crashes when your IP is invalid, you can change it over to a try catch that checks for that exception (run it and crash it to find the exception). This is how I validated the IP in my multiplayer with Lidgren.
Though this is probably not the best way, it works.
One simple way to see if an ip is in a range is to convert the ip and the start and end of the range to longs (a function can be written for the conversion to a long from an IP address). Then see if the ip is between the two numbers.
For example:
IP to check: 172.17.1.25 -- Converted to long --> 172017001025
Range:
Start - 172.0.0.0 -- Converted to long --> 172000000000
End - 172.255.255.255 -- Converted to long --> 172255255255
Now just check if the ip is between the start and end values.
I recommend the IPNetwork utility that can be found on codeplex. It is pretty flexible and powerful. It will allow you to do a lot of things, and determining the network for a given ip address is one of them.
http://ipnetwork.codeplex.com/
The code should look like :
var ipnetwork = IPNetwork.Parse(ip);
if (ipnetwork.Contains(myIp)) {
// do some work...
Hey all. I have written a program that sequentially scans certain parts of a LAN for computers (code will be provided). However, when I run this code, it only returns the DNS HostName of the computer it is running on. I looked into using WMI, but I cannot, as I will not always have priveleges to the computers being found. Is there any other way to find a local computers HostName?
using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
namespace CheckLocalNetwork
{
class PingCheck
{
public static string fullip;
public void CheckSequentialIP()
{
IPHostEntry IpEntry = Dns.GetHostEntry(fullip);
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
options.DontFragment = true;
string data = "a";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 120;
PingReply reply = pingSender.Send(fullip, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine("Address: {0}", reply.Address.ToString());
Console.WriteLine("Host Name: {0}", IpEntry.HostName);
Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime);
Console.WriteLine("Time to live: {0}", reply.Options.Ttl);
Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
Console.WriteLine("Buffer size: {0}", reply.Buffer.Length);
Console.WriteLine("");
}
}
static void Main(string[] args)
{
Console.WriteLine("Press enter to search for ip adresses that begin with 192.168.1");
Console.ReadLine();
for (int endofip = 1; endofip < 101; endofip++)
{
fullip = "192.168.1." + Convert.ToString(endofip);
PingCheck checkfullip = new PingCheck();
checkfullip.CheckSequentialIP();
}
Console.ReadLine();
}
All help is much appreciated.
Hmm - your code sample behaves as expected on my machine - i.e. it returns the hostname of the machine being scanned.
To investigate your problem deeper, have you tried using nslookup to check the ip addresses resolve?
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\Rob>nslookup <-- type this at a command prompt
Default Server: mydns.mydomain.co.uk <--- these two lines indicate the dns server being used to resolve your queries
Address: 192.168.0.1 <----|
> 192.168.0.5 <---- type in the ip address of one of the machines in question
Server: mydns.mydomain.co.uk
Address: 192.168.0.1
Name: myworkstation.mydomain.co.uk <---- this is the hostname, as reported by the DNS using a reverse lookup
Address: 192.168.0.5
If this doesn't return the machine name, then you may have a name resolution issue that is not related to your code.
If this all looks ok, then it might also be worth enumerating the IpEntry.Aliases collection. Are there any entries here, and do they make sense?
Finally - is the code you have above exactly the code that is going wrong for you, or is it a "distilled" example? The reason I ask is that the documentation for Dns.GetHostEntry states
"When an empty string is passed as the
host name, this method returns the
IPv4 addresses of the local host."
I also notice you're holding "fullip" in a static. If this is not the exact code that is causing the problem, especially if this runs multithreaded, is there a chance you are not initialising "fullip" before the Dns.GetHostEntry is called?
I may be way off, but I thought is was worth giving a brain dump of what occured to me as I looked at your problem :)
[EDIT:] - your comment to kdt has clarified something I misunderstood. I thought you were saying you always got back the hostname for your local machine, no matter which machine you "scanned" - which is very odd behaviour. In fact I think you are saying you just get back IP addresses for other machines (their IP address), and only get a hostname for your local. Disregard my last bit about the threading and the empty argument.
This is far more easily explained - your machine is almost certainly just not able to resolve the machine names - I expect my nslookup test I suggested will not return the machine names either.
In order to resolve these IP's to host names, your machine needs a DNS that has entries for these machines, or to have them in its local hosts file; your machine isn't actually asking the remote machine for its name when you do this call so it won;t be able to find it out without help from one of its usual name resolution paths.
It works for me, because my local DNS really does have entries for all the machines on my network, resolving their host names to ip addresses and vice-versa.