Click here to Skip to main content
12,885,022 members (31,439 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB VB.NET
I have a project in VB, and i am trying to get all the IPv4 addresses of all other computers on the network. I have this code but it does not seem to work as it gets IPv6.

Dim _IPHostEntry As Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName)

        For Each ipadds As Net.IPAddress
        In _IPHostEntry.AddressList

Please Help.
Posted 3-Feb-13 8:02am
Updated 3-Feb-13 8:16am
Zoltán Zörgő 3-Feb-13 14:41pm
And how do you know which are "all other computers on the network"? First of all what is the extent of the "network" in your situation?
TheTimer 3-Feb-13 15:02pm
All computers on the network = All computers connected to the same host a me.
Zoltán Zörgő 3-Feb-13 15:35pm
All computers connected to the same host as me is still underdefined. You are connected to codeproject host as me. Are we on the same network? Do you see the problem? Please try to define more precisely the extent, because the possible solution depends on it.
Mike Meinz 3-Feb-13 16:08pm
GetHostEntry gets the IP addresses of the specified host. It does not search for all IP addresses on the network. It returns both the IPv4 and the IPv6 address of the requested computer. The following code selects the IPv4 address:

Dim strIPAddress as String = "" ' This computer's IP Address
Dim strHostName as String = System.Net.Dns.GetHostName ' This computer's name
Dim objAddressList() As System.Net.IPAddress = System.Net.Dns.GetHostEntry(strHostName).AddressList
For x = 0 To objAddressList.GetUpperBound(0)
If objAddressList(x).AddressFamily = Net.Sockets.AddressFamily.InterNetwork then
strIPAddress = objAddressList(x).ToString
Exit For
End If
TheTimer 3-Feb-13 16:45pm
What ever IPv4s I would get if I type in "netstat" in cmd.exe.
I hope that is descriptive enough.
TheTimer 3-Feb-13 16:46pm
What is "strHostName" and "strIPAddress"?
Mike Meinz 3-Feb-13 18:32pm
Sorry. Thought that was obvious. I updated the solution to declare them and put in a comment for each of the declarations. This solution does not answer your question. It demonstrates the way to get the IPv4 address from the AddressList property of GetHostEntry.
TheTimer 3-Feb-13 20:47pm
Grate now no errors! Is there a variable that i can use to add them to a list box?
Mike Meinz 3-Feb-13 20:54pm
Yes there is. Lookup the ListBox control in the Help file and read about the ListBox methods.
TheTimer 3-Feb-13 21:06pm
That is not what I ment, let me reword that.
how do I get the list of IPs from that code?

PS: Thank you for helping
Mike Meinz 3-Feb-13 21:22pm
You don't. My comment said that this only demonstrated how to select the IPv4 address from an AddressList object. Furthermore, my comment said that this only returned the AddressList object for the requested computer. Solution 1 with the check for IPv4 type addresses look like what you need. Try reviewing information about these classes in the Help file.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

What ever IPv4s I would get if I type in "netstat" in cmd.exe.

So netstat[^] used without parameters, displays active TCP connections.
Use IPGlobalProperties.GetActiveTcpConnections Method[^] to get all connections of your machine.

Imports System
Imports System.Net
Imports System.Net.NetworkInformation
Module Program
	Sub Main()
		Dim ipProperties As IPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties()
			Dim tcpConnections As TcpConnectionInformation() = ipProperties.GetActiveTcpConnections()
			For Each info As TcpConnectionInformation In tcpConnections
				Console.WriteLine("Local : " & info.LocalEndPoint.Address.ToString() & ":" & info.LocalEndPoint.Port.ToString() & vbLf & "Remote : " & info.RemoteEndPoint.Address.ToString() & ":" & info.RemoteEndPoint.Port.ToString() & vbLf & "State : " & info.State.ToString() & vbLf & vbLf)
	End Sub
End Module

You can use AddressFamily[^] property to filter connections for only IPv4 ones.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170424.1 | Last Updated 9 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100