473,405 Members | 2,421 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,405 software developers and data experts.

determining IP Addresses at runtime

How can i determine IP Address of my machine at runtime without
providing any information like HostName?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #1
11 4983
JR
Hi,

using System.Net;

IPHostEntry IPAddr = Dns.GetHostByName(Dns.GetHostName());

IPAddr.AddressList is then an array of IP addresses associated with the
local machine.

John.
"Paul Fi" <na*******@hotmail.com> wrote in message
news:ud**************@TK2MSFTNGP12.phx.gbl...
How can i determine IP Address of my machine at runtime without
providing any information like HostName?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #2
Interestingly enough, this information is totally wrong.

Thomas Tomiczek
THONA Software & Consulting Ltd.
(Microsoft MVP C#/.NET)

"JR" <moo> wrote in message
news:3f**********************@news-text.dial.pipex.com...
Hi,

using System.Net;

IPHostEntry IPAddr = Dns.GetHostByName(Dns.GetHostName());

IPAddr.AddressList is then an array of IP addresses associated with the
local machine.

John.
"Paul Fi" <na*******@hotmail.com> wrote in message
news:ud**************@TK2MSFTNGP12.phx.gbl...
How can i determine IP Address of my machine at runtime without
providing any information like HostName?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 15 '05 #3
Works fine on my machine, gives the same address given by ipconfig.

Austin

On Tue, 6 Jan 2004 00:31:29 +0100, "Thomas Tomiczek [MVP]"
<t.********@thona-consulting.com> wrote:
Interestingly enough, this information is totally wrong.

Thomas Tomiczek
THONA Software & Consulting Ltd.
(Microsoft MVP C#/.NET)

"JR" <moo> wrote in message
news:3f**********************@news-text.dial.pipex.com...
Hi,

using System.Net;

IPHostEntry IPAddr = Dns.GetHostByName(Dns.GetHostName());

IPAddr.AddressList is then an array of IP addresses associated with the
local machine.

John.
"Paul Fi" <na*******@hotmail.com> wrote in message
news:ud**************@TK2MSFTNGP12.phx.gbl...
> How can i determine IP Address of my machine at runtime without
> providing any information like HostName?
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!



Nov 15 '05 #4
Yeah it works thanks guyz!
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #5
"Austin Ehlers" <th***********************@hotmail.com> wrote in
message news:uo********************************@4ax.com
Works fine on my machine, gives the same address given by ipconfig.


I tried that and got very mixed results; it works provided the system is
listed in DNS and the DNS has been updated and unfortunately neither
assumption is reliable especially in the case of my laptop which has an
internal NIC, a PCMCIA NIC, a VPN connection and a NIC in the docking
station.

This appears to be working for me:

using mgt=System.Management;

public System.Collections.ArrayList IPAddresses() {
mgt.PropertyData ipaddr;
mgt.PropertyData gw;
System.Collections.ArrayList IPs=new System.Collections.ArrayList(5);
string sQuery="SELECT IPAddress,DefaultIPGateway from
Win32_NetworkAdapterConfiguration " +
"WHERE IPEnabled=true";
mgt.ManagementObjectSearcher oWMI = new sm.ManagementObjectSearcher(sQuery);
mgt.ManagementObjectCollection oIPs = oWMI.Get();
foreach (sm.ManagementObject mo in oIPs) {
ipaddr = mo.Properties["IPAddress"];
gw=mo.Properties["DefaultIPGateway"];
if (ipaddr.IsLocal && gw.Value!=null) {
if (ipaddr.IsArray) {
string[] sTemp=(string[])ipaddr.Value;
for (int k=0;k<=sTemp.GetUpperBound(0);k++) IPs.Add(sTemp[k]);
}
else
IPs.Add(ipaddr.Value.ToString());
}
}
return IPs;
}
--
C# newbie... posts are probably inaccurate, inelegant or both

Nov 15 '05 #6
can u please make a brief explaination on the code and also what object
does "sm" belong to?


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #7
"Paul Fi" <na*******@hotmail.com> wrote in message
news:O1**************@TK2MSFTNGP12.phx.gbl
can u please make a brief explaination on the code and also what
object does "sm" belong to?


sorry, copy & pasted bits out of longer code and simplified a bit and missed
a change. the 'sm' should be 'mgt'

it uses WMI to get a list of IP address and gateway address for all network
connections; it then checks that the ip address IsLocal and that a gateway
exists (for NICs that are not connected I'm seeing the gateway is null).
what I get back is an arraylist of IP addresses

--
C# newbie... posts are probably inaccurate, inelegant or both

Nov 15 '05 #8
do u believe that ur solution is better than the one mentioned b4?if so
in what way?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #9
Simple:

It does not make the stupid assunmption that a ystem that normally is under
external control and may not show your data (the DNS used by your computer)
is actually up to date.

Especially customers using a dial up DNS connection may be surprised.

What the "original solution" (which is a typical "I don't care what reality
says, it works here") returned were NOT the IP addresses that the computer
had.

THe original solution did this:

* Get the name of the computer.
* Ask the DNS name service available for the computer for the IP Addresses
attached to this name.

The second step basically has VERY HIGH failure tolerance, especially in
non-professional environments and ISP scenarios.

The second solution (there is a third, using P/IVOKE to the IpHelper library
and just getting all ip addresses) was using WMI (Windows Management
Instrumentation) to actually ask the local NETWORK CARDS and the TCP IP
STACK bound to them for the addresses registered.

Technically this is a valid solution.

See it like a court case :-)

The second solution was asking a witness what he knows about the case.
Legally valid - the network cards basically have to know what their IP
addresses are).

The first solution would be thrown out as hearsay - basically going out and
asking a stranger what he things about a topi in question. Becuase there is
no requirement that:

* There IS A DNS Server.
* The DNS Server is updated.
* The DNS Server contains the correct information.

and then your software suddenly fails, and basically you told the world you
are not knowing what you are doing. Interesting enough, as I said earlier,
this WILL fail often - basically all dialup internet IP addresses do not get
the host name updates (what the heck does the ISP care about the host name
you tend to enter into your computer?)

"It works here" is for me a reson to fire people. WHen people don#t to their
homwork and read the documentation but then tell ime it works on their
computer - they should server burgers ad McDonalds.

Reead documentation, guys.

When you are asked to return a list of local IP Addresses, do this. Do not
rely on undocumented behavior and assume it will just work.

And yes, btw - I HAVE seen cases (we still try to repro them) where I
actually did get an EXCEPTION on the initial query - with some ISP's at some
times.

Thomas Tomiczek
THONA Software & Consulting Ltd.
(Microsoft MVP C#/.NET)

"Paul Fi" <na*******@hotmail.com> wrote in message
news:uz**************@TK2MSFTNGP12.phx.gbl...
do u believe that ur solution is better than the one mentioned b4?if so
in what way?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #10
JR
Hello Thomas,

The original solution I posted was in the MSDN documentation, I just
converted it to the same thing in C#. The .NET documentation actually has
examples using the exact same C# code I posted. I have also seen this
solution used in popular open source applications.

Now, I am not an expert Windows programmer I use it only for internal tools
(IOW I never need to worry about it working under all conditions), so I am
not qualified to make any arguments with you about the best way of doing
things. However, anyone who looks in the documentation for a solution to
this problem will almost certainly find this solution first, I did. This has
been reinforced to me many times by seeing other people using the same code.

In any case, Bill's solution is certainly more robust and in my opinion
should be preferred, but it is not something I would've (or have) found just
by browsing through the documentation.

John.
Reead documentation, guys.

Nov 15 '05 #11
Thanks.

Can you please post the exact document URL from the MSDN documentation? I
will have this commented / removed / changed as in error.

Thi should not be in for this question as a possible solution.

(that said: i would prefer they would add a "GetIpAddresses" somewhere).

Thomas Tomiczek
THONA Software & Consulting Ltd.
(Microsoft MVP C#/.NET)

"JR" <moo> wrote in message
news:3f**********************@news-text.dial.pipex.com...
Hello Thomas,

The original solution I posted was in the MSDN documentation, I just
converted it to the same thing in C#. The .NET documentation actually has
examples using the exact same C# code I posted. I have also seen this
solution used in popular open source applications.

Now, I am not an expert Windows programmer I use it only for internal tools (IOW I never need to worry about it working under all conditions), so I am
not qualified to make any arguments with you about the best way of doing
things. However, anyone who looks in the documentation for a solution to
this problem will almost certainly find this solution first, I did. This has been reinforced to me many times by seeing other people using the same code.
In any case, Bill's solution is certainly more robust and in my opinion
should be preferred, but it is not something I would've (or have) found just by browsing through the documentation.

John.
Reead documentation, guys.


Nov 15 '05 #12

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

Similar topics

1
by: cpp_weenie | last post by:
Hello group, Should the program below be valid? Or should it generate a compilation error? Any references to the Standard would be appreciated... Thanks! #include <iostream>
5
by: Lyn | last post by:
Is there any way of determining in VBA the name and arguments of the currently executed procedure? To assist in debugging, I would like to be able to trace the procedures as they are executed. ...
2
by: Clinton Pierce | last post by:
I've filled a DataTable with columns that have custom type (a class that I'm using to keep track of other things, not just a value). When the .Select method goes to sort this column, how do I let...
5
by: John Hardin | last post by:
All: Is it possible at runtime to determine the names of all classes derived from a given class? -- John Hardin KA7OHZ <johnh@aproposretail.com> Internal Systems...
0
by: CTDev Team | last post by:
Hi, We are using Exchange Server 5.5, and have applications written in VB6 and C# that read and process emails. We are experiencing intermittent errors similar to C# Application ...
2
by: Garrett | last post by:
Need any help in determining which groups have been given security access to a folder. Searched DirectoryServices to no avail... Any Help?
0
by: John Puopolo | last post by:
All: This *should* be a simple question, but I cannot find a definitive answer on the net so.... I have both versions 1.1 and versions 2.0 of the CLR/.NET installed on my machine. I want to...
3
by: VB Programmer | last post by:
I have an ASPX page where I send out emails through my mail server mail.MyDomain.com. When I send emails to MyName@MyDomain.com it sends PERFECTLY. When I try sending an email to any other address...
9
by: Schraalhans Keukenmeester | last post by:
I have some C functions (with variable length argument lists) that use void pointers as arguments. Is there a way to determine at runtime what type of parameter is actually passed on to the...
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
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: 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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.