Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2011
    Messaggi
    237

    [VB.NET] Enumerari IP e dei PC nella rete locale

    Ciao ragazzi
    Lavoro con VB.NET e VS2012.
    Sto ultimando un programmino stile client/server per scambiare messaggi tra PC in rete.
    Fin'ora ho lavorato con l'IP di loopback e sono riuscito a fare più o meno quello che dovevo.
    Ora vorrei aggiungere qualche opzione alla mia applicazione.
    Ad esempio vorrei riuscire a mostrare i nomi e gli indirizzi IP dei pc collegati in LAN, all'interno dello stesso gruppo di lavoro, in modo da facilitare la connessione al "server".
    Da ieri cerco e qualcosa ho trovato:
    Con questa API
    http://www.pinvoke.net/default.aspx/...erverenum.html
    Enumero alcune info dei PC nel lan, tra cui il nome. (non posto il codice perché è lungo...)
    Ora però vorrei riuscire ad associare a ciascun nome l'indirizzo IP.
    Come si fa? Ho cercato ma sono andato in confusione...
    C'è qualche API specifica?
    Grazie.

  2. #2
    Utente di HTML.it
    Registrato dal
    Jan 2011
    Messaggi
    237
    Ciao
    Ripropongo il post perché non ho ancora risolto del tutto.
    Per chi volesse provare questo codice:
    codice:
    Imports System.Runtime.InteropServices
    Imports System.Security
    Imports System.Net
    
    
    
    
    Public Class NetworkUtility
    
    
        <DllImport("Netapi32", CharSet:=CharSet.Auto, SetLastError:=True), SuppressUnmanagedCodeSecurityAttribute> _
        Private Shared Function NetServerEnum(ServerNane As String, dwLevel As Integer, ByRef pBuf As IntPtr, dwPrefMaxLen As Integer, ByRef dwEntriesRead As Integer, ByRef dwTotalEntries As Integer, dwServerType As Integer, domain As String, ByRef dwResumeHandle As Integer) As Integer
        End Function
    
    
        <DllImport("Netapi32", SetLastError:=True), SuppressUnmanagedCodeSecurityAttribute> _
        Private Shared Function NetApiBufferFree(pBuf As IntPtr) As Integer
        End Function
    
    
    
    
        Private Const MAX_PREFERRED_LENGTH As Integer = -1
        Private Const SV_TYPE_WORKSTATION As Integer = 1
        Private Const SV_TYPE_SERVER As Integer = 2
        Private Const SV_TYPE_NT As Integer = &H1000
    
    
    
    
        'create a _SERVER_INFO_100 STRUCTURE
        <StructLayout(LayoutKind.Sequential)> _
        Private Structure _SERVER_INFO_100
            Friend sv100_platform_id As Integer
            <MarshalAs(UnmanagedType.LPWStr)> _
            Friend sv100_name As String
        End Structure
    
    
        Public Shared Function GetIPDictionary() As Dictionary(Of String, String)
    
    
            Dim networkComputers As New Dictionary(Of String, String)
            'local fields
            Dim buffer As IntPtr = IntPtr.Zero
            Dim tmpBuffer As IntPtr = IntPtr.Zero
            Dim entriesRead As Integer = 0
            Dim totalEntries As Integer = 0
            Dim resHandle As Integer = 0
            Dim sizeofINFO As Integer = Marshal.SizeOf(GetType(_SERVER_INFO_100))
    
    
    
    
            Try
                'call the DllImport : NetServerEnum with all its required parameters
                Dim ret As Integer = NetServerEnum(Nothing, 100, buffer, MAX_PREFERRED_LENGTH, entriesRead, totalEntries, SV_TYPE_NT, Nothing, resHandle)
                'if the returned with a NERR_Success (C++ term), =0 for C#
                If ret = 0 Then
                    'loop through all SV_TYPE_WORKSTATION and SV_TYPE_SERVER PC's
                    For i As Integer = 0 To totalEntries - 1
           
    
    
                        tmpBuffer = New IntPtr(CInt(buffer) + (i * sizeofINFO))
     
                        Dim svrInfo As _SERVER_INFO_100 = CType(Marshal.PtrToStructure(tmpBuffer, GetType(_SERVER_INFO_100)), _SERVER_INFO_100)
                        Dim IP As IPAddress = GetIpFromHostName(svrInfo.sv100_name)
    
    
                        If Not IP Is Nothing Then
                            networkComputers.Add(svrInfo.sv100_name, IP.ToString)
                        Else
                            networkComputers.Add(svrInfo.sv100_name, "NO IP Address")
                        End If
                        'add the PC names to the ArrayList
    
    
                    Next
                End If
            Catch ex As Exception
                MessageBox.Show("Problem with acessing network computers in NetworkBrowser " + vbCr & vbLf & vbCr & vbLf & vbCr & vbLf + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
                Return Nothing
            Finally
                'The NetApiBufferFree function frees 
                'the memory that the NetApiBufferAllocate function allocates
                NetApiBufferFree(buffer)
            End Try
            'return entries found
            Return networkComputers
        End Function
    
    
        Private Shared Function GetIpFromHostName(HostName As String) As IPAddress
    
    
    
    
            Try
                For Each IP As IPAddress In Net.Dns.GetHostAddresses(HostName)
                    If IP.AddressFamily = Sockets.AddressFamily.InterNetwork Then
                        Return IP
                    End If
                Next
            Catch ex As Exception
                Return Nothing
            End Try
            Return Nothing
        End Function
    
    
    End Class
    La funzione shared restituisce un dictionary di string con il nome del PC host e il suo indirizzo IP.
    Il punto è che la funzione mi vede il PC su cui la eseguo, il pc del mio vicino di casa con cui condivido la rete ma non vede il mio notebook, che ho qua a fianco.
    Lo vede a livello di host ma quando vado a ricercare IP la funzione "GetHostAddresses" mi genera un'eccezione, dicendo Host non trovato.
    Se eseguo la stessa routine sul notebook che non viene trovato in rete, la funzione "NetServerEnum" mi da errore 2184, che dalla documentazione qui http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx mi parla di The service has not been started.
    Ok, sul notebook con XP il servizio per enumerare i PC nel dominio non è attivo..
    Come posso fare per attivarlo? Forse anche per quello, dal PC che uso per programmare riesco a vedere il notebook, ma non a recuperare il suo IP...
    Non so come andare avanti..

    Grazie!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.