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