Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

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


        For Each ipadds As Net.IPAddress
        In _IPHostEntry.AddressList
            ListBox1.Items.Add(ipadds)
        Next

Please Help.
Posted
Updated 3-Feb-13 8:16am
v2
Comments
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.
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.
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
Next

1 solution

Quote:
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)
			Next
			Console.ReadLine()
	End Sub
End Module


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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900