Click here to Skip to main content
Sign Up to vote bad
good
See more: VBVB.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

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
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

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  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Christian Graus 488
1 Ron Beyer 306
2 Tadit Dash 253
3 samadhan_kshirsagar 229
4 OriginalGriff 198
0 Sergey Alexandrovich Kryukov 7,041
1 Prasad_Kulkarni 3,815
2 OriginalGriff 3,557
3 _Amy 3,372
4 CPallini 3,034


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