-
Dec 11th, 2017, 02:23 PM
#1
Thread Starter
Member
[RESOLVED] Get all IPs and PC names in a LAN using TCPClient
Hi
I have created a simple LAN (ivp4). How can I get a list of IP addresses and names of all computers in the LAN using TCP/IP sockecting.( DNS not used.)
Something in the lines of
Code:
For each
Listbox1.items.add(computer name &"_"& IPAddress)
Next
This is not (a chat or file )e transfer program.
I haven't the least clue on how to proceed.
Thanks
-
Dec 11th, 2017, 04:01 PM
#2
Re: Get all IPs and PC names in a LAN using TCPClient
This might work for you. It uses the address resolution protocol as a means of locating other connected PCs. The sample code is for VB6 but it's nothing that can't be translated to VB.Net or any other language.
-
Dec 11th, 2017, 04:46 PM
#3
Thread Starter
Member
Re: Get all IPs and PC names in a LAN using TCPClient
I got the following (modified) code from here. (a Chat program)
But when I run it I get the following FatalEnginException Error:
The runtime has encountered a fatal error. The address of the error was at 0x887918d4, on thread 0x6e4. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.
The error occurs at the line:
Code:
ServerInfo = CType(Marshal.PtrToStructure(New IntPtr(CurPtr), GetType(SERVER_INFO_101)), SERVER_INFO_101)
and error has to do with the CurPtr integer variable.
Code:
Imports System.Runtime.InteropServices
Public Class Form1
Public Const SV_TYPE_ALL As Integer = &HFFFFFFFF
Public Structure SERVER_INFO_101
Public Platform_ID As Integer
<MarshalAsAttribute(UnmanagedType.LPWStr)> Public Name As String
Public Version_Major As Integer
Public Version_Minor As Integer
Public Type As Integer
<MarshalAsAttribute(UnmanagedType.LPWStr)> Public Comment As String
End Structure
Public Declare Unicode Function NetServerEnum Lib "Netapi32.dll" ( _
ByVal Servername As Integer, ByVal Level As Integer, ByRef Buffer As Integer, ByVal PrefMaxLen As Integer, _
ByRef EntriesRead As Integer, ByRef TotalEntries As Integer, ByVal ServerType As Integer, _
ByVal DomainName As String, ByRef ResumeHandle As Integer) As Integer
Public Declare Function NetApiBufferFree Lib "Netapi32.dll" (ByVal lpBuffer As Integer) As Integer
Public Shared Function GetNetworkComputers(Optional ByVal DomainName As String = Nothing) As List(Of String)
Dim ServerInfo As SERVER_INFO_101
Dim MaxLenPref As Integer = -1
Dim level As Integer = 101
Dim ResumeHandle As Integer = 0
Dim ret, EntriesRead, TotalEntries, BufPtr, CurPtr As Integer
Dim ReturnList As New List(Of String)
Try
ret = NetServerEnum(0, level, BufPtr, MaxLenPref, EntriesRead, TotalEntries, SV_TYPE_ALL, DomainName, ResumeHandle)
If ret = 0 Then
CurPtr = BufPtr
For i As Integer = 0 To EntriesRead - 1
ServerInfo = CType(Marshal.PtrToStructure(New IntPtr(CurPtr), GetType(SERVER_INFO_101)), SERVER_INFO_101)
CurPtr = CurPtr + Len(ServerInfo)
ReturnList.Add(ServerInfo.Name)
Next
End If
NetApiBufferFree(BufPtr)
Catch ex As Exception
End Try
Return ReturnList
End Function
Private Sub btnGet_Click(sender As Object, e As EventArgs) Handles btnGet.Click
LstBox.DataSource = GetNetworkComputers()
End Sub
End Class
Any ideas
-
Dec 12th, 2017, 01:22 PM
#4
Re: Get all IPs and PC names in a LAN using TCPClient
Here, I made some changes to the code:-
vbnet Code:
Imports System.Runtime.InteropServices Public Class Form1 Public Const SV_TYPE_ALL As Integer = &HFFFFFFFF Public Structure SERVER_INFO_101 Public Platform_ID As Integer <MarshalAsAttribute(UnmanagedType.LPWStr)> Public Name As String Public Version_Major As Integer Public Version_Minor As Integer Public Type As Integer <MarshalAsAttribute(UnmanagedType.LPWStr)> Public Comment As String End Structure Public Declare Unicode Function NetServerEnum Lib "Netapi32.dll" ( _ ByVal Servername As Integer, ByVal Level As Integer, ByRef Buffer As IntPtr, ByVal PrefMaxLen As Integer, _ ByRef EntriesRead As Integer, ByRef TotalEntries As Integer, ByVal ServerType As Integer, _ ByVal DomainName As String, ByRef ResumeHandle As Integer) As Integer Public Declare Function NetApiBufferFree Lib "Netapi32.dll" (ByVal lpBuffer As IntPtr) As Integer Public Shared Function GetNetworkComputers(Optional ByVal DomainName As String = Nothing) As List(Of String) Dim ServerInfo As SERVER_INFO_101 Dim MaxLenPref As Integer = -1 Dim level As Integer = 101 Dim ResumeHandle As Integer = 0 Dim ret, EntriesRead, TotalEntries As Integer Dim BufPtr As IntPtr Dim ReturnList As New List(Of String) Try ret = NetServerEnum(0, level, BufPtr, MaxLenPref, EntriesRead, TotalEntries, SV_TYPE_ALL, DomainName, ResumeHandle) If ret = 0 Then For i As Integer = 0 To EntriesRead - 1 ServerInfo = Marshal.PtrToStructure(IncrementPointer(BufPtr, i * Marshal.SizeOf(ServerInfo)), GetType(SERVER_INFO_101)) ReturnList.Add(ServerInfo.Name) Next End If NetApiBufferFree(BufPtr) Catch ex As Exception End Try Return ReturnList End Function Private Shared Function IncrementPointer(ByVal ptr As IntPtr, ByVal i As Integer) As IntPtr If IntPtr.Size = 4 Then Return New IntPtr(ptr.ToInt32 + i) End If If IntPtr.Size = 8 Then Return New IntPtr(ptr.ToInt64 + i) End If End Function Private Sub btnGet_Click(sender As Object, e As EventArgs) Handles btnGet.Click LstBox.DataSource = GetNetworkComputers() End Sub End Class
See if it works now.
-
Dec 15th, 2017, 03:05 AM
#5
Thread Starter
Member
Re: Get all IPs and PC names in a LAN using TCPClient
Yes thanks Niya. the IncrementPointer function has solved the problem.
Only the PC Names are listed. How about IP
Code:
For each
Listbox1.items.add(PC Name &"_"& IPAddress)
Next
-
Dec 15th, 2017, 09:29 AM
#6
Re: Get all IPs and PC names in a LAN using TCPClient
Originally Posted by gbhsk
Yes thanks Niya. the IncrementPointer function has solved the problem.
Did you copy ONLY the IncrementPointer function to your code? I made more changes than that to the code you posted. Mainly, the original code wasn't using pointers correctly so I fixed all instances of incorrect use. You were meant to replace all of your original code with all the code I posted.
Originally Posted by gbhsk
Only the PC Names are listed. How about IP
Code:
For each
Listbox1.items.add(PC Name &"_"& IPAddress)
Next
You can use the a PC's name to obtain it's IP address:-
vbnet Code:
' 'The name of the PC goes in this variable Dim pcName As String = "PCName" 'Lists all the IP addresses assigned to the PC 'Note: This lists ALL addresses, even IPv6 addresses For Each ip As Net.IPAddress In Net.Dns.GetHostAddresses(pcName) Debug.WriteLine(ip.ToString) Next
The above code lists all the IP addresses assigned to a PC. As implied, you should be aware that a PC can have more than one IP address and many times will also have IPv6 addresses. So if you're only interested in one of these addresses, you will have to filter it out with. Also know that an exception would be thrown if the you give GetHostAddresses a host it can't find or one that doesn't exist.
Last edited by Niya; Dec 15th, 2017 at 09:32 AM.
-
Dec 15th, 2017, 10:32 AM
#7
Thread Starter
Member
Re: Get all IPs and PC names in a LAN using TCPClient
Yes I did use all the code which worked perfectly well providing PC names. Besides PC Names I also need static IPv4 IP addresses for the active PCs in the LAN. So the code snippet above may still have to be filtered further(with if statements perhaps) for any IPv4 addresses.
Thanks
-
Dec 15th, 2017, 12:59 PM
#8
Re: Get all IPs and PC names in a LAN using TCPClient
Originally Posted by gbhsk
Besides PC Names I also need static IPv4 IP addresses for the active PCs in the LAN.
You will have to filter out the the IPv4 addresses yourself like this:-
vbnet Code:
' 'The name of the PC goes in this variable Dim pcName As String = "PCName" 'Lists all the IP addresses assigned to the PC 'Note: The lists ALL addresses, even IPv6 addresses For Each ip As Net.IPAddress In Net.Dns.GetHostAddresses(pcName) 'Only print out IPv4 Addresses If ip.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then Debug.WriteLine(ip.ToString) End If Next
The above is a slight modification that prints only the IPv4 addresses. Be careful though. In practice you will get a single address for a PC most times but never assume that will always be the case. A networked device can have multiple network interfaces, such devices do include Windows PCs. If there are multiple IPv4 addresses for a host referenced by GetHostAddresses, they will be returned in the array. So always be prepared to handle such cases.
Last edited by Niya; Dec 15th, 2017 at 01:04 PM.
-
Dec 15th, 2017, 04:06 PM
#9
Thread Starter
Member
Re: Get all IPs and PC names in a LAN using TCPClient
And finally your previous snippet (modified) has made it.
Thanks immensely.
Final working code is below.
Code:
Imports System.Runtime.InteropServices
Public Class Form1
Public Const SV_TYPE_ALL As Integer = &HFFFFFFFF
Public Structure SERVER_INFO_101
Public Platform_ID As Integer
<MarshalAsAttribute(UnmanagedType.LPWStr)> Public Name As String
Public Version_Major As Integer
Public Version_Minor As Integer
Public Type As Integer
<MarshalAsAttribute(UnmanagedType.LPWStr)> Public Comment As String
End Structure
Public Declare Unicode Function NetServerEnum Lib "Netapi32.dll" ( _
ByVal Servername As Integer, ByVal Level As Integer, ByRef Buffer As IntPtr, ByVal PrefMaxLen As Integer, _
ByRef EntriesRead As Integer, ByRef TotalEntries As Integer, ByVal ServerType As Integer, _
ByVal DomainName As String, ByRef ResumeHandle As Integer) As Integer
Public Declare Function NetApiBufferFree Lib "Netapi32.dll" (ByVal lpBuffer As IntPtr) As Integer
Public Shared Function GetNetworkComputers(Optional ByVal DomainName As String = Nothing) As DataTable
Dim ServerInfo As SERVER_INFO_101
Dim MaxLenPref As Integer = -1
Dim level As Integer = 101
Dim ResumeHandle As Integer = 0
Dim ret, EntriesRead, TotalEntries As Integer
Dim BufPtr As IntPtr
Dim dt As New DataTable
dt.Columns.Add("IPs")
dt.Columns.Add("PCs")
Try
ret = NetServerEnum(0, level, BufPtr, MaxLenPref, EntriesRead, TotalEntries, SV_TYPE_ALL, DomainName, ResumeHandle)
If ret = 0 Then
For i As Integer = 0 To EntriesRead - 1
ServerInfo = Marshal.PtrToStructure(IncrementPointer(BufPtr, i * Marshal.SizeOf(ServerInfo)), GetType(SERVER_INFO_101))
Dim Rw As DataRow = dt.NewRow
Dim IPstring As String = ""
For Each ip As Net.IPAddress In Net.Dns.GetHostAddresses(ServerInfo.Name)
'Debug.WriteLine(ip.ToString)
If Not ip.IsIPv6LinkLocal Then
IPstring = IPstring & ip.ToString
End If
Next
Rw(0) = IPstring.ToString
Rw(1) = ServerInfo.Name.ToString
dt.Rows.Add(Rw)
Next
End If
NetApiBufferFree(BufPtr)
Catch ex As Exception
MsgBox(ex.Message)
End Try
Return dt
End Function
Private Shared Function IncrementPointer(ByVal ptr As IntPtr, ByVal i As Integer) As IntPtr
If IntPtr.Size = 4 Then
Return New IntPtr(ptr.ToInt32 + i)
End If
If IntPtr.Size = 8 Then
Return New IntPtr(ptr.ToInt64 + i)
End If
End Function
Private Sub btnGet_Click(sender As Object, e As EventArgs) Handles btnGet.Click
'LstBx.DataSource = GetNetworkComputers()
LstBx.Items.Clear()
Dim dtt As DataTable = GetNetworkComputers()
If dtt.Rows.Count > 0 Then
For i As Integer = 0 To dtt.Rows.Count - 1
LstBx.Items.Add(dtt.Rows(i)(0).ToString & ":" & dtt.Rows(i)(1).ToString)
Next
End If
dtt.Dispose()
End Sub
End Class
-
Dec 15th, 2017, 04:48 PM
#10
Re: Get all IPs and PC names in a LAN using TCPClient
Originally Posted by gbhsk
Thanks immensely.
You're welcomed.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|