 |
|
 |
It's just wrong. Relying on DNS to get your local IP addresses has so many gotchas involved that it's scary.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
I'm trying to follow your code however, I'm having some trouble figuring out what code is required to return the IP address.
When I try to use Dns (System.Net namespace), no further methods are being show by VS 2008. Any ideas?
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Net;
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {
}
private string ClientIPAddress { ..er? } }
Environment ASP.NET with .NET 3.5 SP1 VS 2008 SP1 IIS7
Jon
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Thanks, that was useful, however do you have any idea on why VS does not like 'StringCollections'?
Imports System.Net Imports System.Collections
//this is just a guess, however it Module Module1
Sub Main()
End Sub
Private Function GetIPs() As StringCollection Dim localIP = New StringCollection() Dim localHostName = Dns.GetHostName() Dim hostEntry = Dns.GetHostEntry(localHostName) For Each ipAddr As IPAddress In hostEntry.AddressList localIP.Add(ipAddr.ToString()) Next GetIPs = localIP End Function
This simple code snippet seems to work for IPv4 addresses:
Imports System.Net Imports System.Collections
Module Module1
Sub Main() Dim localHostName = Dns.GetHostName() Dim hostEntry = Dns.GetHostEntry(localHostName)
For Each ipAddr As IPAddress In hostEntry.AddressList Console.WriteLine(ipAddr) Next End Sub
Some "fun" with C# / ASP.NET
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Net; using System.Collections;
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { String _localHostName = Dns.GetHostName(); IPHostEntry _hostEntries = Dns.GetHostEntry(_localHostName);
foreach (IPAddress _IPAddress in _hostEntries.AddressList) { ListBox1.Items.Add( _IPAddress.ToString()); }
} }
Environment notes ------------------ Visual Studio 2008 .NET 3.5 SP1 Visual Basic.NET / C#
Related links:
http://social.msdn.microsoft.com/Search/en-US/?query=system.collections.stringcollections&ac=3
Jon
modified on Monday, March 30, 2009 6:37 PM
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
do you know how to code a website like ebay.com other website lke that.contact me at jr_jamesrobert01@yahoo.com
|
| Sign In·View Thread·PermaLink | 1.00/5 (3 votes) |
|
|
|
 |
|
|
 |
|
 |
A way to get the local IP address that would be used to connect to somewhere without having to connect.
So if you have a machine that has a bunch of IP addresses, you can open a TCP connection and then query your IP address from the connection information.
But What I'd like to see is a way you could query the system to give you the local IP address that it would use to connect to a remote address.
-M
|
| Sign In·View Thread·PermaLink | 1.17/5 (3 votes) |
|
|
|
 |
|
 |
I spent quite some time on figuring out why my code (which resembles the code given in this article) wouldn't work on 'all' computers.
The retrieved IP address was "::1". This appears to occur on computers with IPv6 enabled and "::1" is the internal loopback address, which can be used for testing. It is not the IP address which I was looking for.
The array of IPAddress's will have this IPv6 internal loopback address as first 'unusable' entry for IPv6 eabled systems whereas this first entry used to be the address we were looking for. I see a lot of code, also here on codeproject, which relies on the fact that the first IPAddress array entry is OK, thay all just take addr[0] as found IP address.
For your code to be IPv6 'compliant' (not fully, that is quitte something different, but at least compliant for IPv6 tunneling systems) you must check if the found address is the one you looking for.
Check on AddressFamily is not) InterNetworkV6) and IPAddress.IsLoopback.
Hope this saves you my headaches on this issue...
As an example...(a bit of a mess, but it serves it's purpose)....
public string getAddress() { // First get the host name of local machine. String strHostName = ""; strHostName = Dns.GetHostName();
// Then using host name, get the IP address list.. IPHostEntry ipEntry = Dns.GetHostEntry(strHostName); IPAddress[] addr = ipEntry.AddressList;
log("Local host name: " + strHostName); // address must be IpV4 (for our appl.) and not the loopback address for (int i = 0; i < addr.Length; i++) { log(" IP Address[" + i + "]: " + addr[i].ToString()); if (addr[i].AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) { log("-----!! IpV6 address"); } if (IPAddress.IsLoopback(addr[i])) { log("-----!! loopback address"); } }
string selectedAddress = String.Empty; for (int i = 0; i < addr.Length; i++) { if (addr[i].AddressFamily != System.Net.Sockets.AddressFamily.InterNetworkV6) { if (!IPAddress.IsLoopback(addr[i])) { selectedAddress = addr[i].ToString(); log(" using: " + selectedAddress + " (index: " + i + ")"); break; } } }
if (selectedAddress == String.Empty) { log("!!! No useable network addres found !!!"); }
return (selectedAddress); }
|
| Sign In·View Thread·PermaLink | 4.73/5 (6 votes) |
|
|
|
 |
|
|
 |
|
 |
Hai, i want to get system information including all hardware and software devices. i don't have any idea about this module. Can anybody guide me how can i get such data using codings in .net framework
|
| Sign In·View Thread·PermaLink | 1.67/5 (5 votes) |
|
|
|
 |
|
 |
Hi.
I was wondering if there is a way to use the same routine to get all the available IP address in a given LAN using the same codes?
Will someone please show me how? Thanks
|
| Sign In·View Thread·PermaLink | 1.40/5 (15 votes) |
|
|
|
 |
|
 |
I tried running the code below but it doesn't give me the IP address I need for my Barcode Network Printer.
DirectorySearcher srch = new DirectorySearcher(); DirectoryEntry entry = srch.SearchRoot; string domain = (string)entry.Properties["name"][0]; //DC name
DirectoryEntry DomainEntry = new DirectoryEntry("WinNT://" + domain); DomainEntry.Children.SchemaFilter.Add("computer");
// To Get all the System names And Display with the Ip Address foreach (DirectoryEntry machine in DomainEntry.Children) {
string[] Ipaddr = new string[3]; Ipaddr[0] = machine.Name; // string guid = machine.Guid.ToString();
System.Net.IPHostEntry Tempaddr = null; Console.WriteLine(">" + Ipaddr[0]); // + " " + guid); try { //Tempaddr = (System.Net.IPHostEntry)Dns.GetHostByAddress(machine.Name); Tempaddr = (System.Net.IPHostEntry)Dns.GetHostEntry(machine.Name); //GetHostByName( } catch (Exception ex) { // IPx.Add(machine.Name); continue; } System.Net.IPAddress[] TempAd = Tempaddr.AddressList; foreach (IPAddress TempA in TempAd) { Ipaddr[1] = TempA.ToString(); //if (Ipaddr[1].ToString() == "10.25.1.222") { Console.WriteLine("found sato printer."); break; }
byte[] ab = new byte[6]; int len = ab.Length;
// This Function Used to Get The Physical Address int r = SendARP((int)TempA.Address, 0, ab, ref len); string mac = BitConverter.ToString(ab, 0, 6);
Ipaddr[2] = mac;
}
Console.WriteLine("\t\t" + Ipaddr[2].ToString().Trim() + "\t " + Ipaddr[1].ToString().Trim() + "\t\t " + Ipaddr[0].ToString().Trim() + " ");
}
I believe this code is all about searching for the assigned computer ip connected to the network. But my real question is,Is there a code in C# that actually searches for all the assigned network including hosts addresses? Its more than a week since I have been trying to solve this problem..
Can anyone willing to challenge my question?
Thank you and Happy New Year to all!!
you can also email me at janverge@gmail.com
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
 |
first of all thank you very much,
you said "The information returned includes multiple IP addresses and aliases if the host specified has more than one entry in the DNS database"
One day I have queried the DNS and returned more than one IP, my machine was then named "TOSHIBA", I changed it to some thing like "my000000" then I logged in to the network and query the DNS again and the result was only one IP which is my machine IP address. How do you explain this and how can I identify my machine IP in case it returns more than one IP.
|
| Sign In·View Thread·PermaLink | 2.29/5 (6 votes) |
|
|
|
 |
|
 |
Hello..
i was woundering how can i make my computer a client and a server at the same time, and how can i make sure it works?  
|
| Sign In·View Thread·PermaLink | 2.00/5 (3 votes) |
|
|
|
 |
|
|
 |
|
|
 |
|
 |
Is it possible to know if an IP address is associated with a LAN or a direct cable connection( for e.g. a USB host to host connection)
|
| Sign In·View Thread·PermaLink | 2.20/5 (5 votes) |
|
|
|
 |
|
 |
This code would not work after copy/paste into framework 2.0 They changed class name from DNS to Dns
renton
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |