Example usage for java.net InetAddress getByName

List of usage examples for java.net InetAddress getByName

Introduction

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

Prototype

public static InetAddress getByName(String host) throws UnknownHostException 

Source Link

Document

Determines the IP address of a host, given the host's name.

Usage

From source file:de.pawlidi.openaletheia.utils.DateTimeUtils.java

public static long getCurrentTime() {
    try {//  w w  w . j  av  a 2s  . c o  m
        NTPUDPClient timeClient = new NTPUDPClient();
        InetAddress inetAddress = InetAddress.getByName("time-c.nist.gov");
        TimeInfo timeInfo = timeClient.getTime(inetAddress);
        return timeInfo.getReturnTime();
    } catch (IOException e) {
        // TODO: handle exception
    }
    return -1;
}

From source file:Main.java

public static void sendStopSignal(int port) {
    try {/*from   ww w.  j  a  v a2s.co m*/
        Socket s = new Socket(InetAddress.getByName("127.0.0.1"), port);
        OutputStream out = s.getOutputStream();
        System.err.println("sending server stop request");
        out.write(("\r\n").getBytes());
        out.flush();
        s.close();
    } catch (Exception e) {
        // can happen when called twice by jvm shutdown hook
        System.err.println("stop monitor thread has terminated");
    }
}

From source file:Main.java

static void sendData(byte[] data) throws IOException {
    Log.d(TAG, "sendData data.len:" + data.length);
    while (true) {

        if (multicastSocket == null) {
            multicastSocket = new MulticastSocket(MULTICAST_PORT);
            Log.d(TAG, "new MulticastSocket()");
        }/*ww w  .j a va2 s.  co m*/

        //MulticastSocket multicastSocket = new MulticastSocket(MULTICAST_PORT);
        multicastSocket.setLoopbackMode(true);
        InetAddress group;
        DatagramPacket packet;
        for (int i = 0; i < data.length; i++) {
            Log.d(TAG, i + ":" + data[i]);
            group = InetAddress.getByName("239." + i + "." + data[i] + ".254");

            multicastSocket.joinGroup(group);
            packet = new DatagramPacket("".getBytes(), "".getBytes().length, group, MULTICAST_PORT);
            multicastSocket.send(packet);
            multicastSocket.leaveGroup(group);
        }
    }
}

From source file:NetUtil.java

/**
 * Resolves string ip address and returns host name as a string.
 *//*from w w  w. j a  v  a  2s.  co m*/
public static String resolveIp(String ip) {
    try {
        InetAddress addr = InetAddress.getByName(ip);
        return addr.getHostName();
    } catch (UnknownHostException uhex) {
        return null;
    }
}

From source file:com.thoughtworks.go.domain.IpAddress.java

public static IpAddress create(String address) {
    try {/*from  www  . j a  va2 s .co m*/
        if (StringUtils.isEmpty(address)) {
            return new NullIpAddress();
        }
        return new IpAddress(InetAddress.getByName(address));
    } catch (UnknownHostException e) {
        throw new RuntimeException("IpAddress '" + address + "' is not a valid IP address");
    }
}

From source file:Main.java

public Main() throws IOException {
    serverSocket = new ServerSocket(8008, 1000, InetAddress.getByName("java2s.com"));
    serverSocket.setSoTimeout(10000);/*from  ww w.  j  av a2 s  .c  o m*/

}

From source file:com.edmunds.etm.loadbalancer.impl.OrderedInet4Address.java

private static InetAddress parseAddress(String address) {
    try {/*from www.j a  v a2  s  . c  om*/
        return InetAddress.getByName(address);
    } catch (UnknownHostException e) {
        throw new RuntimeException("Unable to construct OrderedInet4Address", e);
    }
}

From source file:com.predic8.membrane.core.interceptor.acl.Hostname.java

private static InetAddress initV4() {
    try {/*from www. ja va  2 s.co m*/
        //this should probably never fail... unless no network available.
        return InetAddress.getByName("127.0.0.1");
    } catch (UnknownHostException e) {
        e.printStackTrace(); //if you must... maybe logging framework is not set up yet.
        log.error("Failed resolving localhost IPv4 127.0.0.1!");
        return null;
    }
}

From source file:ch.lamacrypt.internal.network.NTP.java

/**
 * Returns the current Unix epoch (in milliseconds), obtained by polling the
 * following NTP server:/*from www  . ja  v  a 2s  .co  m*/
 * <ul>
 * <li>0.ch.pool.ntp.org</li>
 * <li>0.is.pool.ntp.org</li>
 *
 * </ul>
 * <p>
 * Waits up to 10 seconds to get a response
 *
 * @return current Unix epoch, in milliseconds
 */
public static long getTime() {
    NTPUDPClient client = new NTPUDPClient();
    client.setDefaultTimeout(10000);
    hosts[0] = "0.ch.pool.ntp.org";
    hosts[1] = "0.is.pool.ntp.org";
    boolean done = false;
    long epoch = 0;
    int i = 0;

    while (!done) {
        try {
            InetAddress hostAddr = InetAddress.getByName(hosts[i]);
            epoch = client.getTime(hostAddr).getReturnTime();
            done = true;
        } catch (IOException ex) {
            ErrorHandler.showError(ex);
        }
        i++;
    }

    client.close();
    return epoch;
}

From source file:Test.java

private static void serverStart() {
    try {/*from  w w  w. ja  va  2  s  . co m*/
        InetSocketAddress hostAddress = new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 2583);
        AsynchronousServerSocketChannel serverSocketChannel = AsynchronousServerSocketChannel.open()
                .bind(hostAddress);
        Future<AsynchronousSocketChannel> serverFuture = serverSocketChannel.accept();
        final AsynchronousSocketChannel clientSocket = serverFuture.get();
        if ((clientSocket != null) && (clientSocket.isOpen())) {
            InputStream connectionInputStream = Channels.newInputStream(clientSocket);
            ObjectInputStream ois = null;
            ois = new ObjectInputStream(connectionInputStream);
            while (true) {
                Object object = ois.readObject();
                if (object.equals("EOF")) {
                    clientSocket.close();
                    break;
                }
                System.out.println("Received :" + object);
            }
            ois.close();
            connectionInputStream.close();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}