Results 1 to 17 of 17

Thread: [RESOLVED] Can't get pinged IP, Its name

  1. #1

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    383

    Resolved [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/.

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,746

    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?

  3. #3

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    383

    Re: Can't get pinged IP, Its name

    Quote Originally Posted by PlausiblyDamp View Post
    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.
    Quote Originally Posted by PlausiblyDamp View Post
    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.

  4. #4
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,746

    Re: Can't get pinged IP, Its name

    Quote Originally Posted by pourkascheff View Post
    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)

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,765

    Re: Can't get pinged IP, Its name

    Quote Originally Posted by pourkascheff View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    383

    Re: Can't get pinged IP, Its name

    Quote Originally Posted by jmcilhinney View Post
    ALWAYS have Option Strict On at the project level.
    Roger. o7

  7. #7

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    383

    Re: Can't get pinged IP, Its name

    Quote Originally Posted by PlausiblyDamp View Post
    Your code above is using Dns.EndGetHostEntry, rather than Dns.GetHostEntry
    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.

  8. #8

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    383

    Re: Can't get pinged IP, Its name

    Quote Originally Posted by jmcilhinney View Post
    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.

  9. #9
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,746

    Re: Can't get pinged IP, Its name

    Quote Originally Posted by pourkascheff View Post
    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?

  10. #10

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    383

    Re: Can't get pinged IP, Its name

    Quote Originally Posted by PlausiblyDamp View Post
    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?

  11. #11
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,746

    Re: Can't get pinged IP, Its name

    Quote Originally Posted by pourkascheff View Post
    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.

  12. #12

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    383

    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.

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,765

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,441

    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

  15. #15
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,366

    Re: [RESOLVED] Can't get pinged IP, Its name

    Quote Originally Posted by Shaggy Hiker View Post
    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.

  16. #16
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,722

    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  17. #17

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    383

    Re: [RESOLVED] Can't get pinged IP, Its name

    Thanks all for your concerns, didn't know y'all can see that : )

    Quote Originally Posted by Niya View Post
    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
  •  



Click Here to Expand Forum to Full Width