PDA

View Full Version : بدست آوردن IPAddress مربوط به Client های یک شبکه



azygole
یک شنبه 06 فروردین 1385, 13:31 عصر
سلام خسته نباشید . سال نو مبارک .
ببخشید یه سئوال داشتم . من می خواهم IPAddress مربوط به Client های یک شبکه را بدست بیارم . از دستورات زیر هم استفاده کردم . اما روی هر client به من IP مربوط به server را بر می گرداند اگه میشه راهنماییم کنید .

String strHostName = Dns.gethos();
IPHostEntry hostInfo = Dns.Resolve(strHostName);

m-heidari
چهارشنبه 09 فروردین 1385, 01:31 صبح
شاید این کد به دردت بخوره . که انشاالله میخوره D:

البته این کد کاملتر هست و بیشتر اطلاعات شبکه رو میده.

Imports System.Net
Imports System.Net.NetworkInformation
Imports System.Globalization

Public Class NetworkInformation

Dim networkInterfaces() As NetworkInterface
Dim currentInterface As NetworkInterface

Delegate Sub NetworkAddressChangedCallback()
Delegate Sub NetworkAvailabilityCallback( _
ByVal isNetworkAvailable As Boolean)

Private Sub NetworkInformation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler System.Net.NetworkInformation.NetworkChange.Networ kAvailabilityChanged, AddressOf Me.networkChange_NetworkAvailabilityChanged
AddHandler System.Net.NetworkInformation.NetworkChange.Networ kAddressChanged, AddressOf Me.networkChange_NetworkAddressChanged
networkInterfaces = System.Net.NetworkInformation.NetworkInterface.Get AllNetworkInterfaces
UpdateNetworkAvailability(System.Net.NetworkInform ation.NetworkInterface.GetIsNetworkAvailable())
UpdateNetworkInformation()
End Sub

Private Sub networkChange_NetworkAvailabilityChanged( _
ByVal sender As Object, ByVal e As NetworkAvailabilityEventArgs)
Me.Invoke( _
New NetworkAvailabilityCallback( _
AddressOf UpdateNetworkAvailability), _
New Object() {e.IsAvailable})
End Sub

Private Sub networkChange_NetworkAddressChanged( _
ByVal sender As Object, ByVal e As EventArgs)
Me.Invoke( _
New NetworkAddressChangedCallback( _
AddressOf UpdateNetworkInformation))
End Sub

Private Sub UpdateNetworkInformation()
networkInterfaces = NetworkInterface.GetAllNetworkInterfaces()
networkInterfacesComboBox.Items.Clear()
For Each nic As NetworkInterface In networkInterfaces
networkInterfacesComboBox.Items.Add(nic.Descriptio n)
Next
If networkInterfaces.Length = 0 Then
networkInterfacesComboBox.Items.Add( _
"No NICs found on the machine.")
Else
currentInterface = networkInterfaces(0)
UpdateCurrentNicInformation()
End If
networkInterfacesComboBox.SelectedIndex = 0
End Sub

Private Sub UpdateCurrentNicInformation()
Dim ipProperties As IPInterfaceProperties = _
currentInterface.GetIPProperties()
dnsSuffixTextLabel.Text = ipProperties.DnsSuffix.ToString()
addressListView.Items.Clear()
Dim anycastInfo As IPAddressInformationCollection = _
ipProperties.AnycastAddresses
For Each info As IPAddressInformation In anycastInfo
InsertAddress(info.Address, "Anycast")
Next
Dim unicastInfo As UnicastIPAddressInformationCollection = _
ipProperties.UnicastAddresses
For Each info As UnicastIPAddressInformation In unicastInfo
InsertAddress(info.Address, "Unicast")
Next
Dim multicastInfo As MulticastIPAddressInformationCollection = _
ipProperties.MulticastAddresses
For Each info As MulticastIPAddressInformation In multicastInfo
InsertAddress(info.Address, "Multicast")
Next
Dim gatewayInfo As GatewayIPAddressInformationCollection = _
ipProperties.GatewayAddresses
For Each info As GatewayIPAddressInformation In gatewayInfo
InsertAddress(info.Address, "Gateway")
Next

Dim ipAddresses As IPAddressCollection = _
ipProperties.WinsServersAddresses
InsertAddresses(ipAddresses, "WINS Server")
ipAddresses = ipProperties.DhcpServerAddresses
InsertAddresses(ipAddresses, "DHCP Server")
ipAddresses = ipProperties.DnsAddresses
InsertAddresses(ipAddresses, "DNS Server")
End Sub

Private Sub InsertAddresses( _
ByVal ipAddresses As IPAddressCollection, _
ByVal addressType As String)
For Each address As IPAddress In ipAddresses
InsertAddress(address, addressType)
Next
End Sub

Private Sub InsertAddress( _
ByVal address As IPAddress, _
ByVal addressType As String)
Dim listViewInformation(2) As String
listViewInformation(0) = address.ToString()
listViewInformation(1) = addressType

Dim item As ListViewItem = New ListViewItem(listViewInformation)
addressListView.Items.Add(item)
End Sub

Private Sub UpdateNetworkAvailability( _
ByVal isNetworkAvailable As Boolean)
If isNetworkAvailable Then
networkAvailabilityTextLabel.Text = _
"At least one network interface is up."
Else
networkAvailabilityTextLabel.Text = _
"The network is not currently available."
End If
End Sub

Private Sub networkInterfacesComboBox_SelectedIndexChanged( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles networkInterfacesComboBox.SelectedIndexChanged
currentInterface = _
networkInterfaces(networkInterfacesComboBox.Select edIndex)
UpdateCurrentNicInformation()
End Sub

Private Sub updateInfoTimer_Tick( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles updateInfoTimer.Tick
UpdateNicStats()
End Sub

Private Sub UpdateNicStats()
Dim ipStats As IPv4InterfaceStatistics = _
currentInterface.GetIPv4Statistics()

Dim numberFormat As NumberFormatInfo = _
NumberFormatInfo.CurrentInfo
Dim bytesReceivedInKB As Long = ipStats.BytesReceived / 1024
Dim bytesSentInKB As Long = ipStats.BytesSent / 1024

speedTextLabel.Text = GetSpeedString(currentInterface.Speed)
bytesReceivedTextLabel.Text = _
bytesReceivedInKB.ToString("N0", numberFormat) + " KB"
bytesSentTextLabel.Text = _
bytesSentInKB.ToString("N0", numberFormat) + " KB"

operationalStatusTextLabel.Text = _
currentInterface.OperationalStatus.ToString()
supportsMulticastTextLabel.Text = _
currentInterface.SupportsMulticast.ToString()
End Sub

Private Shared Function GetSpeedString(ByVal speed As Long) As String
Select Case speed
Case 10000000
GetSpeedString = "10 MB"
Case 11000000
GetSpeedString = "11 MB"
Case 54000000
GetSpeedString = "54 MB"
Case 100000000
GetSpeedString = "100 MB"
Case 1000000000
GetSpeedString = "1 GB"
Case Else
GetSpeedString = speed.ToString(NumberFormatInfo.CurrentInfo)
End Select

End Function

End Class

azygole
شنبه 12 فروردین 1385, 13:35 عصر
با سلام.
جناب آقای حیدری با تشکر فراوان از پاسخ جامع شما . اما متاسفانه من یک برنامه نویس مبتدی در زمینه #C هستم . می توانم خواهش کنم کد فوق را به صورت #C برگردانید تا من متوجه آن شوم؟ البته روی کد فوق هم کار کردم اما نتوانستم به نتیجه مطلوبم برسم . با تشکر فراوان از شما .

azygole
دوشنبه 14 فروردین 1385, 10:14 صبح
کاش یک نفر #C بلد بود جواب منو میداد.

once4ever
دوشنبه 14 فروردین 1385, 10:54 صبح
نکته اول اینکه جناب m-heidari ازاین به بعد کدهاتونو با علامت [ code ] http://www.barnamenevis.org/forum/images/editor/code.gif مشخص کنید که درست نشون داده بشه.
برای پاسخ شما هم باتوجه به کدی که هست احتمالا باید از کلاس NetworkInterface استفاده کنی. ( من الان ویژوال دات نت ندارم که اینو چک کنم)
یک عضو از کلاس NetworkInterface درست کن و ببین شاید همچین تابعی داشته باشه: GetIPProperties
البته using System.Net.NetworkInformation یادت نره.

azygole
دوشنبه 14 فروردین 1385, 11:12 صبح
سلام خسته نباشید .
فقط using System.Net.Sockets و using System.Net را دارد . GetIPProperties را هم ندارد . میشه چک کنید جوابشو به من در #C بدین . آفرین دستتون درد نکنه . خیلی به این جواب احتیاج دارم .

once4ever
دوشنبه 14 فروردین 1385, 11:37 صبح
از using System.Net استفاده کن و ببین این کد کار میکنه؟


foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
foreach (UnicastIPAddressInformation ipaddress in nic.GetIPProperties().UnicastAddresses)
{
//your code
}
}

habedijoo
دوشنبه 14 فروردین 1385, 12:34 عصر
دوست عزیز من در قسمت وی بی دات نت یه مثال کامل گذاشتم . برو سرچ بزن پیداش کن . خیلی راحت میتونی تبدیلش کنی به سی شارپ .