001package com.hfg.util; 002 003import java.net.InetAddress; 004import java.net.UnknownHostException; 005 006//------------------------------------------------------------------------------ 007/** 008 * General network utility methods. 009 * 010 * @author J. Alex Taylor, hairyfatguy.com 011 */ 012//------------------------------------------------------------------------------ 013// com.hfg XML/HTML Coding Library 014// 015// This library is free software; you can redistribute it and/or 016// modify it under the terms of the GNU Lesser General Public 017// License as published by the Free Software Foundation; either 018// version 2.1 of the License, or (at your option) any later version. 019// 020// This library is distributed in the hope that it will be useful, 021// but WITHOUT ANY WARRANTY; without even the implied warranty of 022// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 023// Lesser General Public License for more details. 024// 025// You should have received a copy of the GNU Lesser General Public 026// License along with this library; if not, write to the Free Software 027// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 028// 029// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 030// jataylor@hairyfatguy.com 031//------------------------------------------------------------------------------ 032 033public class NetworkUtil 034{ 035 036 //########################################################################### 037 // PUBLIC FUNCTIONS 038 //########################################################################### 039 040 //-------------------------------------------------------------------------- 041 /** 042 Returns the fully qualified name of the local host. 043 * @return the fully qualified name of the local host. 044 */ 045 public static String getFullyQualifiedDomainName() 046 { 047 String qualifiedHostname; 048 049 try 050 { 051 InetAddress addr = InetAddress.getLocalHost(); 052 qualifiedHostname = addr.getCanonicalHostName(); 053 } 054 catch (UnknownHostException e) 055 { 056 throw new RuntimeException("Problem while determining the fully qualified host name!", e); 057 } 058 059 return qualifiedHostname; 060 } 061 062 //-------------------------------------------------------------------------- 063 public static String getHostNameForIPAddress(String inIPAddress) 064 { 065 String hostname = null; 066 067 try 068 { 069 InetAddress addr = InetAddress.getByName(inIPAddress); 070 hostname = addr.getHostName(); 071 } 072 catch (UnknownHostException e) 073 { 074 // Ignore 075 } 076 077 return hostname; 078 } 079}