473,387 Members | 1,863 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,387 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 4522

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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.