-
Oct 21st, 2022, 12:32 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Can't get pinged IP, Its name
Hi, consider a tcp/ip network simple ping app. How can we get "EntryName" or maybe "HostName" (Not sure what's the differences) corresponding to that IP address and show it next to other parameters on a data grid view?
I came up with this code. IPs, estimated ping delay and being successful or their offline condition are totally alright. What is missing is SystemName (HostName/GuestName/ClientName/ServerName/EntryName whatever it is and you called it):
Code:
Dim PING As New Ping
Dim REPLY As PingReply
Try
For i = 0 To 255
For j = 100 To 255
REPLY = PING.Send("192.168." & i.ToString & "." & j.ToString, 1)
TextBox1.Text = "192.168." & i.ToString & "." & j.ToString
If REPLY.Status = IPStatus.Success Then
DataGridView1.Rows.Add("192.168." & i.ToString & "." & j.ToString, Dns.EndGetHostEntry(IPAddress.Parse(TextBox1.Text)).HostName, REPLY.RoundtripTime.ToString)
End If
Next
Next
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
TextBox1 is somewhere you put "Under scanning IP" in it.
However mentioned code leads to an exception instantly: Unable to cast object of type 'System.Net.IPAddress' to type 'System.IAsyncResult'
What can I do? What are my options? Any clues are welcome here... ♥ Pourkascheff/.
-
Oct 21st, 2022, 12:57 PM
#2
Re: Can't get pinged IP, Its name
Dns.EndGetHostEntry doesn't take an up address, it is part of the older async way of doing things. Is there a GetHostEntry method?
Also do you have Option Strict On set?
-
Oct 22nd, 2022, 12:34 PM
#3
Thread Starter
Hyperactive Member
Re: Can't get pinged IP, Its name
Originally Posted by PlausiblyDamp
Dns.EndGetHostEntry doesn't take an up address, it is part of the older async way of doing things. Is there a GetHostEntry method?
Um, yes. The whole code is up there. I pinged an IP, I just need its name i.e. "PlausiblyDamp_PC" in case of IPStatus.Success. I'm not as familiar as SerialPort with LAN.
Originally Posted by PlausiblyDamp
Also do you have Option Strict On set?
No, sir. It is not. When I turn it on an error says: "Option Strict On disallows implicit conversions from 'System.Net.IPAddress' to 'System.IAsyncResult'.".
Option explicit = on.
Option infer = on.
Option compare = binary.
-
Oct 22nd, 2022, 01:00 PM
#4
Re: Can't get pinged IP, Its name
Originally Posted by pourkascheff
Um, yes. The whole code is up there. I pinged an IP, I just need its name i.e. "PlausiblyDamp_PC" in case of IPStatus.Success. I'm not as familiar as SerialPort with LAN.
No, sir. It is not. When I turn it on an error says: "Option Strict On disallows implicit conversions from 'System.Net.IPAddress' to 'System.IAsyncResult'.".
Option explicit = on.
Option infer = on.
Option compare = binary.
In that case Option Strict On was pointing out the error at compile time, rather than crashing at runtime - it is rarely a good idea to disable Option Strict when it is pointing out errors, the errors are still there and will only catch you out later.
Your code above is using Dns.EndGetHostEntry, rather than Dns.GetHostEntry
DataGridView1.Rows.Add("192.168." & i.ToString & "." & j.ToString, Dns.EndGetHostEntry(IPAddress.Parse(TextBox1.Text)).HostName, REPLY.RoundtripTime.ToString)
-
Oct 22nd, 2022, 09:06 PM
#5
Re: Can't get pinged IP, Its name
Originally Posted by pourkascheff
No, sir. It is not. When I turn it on an error says: "Option Strict On disallows implicit conversions from 'System.Net.IPAddress' to 'System.IAsyncResult'.".
Option explicit = on.
Option infer = on.
Option compare = binary.
ALWAYS have Option Strict On at the project level. Make sure it is On in the VS options, so it will be On by default in all new projects. In specific instances where you need to use late-binding, turn it Off at the file level in only those files that specifically require late-binding, and use partial classes to keep the code in those files to an absolute minimum. NEVER turn it Off just to hide an error message like that. Fix the error that it is flagging.
-
Oct 23rd, 2022, 01:32 AM
#6
Thread Starter
Hyperactive Member
Re: Can't get pinged IP, Its name
Originally Posted by jmcilhinney
ALWAYS have Option Strict On at the project level.
Roger. o7
-
Oct 23rd, 2022, 01:38 PM
#7
Thread Starter
Hyperactive Member
Re: Can't get pinged IP, Its name
Originally Posted by PlausiblyDamp
Replacing "Dns.GetHostEntry" will throw an exception once a valid IP was found: "No such host is known."
Dns.EndGetHostEntry(IPAddress.Parse(TextBox1.Text).ToString).HostName was also tested. Same results achieved.
-
Oct 23rd, 2022, 01:45 PM
#8
Thread Starter
Hyperactive Member
Re: Can't get pinged IP, Its name
Originally Posted by jmcilhinney
NEVER turn it Off just to hide an error message like that. Fix the error that it is flagging.
Well, In this particular case, a simple single-lined code to return your IP address (Current slash using IP address i.e. your wi-fi adapter in case of using, not your unplugged ethernet which could be 127.0.0.1 or 100.273.255.253 sort of thing while you're using VPN. 192.168.0.0 sort of thing I mean.) could be:
Code:
MyIPAddress.Text = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList.LastOrDefault.ToString
In this newer version of VS, shows a warning: 'Public Shared Function GetHostByName(hostName As String) As System.Net.IPHostEntry' is obsolete: 'GetHostByName is obsoleted for this type, please use GetHostEntry instead.
Last edited by pourkascheff; Oct 23rd, 2022 at 01:59 PM.
-
Oct 23rd, 2022, 04:23 PM
#9
Re: Can't get pinged IP, Its name
Originally Posted by pourkascheff
Replacing "Dns.GetHostEntry" will throw an exception once a valid IP was found: "No such host is known."
Dns.EndGetHostEntry(IPAddress.Parse(TextBox1.Text).ToString).HostName was also tested. Same results achieved.
The EndGetHostEntry would only work if you had previously called the BeginGetHostEntry method - the two are used together when dealing with Async calls, however there are better ways to do this now.
What was the exception type that was thrown by Dns.GetHostEntry, the toe might be more helpful than the error message itself. Also are you sure it is Dns.GetHostEntry throwing the exception and not IPAddress.Parse?
-
Oct 24th, 2022, 04:25 PM
#10
Thread Starter
Hyperactive Member
Re: Can't get pinged IP, Its name
Originally Posted by PlausiblyDamp
What was the exception type that was thrown by Dns.GetHostEntry?
SocketException was unhandled by user code: No such host is known. Error code (In details): 11001
Oh that makes sense. Begin/End GetHostEntry.
It seemed an easy quest at first. A loop which checks every IP you tell, Is it online/responded? What is its name? "PlausiblyDamp_PC". If is there another way to do so, I'm ready to replace the entire code. I was checking This very recent topic and jmcilhinney topic in codebank archive. What is your opinion about them?
-
Oct 25th, 2022, 06:18 AM
#11
Re: Can't get pinged IP, Its name
Originally Posted by pourkascheff
SocketException was unhandled by user code: No such host is known. Error code (In details): 11001
Oh that makes sense. Begin/End GetHostEntry.
It seemed an easy quest at first. A loop which checks every IP you tell, Is it online/responded? What is its name? "PlausiblyDamp_PC". If is there another way to do so, I'm ready to replace the entire code. I was checking This very recent topic and jmcilhinney topic in codebank archive. What is your opinion about them?
looking at the documentation, and given the name being DNS, this method performs a DNS lookup on the address / ipaddress. That means if there is no DNS entry for the hostname / ipaddress then it will fail.
For a DNS query to map an ip address to a hostname requires the dns server to have been configured with a reverse lookup zone for the address range in question, there is a very good chance this hasn't been done.
Unfortunately there isn't an easy way to map an ip address to a hostname, in fact it is possible for a single ip address to be known with multiple names or even a single name to be associated with multiple addresses.
-
Feb 24th, 2023, 06:30 AM
#12
Thread Starter
Hyperactive Member
Re: Can't get pinged IP, Its name
Well, I tried this and it worked. (From oct. 2022)
Code:
Textbox1.Text = System.Net.Dns.GetHostEntry(ANYIPADDRESS.ToString).HostName
I might badly ask the question. "How to get a computer name based on its IP?" I'm angry now.
Last edited by pourkascheff; Feb 24th, 2023 at 06:38 AM.
-
Feb 24th, 2023, 06:54 AM
#13
Re: [RESOLVED] Can't get pinged IP, Its name
For future reference, if you want to delete a post, click Edit Post and then delete it from there. You need a minimum post count to do that though, so you may or may not have enough at the moment.
-
Feb 24th, 2023, 10:18 AM
#14
Re: [RESOLVED] Can't get pinged IP, Its name
Yeah, I think he needs a few more. I can't remember the level at which that is opened up. I thought it was down around a dozen, or so, but it might also be up over 1000.
My usual boring signature: Nothing
-
Feb 24th, 2023, 11:04 AM
#15
Re: [RESOLVED] Can't get pinged IP, Its name
Originally Posted by Shaggy Hiker
Yeah, I think he needs a few more. I can't remember the level at which that is opened up. I thought it was down around a dozen, or so, but it might also be up over 1000.
I checked as soon as I exceeded 2000 posts, and still didn't have it. After quite a few more posts, it was there. I suspect that it is set at 2048 as a nod to binary/powers of 2.
-
Feb 24th, 2023, 01:23 PM
#16
Re: [RESOLVED] Can't get pinged IP, Its name
You need to be a PowerPoster to delete posts. You gain that ability at 2048 posts as well as the ability to create custom titles. Before that point you get a different title at every power of 2 though with no extra powers until 2048.
-
Feb 24th, 2023, 02:35 PM
#17
Thread Starter
Hyperactive Member
Re: [RESOLVED] Can't get pinged IP, Its name
Thanks all for your concerns, didn't know y'all can see that : )
Originally Posted by Niya
2048.
No need to further edits Niya, I will check it out every powers-of-2 digital counts...
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|