The host name is retrieved Based on the IP address in the LAN (C #)

Source: Internet
Author: User
Have you ever encountered this situation? Your Firewall reports that a computer with a certain IP address in the LAN is attacking you, but the firewall does not prompt the name of the computer that is launching the attack, who is the computer under attack? (your computer may be poisoned )? One morning, you just went to work. After you turned on your computer and found that you couldn't connect to the server, you only saw it at the server. Someone used the IP address of the server. Who was using the IP address of the server? NSLookup can be used to reverse Query IP addresses of domain names (host names. Haha, but today we are talking about using C.

1. Obtain the host name based on the IP address ///   <Summary>
/// Obtain the host name based on the IP address
///   </Summary>
///   <Param name = "ip"> Host IP Address </Param>
///   <Returns> Host Name </Returns>
Public   String Gethostnamebyip ( String IP)
{
IP = IP. Trim ();
If (IP =   String . Empty)
Return   String . Empty;
Try
{
// Ping or not
If ( This . Ping (IP ))
{
System. net. iphostentry host = System. net. DNS. gethostentry (IP );
Return Host. hostname;
}
Else
Return   String . Empty;
}
Catch (Exception)
{
Return   String . Empty;
}
}

(If your computer can access the Internet you can even query: IP Address "64.233.189.104" is Google's host named "hk-in-f104.google.com" ip address.

AboutCodeThis. Ping (IP) method later.
Now that we know how to "obtain the host name based on the IP address", we need to talk about how to "obtain the Host IP address based on the host name.

2. Obtain the Host IP address based on the host name.

///   <Summary>
/// Obtain the Host IP address based on the host name (domain name)
///   </Summary>
///   <Param name = "hostname"> Host Name or domain name </Param>
///   <Example> Getipbydomain ("pc001"); getipbydomain ("www.google.com "); </Example>
///   <Returns> Host IP Address </Returns>
Public   String Getipbyhostname ( String Hostname)
{
Hostname = Hostname. Trim ();
If (Hostname =   String . Empty)
Return   String . Empty;
Try
{
System. net. iphostentry host = System. net. DNS. gethostentry (hostname );
Return Host. Addresslist. getvalue ( 0 ). Tostring ();
}
Catch (Exception)
{
Return   String . Empty;
}
}

NOTE: If your computer can access the Internet, you can even find that the IP address of "www.google.com" is "64.233.189.104 ".

Finally, let's talk about C #'s simple Ping function to test whether the network is connected.

3. C # implement the simple Ping function to test whether the network is connected ///   <Summary>
/// Can ping the specified host?
///   </Summary>
///   <Param name = "ip"> IP address, host name, or domain name </Param>
///   <Returns> True or false </Returns>
Public   Bool Ping ( String IP)
{
System. net. networkinformation. Ping P =   New System. net. networkinformation. Ping ();
System. net. networkinformation. pingoptions options =   New System. net. networkinformation. pingoptions ();
Options. dontfragment =   True ;
String Data =   " Test data! " ;
Byte [] Buffer = Encoding. ASCII. getbytes (data );
Int Timeout =   1000 ; // Timeout time, in milliseconds
System. net. networkinformation. pingreply reply = P. Send (IP, timeout, buffer, options );
If (Reply. Status = System. net. networkinformation. ipstatus. Success)
Return   True ;
Else
Return   False ;
}

address: http://www.cnblogs.com/anjou/archive/2007/10/11/920214.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.