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

public static void main(String[] args) throws Exception {

    int port = 0;
    byte ttl = (byte) 1;

    InetAddress ia = InetAddress.getByName("127.0.0.1");

    byte[] data = "Here's some multicast data\r\n".getBytes();
    DatagramPacket dp = new DatagramPacket(data, data.length, ia, port);

    MulticastSocket ms = new MulticastSocket(InetSocketAddress.createUnresolved("java2s.com", 8080));
    ms.joinGroup(ia);/* w  w w.  j a va2  s . c  o m*/
    for (int i = 1; i < 10; i++) {
        ms.send(dp, ttl);
    }
    ms.leaveGroup(ia);

    System.out.println(ms.getTimeToLive());

    ms.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    int port = 0;
    byte ttl = (byte) 1;

    InetAddress ia = InetAddress.getByName("127.0.0.1");

    byte[] data = "Here's some multicast data\r\n".getBytes();
    DatagramPacket dp = new DatagramPacket(data, data.length, ia, port);

    MulticastSocket ms = new MulticastSocket(InetSocketAddress.createUnresolved("java2s.com", 8080));
    ms.joinGroup(ia);//w  ww  .j a  v  a2 s  . com
    for (int i = 1; i < 10; i++) {
        ms.send(dp, ttl);
    }
    ms.leaveGroup(ia);

    System.out.println(ms.getLoopbackMode());

    ms.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    int port = 0;
    byte ttl = (byte) 1;

    InetAddress ia = InetAddress.getByName("127.0.0.1");

    byte[] data = "Here's some multicast data\r\n".getBytes();
    DatagramPacket dp = new DatagramPacket(data, data.length, ia, port);

    MulticastSocket ms = new MulticastSocket(InetSocketAddress.createUnresolved("java2s.com", 8080));
    ms.joinGroup(ia);/*w w w.j a va 2 s  .c o m*/
    for (int i = 1; i < 10; i++) {
        ms.send(dp, ttl);
    }
    ms.leaveGroup(ia);

    System.out.println(ms.getNetworkInterface().getDisplayName());

    ms.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    int port = 0;
    byte ttl = (byte) 1;

    InetAddress ia = InetAddress.getByName("127.0.0.1");

    byte[] data = "Here's some multicast data\r\n".getBytes();
    DatagramPacket dp = new DatagramPacket(data, data.length, ia, port);

    MulticastSocket ms = new MulticastSocket(InetSocketAddress.createUnresolved("java2s.com", 8080));
    ms.joinGroup(ia);/*from   w w w  .j  a  v  a2s  .c om*/
    for (int i = 1; i < 10; i++) {
        ms.send(dp, ttl);
    }
    ms.leaveGroup(ia);

    ia = ms.getInterface();
    System.out.println(ia);

    ms.close();
}

From source file:lookForPorts2.java

public static void main(String[] args) {

    Socket theSocket;//from   w w w .  j av a 2 s .  c om
    String host = "localhost";

    if (args.length > 0) {
        host = args[0];
    }

    try {
        InetAddress theAddress = InetAddress.getByName(host);
        for (int i = 1024; i < 65536; i++) {
            try {
                System.out.println("Looking for port " + i);
                theSocket = new Socket(theAddress, i);
                System.out.println("There is a server on port " + i + " of " + host);
            } catch (IOException e) {
                // must not be a server on this port
            }
        }
    } catch (UnknownHostException e) {
        System.err.println(e);
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    byte[] outbuf = new byte[1024];
    int port = 1234;
    DatagramSocket socket = new DatagramSocket();
    InetAddress groupAddr = InetAddress.getByName("8.1.2.3");
    DatagramPacket packet = new DatagramPacket(outbuf, outbuf.length, groupAddr, port);
    socket.send(packet);//from www  . j av a  2s.  c o m
}

From source file:Main.java

License:asdf

public static void main(String[] arges) throws Exception {

    InetAddress addr = InetAddress.getByName(null);
    Socket sk = new Socket(addr, 8888);
    BufferedReader in = new BufferedReader(new InputStreamReader(sk.getInputStream()));
    PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sk.getOutputStream())), true);
    out.println("asdf");
    System.out.println(in.readLine());
}

From source file:DNSLookup.java

public static void main(String args[]) {
    try {/*from   ww  w  .  j a  v  a  2s  . c o m*/
        InetAddress host;
        if (args.length == 0) {
            host = InetAddress.getLocalHost();
        } else {
            host = InetAddress.getByName(args[0]);
        }
        System.out.println("Host:'" + host.getHostName() + "' has address: " + host.getHostAddress());
        byte bytes[] = host.getAddress();
        int fourBytes[] = new int[bytes.length];
        for (int i = 0, n = bytes.length; i < n; i++) {
            fourBytes[i] = bytes[i] & 255;
        }
        System.out.println("\t" + fourBytes[0] + "." + fourBytes[1] + "." + fourBytes[2] + "." + fourBytes[3]);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    int mcPort = 12345;
    String mcIPStr = "230.1.1.1";
    DatagramSocket udpSocket = new DatagramSocket();

    InetAddress mcIPAddress = InetAddress.getByName(mcIPStr);
    byte[] msg = "Hello".getBytes();
    DatagramPacket packet = new DatagramPacket(msg, msg.length);
    packet.setAddress(mcIPAddress);//  w  w w .j  ava2 s . co  m
    packet.setPort(mcPort);
    udpSocket.send(packet);

    System.out.println("Sent a  multicast message.");
    System.out.println("Exiting application");
    udpSocket.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    final int LOCAL_PORT = 12345;
    final String SERVER_NAME = "localhost";
    DatagramSocket udpSocket = new DatagramSocket(LOCAL_PORT, InetAddress.getByName(SERVER_NAME));

    System.out.println("Created UDP  server socket at " + udpSocket.getLocalSocketAddress() + "...");
    while (true) {
        System.out.println("Waiting for a  UDP  packet...");
        DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);
        udpSocket.receive(packet);/*from  www.  ja v  a2 s .c o m*/
        displayPacketDetails(packet);
        udpSocket.send(packet);
    }
}