Dns Error Gethostbyname
Contents |
send some data over a socket connection. The user can specify the destination by name or dns.gethostname c# by IP address. I was using syntax like the following system.net.dns.gethostname powershell to get the address IPAddress addr = Dns.GetHostByName(host).AddressList[0];IPEndPoint endPoint = new IPEndPoint(addr, 9100); That worked, but
C# Dns Gethostentry
VS2010 spits out the following warning: ‘System.Net.Dns.GetHostByName(string)' is obsolete: ‘"GetHostByName is obsoleted for this type, please use GetHostEntry instead. http://go.microsoft.com/fwlink/?linkid=14202″‘ So I replaced the call
Msdn Gethostname
to GetHostByName with GetHostEntry. When I passed in the IP address as a string GetHostByName, it threw an error, "No such host is known". That's not good. I didn't want to use obsolete code, but the recommended replacement wasn't working. I did a bit of searchnng on the Internets and iphostentry c# found that GetHostEntry attempts to do a DNS reverse resolve and that doesn't always work. As it turns out, GetHostEntry is not the only method that can be substituted for GetHostByName. GetHostAddresses will return the IP address for the specified host. I was able to use the following code without any warnings: IPAddress addr = Dns.GetHostAddresses(host)[0];IPEndPoint endPoint = new IPEndPoint(addr, 9100); And we are good. Share this:Click to email this to a friend (Opens in new window)Click to share on Facebook (Opens in new window)Click to share on LinkedIn (Opens in new window)Click to share on Reddit (Opens in new window)Click to share on Twitter (Opens in new window)Click to share on Google+ (Opens in new window)Click to print (Opens in new window)Like this:Like Loading... Related Post navigation Previous PostAn odd way to postNext PostAn open letter to Newport Television 2 thoughts on “Replacing GetHostByName with GetHostAddresses” zooplah says: Marc
Generators References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Context options and parameters Supported Protocols and Wrappers Security Introduction General considerations Installed as
Vb.net Gethostentry
CGI binary Installed as an Apache module Session Security Filesystem Security gethostbyname deprecated Database Security Error Reporting Using Register Globals User Submitted Data Magic Quotes Hiding PHP Keeping Current gethostbyname localhost Features HTTP authentication with PHP Cookies Sessions Dealing with XForms Handling file uploads Using remote files Connection handling Persistent Database Connections Safe Mode Command line usage http://www.rajapet.com/2011/11/replacing-gethostbyname-with.html Garbage Collection DTrace Dynamic Tracing Function Reference Affecting PHP's Behaviour Audio Formats Manipulation Authentication Services Command Line Specific Extensions Compression and Archive Extensions Credit Card Processing Cryptography Extensions Database Extensions Date and Time Related Extensions File System Related Extensions Human Language and Character Encoding Support Image Processing and Generation Mail Related Extensions Mathematical http://php.net/manual/en/function.gethostbyname.php Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable and Type Related Extensions Web Services Windows Only Extensions XML Manipulation Keyboard Shortcuts? This help j Next menu item k Previous menu item g p Previous man page g n Next man page G Scroll to bottom g g Scroll to top g h Goto homepage g s Goto search(current page) / Focus search box gethostbynamel » « gethostbyaddr PHP Manual Function Reference Other Services Network Network Functions Change language: English Brazilian Portuguese Chinese (Simplified) French German Japanese Korean Romanian Russian Spanish Turkish Other Edit Report a Bug gethostbyname (PHP 4, PHP 5, PHP 7)gethostbyname — Get the IPv4 address corresponding to a given Internet host name Description string gethostbyname ( string $hostname ) Returns the IPv4 address of the Internet host specified by hostname. Parameters hostname The host name. Return Values Returns the IPv4
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or http://stackoverflow.com/questions/5754010/gethostbyname-error-with-http-link-to-get-ip-address posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss http://stackoverflow.com/questions/9476897/difference-between-gethostentry-and-gethostbyname Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up GetHostByName error with http link to get IP address up vote 0 down vote favorite Hi I have a requirement where extract the IP from URL. The following code is working in one case: string str2 dns error = "www.google.com"; IPHostEntry ip = Dns.GetHostByName(str2); IPAddress [] IpA = ip.AddressList; for (int i = 0; i < IpA.Length; i++) { Console.WriteLine ("IP Address {0}: {1} ", i, IpA[i].ToString ()); } But if change the URL as string str2 = "http://google.com"; GetHostByName is throwing exception. What method I should use which work in both cases? c# .net share|improve this question edited Apr 22 '11 at 9:41 Oscar Mederos 13.7k1546100 asked Apr 22 '11 at 9:18 CrazyC 73031841 Depend on dns error gethostbyname the answer's i got. –CrazyC Apr 22 '11 at 9:44 add a comment| 1 Answer 1 active oldest votes up vote 5 down vote You could use Uri.IsWellFormedUriString method to determine whether the str2 is well-formed and then get the host only: string str2 = "http://www.google.com"; if (Uri.IsWellFormedUriString(str2, UriKind.Absolute)) { str2 = new Uri(str2).Host; } var host = Dns.GetHostEntry(str2); also MSDN says that Dns.GetHostByName is obsolete, and you should use Dns.GetHostEntry instead. share|improve this answer answered Apr 22 '11 at 9:30 Alex 24.4k65097 1 +1 for Dns.GetHostByName obsolescence. –as-cii Apr 22 '11 at 10:04 /agree with @AS-CII –Chris Barlow Jan 19 '12 at 13:49 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer, you agree to the privacy policy and terms of service. Not the answer you're looking for? Browse other questions tagged c# .net or ask your own question. asked 5 years ago viewed 537 times active 5 years ago Related 410Get a new object instance from a Type488How do I get the path of the assembly the code is in?1561How to get a consistent byte representation of strings in C# without manually specifying an encoding?423How to get the type of T from a generic List
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Difference between GetHostEntry and GetHostByName? up vote 4 down vote favorite On MSDN it mentions GetHostByName is obsolete. The replacement is GetHostEntry. What are their difference? c# dns gethostbyname share|improve this question asked Feb 28 '12 at 5:33 KMC 7,01528104194 add a comment| 1 Answer 1 active oldest votes up vote 5 down vote accepted It looks like GetHostEntry does a little more error checking and also supports Network Tracing GetHostByName Decompiled: public static IPHostEntry GetHostByName(string hostName) { if (hostName == null) throw new ArgumentNullException("hostName"); Dns.s_DnsPermission.Demand(); IPAddress address; if (IPAddress.TryParse(hostName, out address)) return Dns.GetUnresolveAnswer(address); else return Dns.InternalGetHostByName(hostName, false); } GetHostEntry Decompiled: public static IPHostEntry GetHostEntry(string hostNameOrAddress) { if (Logging.On) Logging.Enter(Logging.Sockets, "DNS", "GetHostEntry", hostNameOrAddress); Dns.s_DnsPermission.Demand(); if (hostNameOrAddress == null) throw new ArgumentNullException("hostNameOrAddress"); IPAddress address; IPHostEntry ipHostEntry; if (IPAddress.TryParse(hostNameOrAddress, out address)) { if (((object) address).Equals((object) IPAddress.Any) || ((object) address).Equals((object) IPAddress.IPv6Any)) throw new ArgumentException(SR.GetString("net_invalid_ip_addr"), "hostNameOrAddress"); ipHostEntry = Dns.InternalGetHostByAddress(address, true); } else ipHostEntry = Dns.InternalGetHostByName(hostNameOrAddress, true); if (Logging.On) Logging.Exit(Logging.Sockets, "DNS", "GetHostEntry", (object) ipHostEntry); return ipHostEntry; } share|improve this answer answered Apr 12 '12 at 20:35 F