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:MulticastSender.java

public static void main(String[] args) {

    InetAddress ia = null;//  w  w  w  .  j a va 2  s . co m
    int port = 0;
    String characters = "Here's some multicast data\n";
    byte[] data = new byte[characters.length()];

    // read the address from the command line
    try {
        try {
            ia = InetAddress.getByName(args[0]);
        } catch (UnknownHostException e) {
            //ia = InetAddressFactory.newInetAddress(args[0]);
        }
        port = Integer.parseInt(args[1]);
    } catch (Exception e) {
        System.err.println(e);
        System.err.println("Usage: java MulticastSender MulticastAddress port");
        System.exit(1);
    }

    characters.getBytes(0, characters.length(), data, 0);
    DatagramPacket dp = new DatagramPacket(data, data.length, ia, port);

    try {
        MulticastSocket ms = new MulticastSocket();
        ms.joinGroup(ia);
        for (int i = 1; i < 10; i++) {
            ms.send(dp, (byte) 1);
        }
        ms.leaveGroup(ia);
        ms.close();
    } catch (SocketException se) {
        System.err.println(se);
    } catch (IOException ie) {
        System.err.println(ie);
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    byte[] ary = new byte[128];
    DatagramPacket pack = new DatagramPacket(ary, 128);

    // read//ww  w. ja  v  a2 s.  c  o  m
    DatagramSocket sock = new DatagramSocket(1111);
    sock.receive(pack);
    String word = new String(pack.getData());
    System.out.println("From: " + pack.getAddress() + " Port: " + pack.getPort());
    System.out.println(word);
    sock.close();
    // write
    sock = new DatagramSocket();
    pack.setAddress(InetAddress.getByName(args[1]));
    pack.setData(args[2].getBytes());
    pack.setPort(1111);
    sock.send(pack);
    sock.close();

}

From source file:Test.java

public static void main(String[] args) throws Exception {
    NetworkInterface networkInterface = NetworkInterface.getByName("net1");

    DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET);

    dc.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    dc.bind(new InetSocketAddress(8080));
    dc.setOption(StandardSocketOptions.IP_MULTICAST_IF, networkInterface);

    InetAddress group = InetAddress.getByName("180.90.4.12");
    MembershipKey key = dc.join(group, networkInterface);
}

From source file:MulticastSniffer.java

public static void main(String[] args) {
    InetAddress ia = null;/*from w  ww  .j  a  v  a  2s.c om*/
    byte[] buffer = new byte[65535];
    DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
    int port = 0;

    // read the address from the command line
    try {
        try {
            ia = InetAddress.getByName(args[0]);
        } catch (UnknownHostException e) {
            System.err.println(e);
        }
        port = Integer.parseInt(args[1]);
    } catch (Exception e) {
        System.err.println(e);
        System.err.println("Usage: java MulticastSniffer mcast-addr port");
        System.exit(1);
    }
    System.out.println("About to join " + ia);

    try {
        MulticastSocket ms = new MulticastSocket(port);
        ms.joinGroup(ia);
        while (true) {
            System.out.println("About to receive...");
            ms.receive(dp);
            String s = new String(dp.getData(), 0, 0, dp.getLength());
            System.out.println(s);
        }
    } catch (SocketException se) {
        System.err.println(se);
    } catch (IOException ie) {
        System.err.println(ie);
    }
}

From source file:UDP0.java

public static void main(String[] args) throws Exception {
    byte[] ary = new byte[128];
    DatagramPacket pack = new DatagramPacket(ary, 128);
    if (args[0].charAt(0) == 'r') {
        // read//from w  ww  . j  av  a2 s . c  o  m
        DatagramSocket sock = new DatagramSocket(1111);
        sock.receive(pack);
        String word = new String(pack.getData());
        System.out.println("From: " + pack.getAddress() + " Port: " + pack.getPort());
        System.out.println(word);
        sock.close();
    } else { // write
        DatagramSocket sock = new DatagramSocket();
        pack.setAddress(InetAddress.getByName(args[1]));
        pack.setData(args[2].getBytes());
        pack.setPort(1111);
        sock.send(pack);
        sock.close();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    MembershipKey key = null;//from   ww  w  .j a  v a  2  s.  c o  m
    DatagramChannel client = DatagramChannel.open(StandardProtocolFamily.INET);

    NetworkInterface interf = NetworkInterface.getByName(MULTICAST_INTERFACE_NAME);
    client.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    client.bind(new InetSocketAddress(MULTICAST_PORT));
    client.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);

    InetAddress group = InetAddress.getByName(MULTICAST_IP);
    key = client.join(group, interf);

    System.out.println("Joined the   multicast  group:" + key);
    System.out.println("Waiting for a  message  from  the" + "  multicast group....");

    ByteBuffer buffer = ByteBuffer.allocate(1048);
    client.receive(buffer);
    buffer.flip();
    int limits = buffer.limit();
    byte bytes[] = new byte[limits];
    buffer.get(bytes, 0, limits);
    String msg = new String(bytes);

    System.out.format("Multicast Message:%s%n", msg);
    key.drop();
}

From source file:be.error.rpi.test.UdpTest.java

public static void main(String[] args) throws Exception {
    final InetAddress IPAddress = InetAddress.getByName("192.168.0.10");
    final DatagramSocket clientSocket = new DatagramSocket();

    new Thread() {
        @Override//from   w w w. java  2s . c o  m
        public void run() {
            try {
                while (true) {
                    String s = "0:0:0:";
                    DatagramPacket sendPacket = new DatagramPacket(s.getBytes(), s.getBytes().length, IPAddress,
                            8000);
                    clientSocket.send(sendPacket);
                    Thread.sleep(100);
                }
            } catch (Exception e) {

            }
        }
    }.start();

    new Thread() {
        @Override
        public void run() {
            try {
                while (true) {
                    String s = "1:1:1:";
                    DatagramPacket sendPacket = new DatagramPacket(s.getBytes(), s.getBytes().length, IPAddress,
                            8000);
                    clientSocket.send(sendPacket);
                    Thread.sleep(100);
                }
            } catch (Exception e) {

            }
        }
    }.start();

}

From source file:com.googlecode.shutdownlistener.ShutdownUtility.java

public static void main(String[] args) throws Exception {
    final ShutdownConfiguration config = ShutdownConfiguration.getInstance();

    final String command;
    if (args.length > 0) {
        command = args[0];/* w w  w  . jav a  2 s  . c o  m*/
    } else {
        command = config.getStatusCommand();
    }

    System.out.println("Calling " + config.getHost() + ":" + config.getPort() + " with command: " + command);

    final InetAddress hostAddress = InetAddress.getByName(config.getHost());
    final Socket shutdownConnection = new Socket(hostAddress, config.getPort());
    try {
        shutdownConnection.setSoTimeout(5000);
        final BufferedReader reader = new BufferedReader(
                new InputStreamReader(shutdownConnection.getInputStream()));
        final PrintStream writer = new PrintStream(shutdownConnection.getOutputStream());
        try {
            writer.println(command);
            writer.flush();

            while (true) {
                final String line = reader.readLine();
                if (line == null) {
                    break;
                }

                System.out.println(line);
            }
        } finally {
            IOUtils.closeQuietly(reader);
            IOUtils.closeQuietly(writer);
        }
    } finally {
        try {
            shutdownConnection.close();
        } catch (IOException ioe) {
        }
    }

}

From source file:examples.fwhois.java

public static final void main(String[] args) {
    int index;//from w w  w .  j  a v  a2 s .c o m
    String handle, host;
    InetAddress address = null;
    WhoisClient whois;

    if (args.length != 1) {
        System.err.println("usage: fwhois handle[@<server>]");
        System.exit(1);
    }

    index = args[0].lastIndexOf("@");

    whois = new WhoisClient();
    // We want to timeout if a response takes longer than 60 seconds
    whois.setDefaultTimeout(60000);

    if (index == -1) {
        handle = args[0];
        host = WhoisClient.DEFAULT_HOST;
    } else {
        handle = args[0].substring(0, index);
        host = args[0].substring(index + 1);
    }

    try {
        address = InetAddress.getByName(host);
    } catch (UnknownHostException e) {
        System.err.println("Error unknown host: " + e.getMessage());
        System.exit(1);
    }

    System.out.println("[" + address.getHostName() + "]");

    try {
        whois.connect(address);
        System.out.print(whois.query(handle));
        whois.disconnect();
    } catch (IOException e) {
        System.err.println("Error I/O exception: " + e.getMessage());
        System.exit(1);
    }
}

From source file:examples.unix.fwhois.java

public static void main(String[] args) {
    int index;//from  ww w  . j a  v a 2  s  .c  om
    String handle, host;
    InetAddress address = null;
    WhoisClient whois;

    if (args.length != 1) {
        System.err.println("usage: fwhois handle[@<server>]");
        System.exit(1);
    }

    index = args[0].lastIndexOf("@");

    whois = new WhoisClient();
    // We want to timeout if a response takes longer than 60 seconds
    whois.setDefaultTimeout(60000);

    if (index == -1) {
        handle = args[0];
        host = WhoisClient.DEFAULT_HOST;
    } else {
        handle = args[0].substring(0, index);
        host = args[0].substring(index + 1);
    }

    try {
        address = InetAddress.getByName(host);
        System.out.println("[" + address.getHostName() + "]");
    } catch (UnknownHostException e) {
        System.err.println("Error unknown host: " + e.getMessage());
        System.exit(1);
    }

    try {
        whois.connect(address);
        System.out.print(whois.query(handle));
        whois.disconnect();
    } catch (IOException e) {
        System.err.println("Error I/O exception: " + e.getMessage());
        System.exit(1);
    }
}