Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Beginner] Simple Function for retrieving Local Computer IP
#1
Here is an example of a simple app I made with VB2008 to show you how you can retrieve your Local Computer IP Address from your network.

I did not use winsock in this code. Instead I used the function:

Code:
Function ReturnLocalIP(ByVal Type As Net.Sockets.AddressFamily) As String
        'Pass "Net.Sockets.AddressFamily.InterNetwork" for IPv4
        'Pass "Net.Sockets.AddressFamily.InterNetworkV6" for IPv6
        Dim HostEntry As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName)
        For Each IP As Net.IPAddress In HostEntry.AddressList
            If IP.AddressFamily = Type Then
                Return IP.ToString
            End If
        Next
        Return "NULL"
    End Function

You need to put that code in your form load. In my application it's under Public Class form 1.

Then when you add a variable, like a label or a textbox that you want to display the IP. You can call it with:

Code:
(Youre Variable) = ReturnLocalIP(Net.Sockets.AddressFamily.InterNetwork)

For example. I used a textbox so my code for the textbox was:

Code:
TextBox1.Text = ReturnLocalIP(Net.Sockets.AddressFamily.InterNetwork)

When you debug it, make sure it works. If you want to add the code to display the window on top. The code is:

Code:
Me.TopMost = True

and

Me.TopMost = False

I just put those in 2 radio buttons because I find it easier to view which option is selected, whether it is enabled or not. And I set the radio button where I wanted the option OFF to be automatically "dotted" when the application starts.

My Icon (Ico):
[Image: 23msa60.jpg]

Application View:
[Image: 28tgk10.jpg]

Enjoy Smile
Reply
#2
Nice -- I really like it, had no idea... I need to start learning some functions that might help. I'm going to book mark this thing.
[Image: t5BWm.png]
Reply
#3
I always found Start/Run/Cmd/Ipconfig to be easy enough?
None the less it seems like not a bad little app. Wish i could create one actually.
The Rules!
FTW Forum <-- Home of the Damned! --> Join me On MM


Reply
#4
(10-03-2010, 11:39 PM)DAMINK™ Wrote: I always found Start/Run/Cmd/Ipconfig to be easy enough?
None the less it seems like not a bad little app. Wish i could create one actually.

I think the same way as you haha Smile Thats why I posted the source code to my compiled application as well
Reply
#5
Nice code, I like the way you have gone about retrieving the local IP.
How ever I don't see the need for it, as a standalone function. You can simple type ipconfig in command prompt.
It doesn't get much easier that that!
[Image: s1.jpg]
Reply
#6
ipconfig /all but then you'd have to look through all the network interfaces to find the one your searching for. All the DNS numbers IP addresses, Gateways and other Subnet masks. It's hard to say which one could save you a little bit of time. Depends on the person I suppose. This can sort of be viewed as a filter so that you dont have to look through all the information that ipconfig in cmd prompt gives you, but you also get to feel good about yourself if you take the time to compile an app like this Smile lol
Reply
#7
True true. Ipconfig can give a bit of info.
However not as much as netstat -an or similar so thats why i said that Smile
Hell you could (xp) right click on network and choose status then support and it will show your local ip also.
Ultimately i think the real issue here is this.
Your local ip should remain the same in most circumstances.
Unless there is a power out or router reboot.
So well established systems will retain there local ip for long periods of time. Especially ones with UPS
But again its a good to show you have the skills to code something like this.
I really cant. Well not in VB. However the more i see on SF the more i want to start learning it.
The Rules!
FTW Forum <-- Home of the Damned! --> Join me On MM


Reply
#8
I know mine changes if a laptop connects to our network or another computer, then my computer has to search for a different host to avoid "conflicts" so I keep the IPV4 settings to detect automatically for that reason. It can be a pain to get everything sorted out if I was to choose the wrong local computer ip.

thanks Smile
Reply
#9
(10-04-2010, 12:33 AM)Infinity Wrote: I know mine changes if a laptop connects to our network or another computer, then my computer has to search for a different host to avoid "conflicts" so I keep the IPV4 settings to detect automatically for that reason. It can be a pain to get everything sorted out if I was to choose the wrong local computer ip.

thanks Smile

Yea thats a good point. Wireless devices that are not online or logged into the router.
They could throw the ip addressing.
However a connected pc should not loose its IP. Rather the laptop should be given a new one.
The Rules!
FTW Forum <-- Home of the Damned! --> Join me On MM


Reply
#10
I have to reset my router sometimes because internet conflicts that screw up my internet connection. When I reset the router, I get my internet back but all the computers connected to the network (ethernet) find different local IP's. Either way, if it's not practical, it's still good to understand the code to know how it retrieves your local computer IP. Most people use winsock, which is really old by now, so I thought i'd show you a different way of doing this.

educational purposes > practical use of the application
testing this code out = a sense of accomplishment
understanding the code = you've learned all you need to know here about my thread.

Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [VB.Net] Local/Remote TCP info from process [Source] The-One 5 4,129 08-11-2012, 05:18 PM
Last Post: Kenneth
  [Function] Check For Prime - C# Source AceInfinity 0 1,046 03-31-2012, 07:00 AM
Last Post: AceInfinity
  [Snippet] Get Local Getway euverve 1 1,025 05-16-2011, 07:56 PM
Last Post: KoBE
  Generate Random Numbers & Letters [ Small Function ] MYPE 22 4,159 04-02-2011, 05:25 PM
Last Post: Pyratepig
  [Tut] How to make an auto text spammer in vb 2008. [Beginner] DeMeR 4 4,193 10-04-2010, 07:57 AM
Last Post: Jordan L.

Forum Jump:


Users browsing this thread: 1 Guest(s)