Example usage for java.net InetAddress getLocalHost

List of usage examples for java.net InetAddress getLocalHost

Introduction

In this page you can find the example usage for java.net InetAddress getLocalHost.

Prototype

public static InetAddress getLocalHost() throws UnknownHostException 

Source Link

Document

Returns the address of the local host.

Usage

From source file:com.bitcup.configurator.Context.java

private String getLocalHostName() {
    String hostname = null;/* www  . j  a v  a 2s .co  m*/
    try {
        hostname = InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
        logger.error("Unable to infer hostname from InetAddress.getLocalHost().getHostName()", e.getMessage());
    }
    return hostname;
}

From source file:gemlite.core.internal.admin.AdminUtil.java

/**
 * ?ip/*from  ww w. j a  va 2 s .c  om*/
 * 
 * @return
 */
public static String getIp() {
    String ip = System.getProperty(ITEMS.BINDIP.name());
    if (StringUtils.isEmpty(ip)) {
        try {
            InetAddress addr = InetAddress.getLocalHost();
            ip = addr.getHostAddress().toString();
            ip += IPSUFFIX; // ???
        } catch (Exception e) {
            ip = e.getMessage();
            LogUtil.getAppLog().error("get Ip error:", e);
        }
    }
    return ip;
}

From source file:com.nridge.core.base.std.Platform.java

/**
 * Convenience method that returns the full qualified domain name of the current machine.
 *
 * @return Fully Qualified Domain Name//from www . ja v  a 2 s.  co m
 */
public static String getFullyQualifiedDomainName() {
    String hostName;

    try {
        InetAddress inetAddress = InetAddress.getLocalHost();
        hostName = inetAddress.getCanonicalHostName();
    } catch (UnknownHostException e) {
        hostName = "localhost";
    }

    return hostName;
}

From source file:org.eclipse.oomph.setup.internal.sync.SyncUtil.java

private static String getWorkstation() {
    try {//from  ww  w  .  j a v a2 s  . c o  m
        return InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
        return null;
    }
}

From source file:com.app.server.node.NodeServer.java

/**
 * This method implements the node server request
 *//*  w  w w. j a  v a  2s  .co  m*/
public void run() {
    //deployer.start();
    try {
        ObjectName objName = new ObjectName("com.app.server:type=deployer,service=RMIDeployer");
        MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
        mbeanServer.createMBean("com.app.server.node.RMIDeployer", objName);
        mbeanServer.invoke(objName, "init", new Object[] { deployerList },
                new String[] { Vector.class.getName() });
        ServerConfig serverConfig = new ServerConfig();
        serverConfig.setRmiregistryport(port + "");
        serverConfig.setCachedir("d:/AppServer/cache");
        mbeanServer.invoke(objName, "init", new Object[] { serviceList, serverConfig, mbeanServer },
                new String[] { Vector.class.getName(), ServerConfig.class.getName(),
                        MBeanServer.class.getName() });
        InetAddress inet = null;
        inet = InetAddress.getLocalHost();
        mbeanServer.invoke(objName, "deploy",
                new Object[] { new URL("file:///D:/AppServer/deploy/rmitest.rmi"), false, null,
                        inet.getHostName() + ":" + port },
                new String[] { URL.class.getName(), boolean.class.getName(), ClassLoader.class.getName(),
                        String.class.getName() });
        //deployer.deploy(new URL("file:///D:/AppServer/deploy/rmitest.rmi"),false,null,inet.getHostName()+":"+port);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.lfv.yada.net.client.ClientNetworkManager.java

public ClientNetworkManager(int terminalId, ClientBundle bundle, SocketAddress serverSocketAddr,
        SocketAddress localhostBindSocketAddr, SocketAddress multicastSocketAddr, int multicastTTL,
        TerminalProperties properties) throws IOException {

    // Create a logger for this class
    log = LogFactory.getLog(getClass());

    // Load terminal properties
    this.properties = properties;

    // Setup stuff
    this.terminalId = terminalId;
    this.bundle = bundle;
    this.serverSocketAddr = new SocketAddress(serverSocketAddr);

    // Resolve local host address
    InetAddress localHost = getLocalHostFix();
    if (localHost == null) {
        localHost = InetAddress.getLocalHost();
        if (localHost == null)
            throw new UnknownHostException("Could not resolve ip address of localhost");
    }/* w w w .j  ava 2  s .  c om*/

    // The socket to be used for sending and receiving unicast packets
    DatagramSocket usocket;
    if (localhostBindSocketAddr.getAddress() == 0)
        usocket = new DatagramSocket();
    else
        usocket = new DatagramSocket(localhostBindSocketAddr.getPort(),
                localhostBindSocketAddr.getInetAddress());

    // The multicast socket
    InetAddress maddr = multicastSocketAddr.getInetAddress();
    int mport = multicastSocketAddr.getPort();
    MulticastSocket msocket = null;

    multicastAvailable = (maddr != null && mport > 0);
    if (multicastAvailable) {
        msocket = new MulticastSocket(mport);
        try {
            msocket.joinGroup(maddr);
            msocket.setTimeToLive(multicastTTL);
        } catch (SocketException ex) {
            log.warn("!!!");
            log.warn("!!! Unable to create multicast socket! Multicasting has been disabled!");
            log.warn("!!! On linux systems a default gateway must be defined, try:");
            log.warn("!!! > route add default gw <some_ip_address>");
            log.warn("!!!");
            msocket = null;
            multicastAvailable = false;
        }
    } else
        log.warn("No multicast address or port defined, multicasting has been disabled!");

    // Store the local unicast ip address and port
    localSocketAddr = new SocketAddress(localHost, usocket.getLocalPort());

    // Create a receiver and a sender (send/recv must use the same port number)
    receiver = new ClientPacketReceiver(terminalId, usocket, msocket);
    sender = new ClientPacketSender(usocket, msocket, multicastSocketAddr);

    // Create a transaction mananger
    transactionManager = new TransactionManager(sender);
    receiver.setControlPacketDispatcher(transactionManager);
    receiver.setSendPacketDispatcher(sender);

    // Create a timer for handling pings
    timer = new Timer("Snetworkmanager", true);

    // Initialize packet pool
    PacketPool.getPool();

    // Setup request handlers
    transactionManager.setRequestHandler(Packet.SESSION, new SessionRequestPacketHandler());
    transactionManager.setRequestHandler(Packet.UPDATE, new UpdateRequestPacketHandler());
    transactionManager.setRequestHandler(Packet.INFO, new InfoRequestPacketHandler());
    transactionManager.setRequestHandler(Packet.ISA, new IsaRequestPacketHandler());
    transactionManager.setRequestHandler(Packet.CONNECT, new ConnectRequestPacketHandler());
    transactionManager.setRequestHandler(Packet.INITIATE, new InitiateRequestPacketHandler());
}

From source file:net.dfs.remote.main.ClientServicesStarter.java

public static String clientName() throws UnknownHostException {
    return InetAddress.getLocalHost().getHostName();
}

From source file:gov.nasa.ensemble.common.CommonUtils.java

public static String getHostname() throws IOException {
    return InetAddress.getLocalHost().getHostName();
}

From source file:com.edmunds.etm.agent.impl.TcpHealthCheck.java

private InetAddress getHostAddress() {
    if (hostAddress == null) {
        try {/*from   ww  w .  j a  v  a  2  s .co  m*/
            if (StringUtils.isEmpty(hostName)) {
                hostAddress = InetAddress.getLocalHost();
            } else {
                hostAddress = InetAddress.getByName(hostName);
            }
        } catch (UnknownHostException e) {
            String message = String.format("Could not get IP address for host %s", hostName);
            logger.error(message, e);
            throw new RuntimeException(message, e);
        }
    }
    return hostAddress;
}

From source file:net.geertvos.theater.core.util.UUIDGen.java

public static InetAddress getLocalAddress() {
    if (localInetAddress_ == null)
        try {/*from w w  w. j av a 2 s  .  c  om*/
            localInetAddress_ = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            return null;
        }
    return localInetAddress_;
}