473,395 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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/de...mi_classes.asp

- cab0
Jul 21 '05 #1
0 4524

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Baihao Yuan | last post by:
Hello, I have a machine, which has two network cards, network card 1 and network card 2, they are on different subnetworks. The IP address of network card 1 is 192.0.0.1, the IP address of...
3
by: hitchiker | last post by:
hello i have a little difficult problem searchengines could not help me yet i have 2 zigbee (standard for wireless personal area networking, such as bluetooth) cards, connected to the pc's via...
0
by: John Lucas | last post by:
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...
35
by: keerthyragavendran | last post by:
hi i'm downloading a single file using multiple threads... how can i specify a particular range of bytes alone from a single large file... for example say if i need only bytes ranging from...
2
by: bhowerton | last post by:
Hello, I am trying to setup an office LAN. It will be a small network at first. I have one PC serving as a Windows 2003 Server (this machine has 2 Ethernet cards installed). I also have a D-Link...
0
by: amir | last post by:
Hello All, I am currently looking for writing an application that will act like a network bridge. Meaning, I have 2 network cards and i want all thing from 1 card will pass directly to the other...
10
by: gary0gilbert | last post by:
An unusual spin to this recurring disk or network error in a Terminal Server environment. Access 2000, Terminal Server 2000, file server is windows 2000. All users have a separate copy of the...
5
by: =?Utf-8?B?TWlrZSBE?= | last post by:
This is a difficult question: I need to be able to input data via hand-scanner into a web page textbox, but it needs to be done more than once. The user will scan one card after another and in the...
1
by: =?Utf-8?B?cHdt?= | last post by:
When we write a WCF client to run on a machine with a single network interface card, we create a channel of type T with a statement something like this: T proxy =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.