Click here to Skip to main content
Rate this: bad
good
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
            ListBox1.Items.Add(ipadds)
        Next
Please Help.
Posted 3-Feb-13 8:02am
Edited 3-Feb-13 8:16am
v2
Comments
Zoltán Zörgő at 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 at 3-Feb-13 15:02pm
   
All computers on the network = All computers connected to the same host a me.
Zoltán Zörgő at 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 at 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 at 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
TheTimer at 3-Feb-13 16:46pm
   
What is "strHostName" and "strIPAddress"?
Mike Meinz at 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 at 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 at 3-Feb-13 20:54pm
   
Yes there is. Lookup the ListBox control in the Help file and read about the ListBox methods.
TheTimer at 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 at 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
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 449
1 Maciej Los 274
2 CPallini 160
3 Sergey Alexandrovich Kryukov 152
4 Richard MacCutchan 130
0 OriginalGriff 5,474
1 Sergey Alexandrovich Kryukov 4,713
2 Peter Leow 2,944
3 DamithSL 2,465
4 Maciej Los 2,434


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