FYI: Using two nics (Network Cards) with vb.net

  • Thread starter Thread starter John Lucas
  • Start date Start date
J

John Lucas

I have been reading many of the posts about determining which network
card to use (in a machine with multiple nics), and determining whether
or not that card is active. I'm hoping this will help others who are
struggling with the same issues. This info exists in the groups, but
not all in the same place.

Thanks to everyone who contributes. Well done!

Consider the following code:

m_DataSocket = New Socket(AddressFamily.Unspecified,
SocketType.Stream, _
ProtocolType.Tcp)
Dim localIP as IPAddress =
Dns.Resolve(Dns.GetHostName()).AddressList(0)
m_DataSocket.Bind(new IPEndPoint(localIP, 0))
m_DataSocket.Listen(0)
theSocket = m_DataSocket.Accept()

This works great if you have one nic, and it is connected to the
network.
But if you have two nics, as is the case with many laptops/notebooks,
this may not work.

Why?
"Dns.Resolve(Dns.GetHostName()).AddressList(0)" only gets the ip
address of the first nic in your system. Which nic? who knows.

I have multiple nics in my laptop. I unplug my lan cable, and leave
work. I come home from work, turn on my wireless, and blissfully read
google groups, and code. (Sad, but true)

The "Accept" method now locks up (the app, not my whole machine),
ruining my bliss, because the first address in the list is now
disconnected. I could manually tell my app to use nic#1
(AddressList(0)) or nic#2 (AddressList(1)) , but then it would be a
manual process, and I like it to be automatic.

To get around this, I used The system.management classes with WMI.
It's slow, but effective.

Dim mc As System.Management.ManagementClass
Dim mo As ManagementObject
Private m_ConnectedNics As New ArrayList
Private m_AvailableNics As New ArrayList
Private m_ConnectedIPAddresses As New ArrayList

' Find all the adapters in your machine

mc = New ManagementClass("Win32_NetworkAdapter")
Dim moc As ManagementObjectCollection = mc.GetInstances()
For Each mo In moc

' find out what their statuses (stati?) are

Dim tmpstat = mo("NetConnectionStatus")
If tmpstat Is Nothing Then
Debug.WriteLine("Not Connected " + mo("caption"))
Else
Dim stat As UInt16 = tmpstat
Me.m_AvailableNics.Add(mo("caption"))

If stat.ToString = "2" Then ' see link below for WMI values : 2=
connected
' Voila! it's connected! - add it to the list
Me.m_ConnectedNics.Add(mo("caption"))

' Find the IP address of the nic(s)

Dim mc2 As System.Management.ManagementClass
Dim mo2 As ManagementObject
mc2 = New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim moc2 As ManagementObjectCollection = mc2.GetInstances()
For Each mo2 In moc2
If mo("caption") = mo2("caption") Then
Try
Dim y
For Each y In mo2.Properties("IPAddress").Value

' Build a list of ip addresses that are connected

Me.m_ConnectedIPAddresses.Add(y.ToString)
Next
End If
Next
End If
End If
Next

If anyone knows of a faster way to do this...PLEASE let me know!

Way more info on WMI classes and a list of which ones exist, and
values, etc...
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/wmi_classes.asp

- cab0
 
Back
Top