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[]) {
    try {/*  w w  w. ja v a2s.c o m*/

        InetAddress ia = InetAddress.getByName("www.java2s.com");

        DatagramSocket ds = new DatagramSocket();

        byte buffer[] = "hello".getBytes();
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 80);

        // Send the datagram packet
        ds.send(dp);

        System.out.println(ds.getLocalPort());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:MainClass.java

public static void main(String[] args) {

    String host = "localhost";

    try {/*  www.  ja  v  a  2 s  .c o  m*/
        InetAddress theAddress = InetAddress.getByName(host);
        for (int i = 1024; i < 65536; i++) {
            Socket theSocket = new Socket(theAddress, i);
            System.out.println("There is a server on port " + i + " of " + host);
        }
    } catch (Exception ex) {
        System.err.println(ex);
    }

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    InetAddress addr = InetAddress.getByName("java.sun.com");
    int port = 80;
    SocketAddress sockaddr = new InetSocketAddress(addr, port);

    Socket sock = new Socket();

    sock.connect(sockaddr);/* www .  j a  va 2 s .c o m*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    InetAddress addr = InetAddress.getByName("java.sun.com");
    int port = 80;
    SocketAddress sockaddr = new InetSocketAddress(addr, port);

    Socket sock = new Socket();

    int timeoutMs = 2000;
    sock.connect(sockaddr, timeoutMs);//  w w w  . ja  va 2 s.c  om
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    InetAddress addr = InetAddress.getByName("java.sun.com");
    int port = 80;
    SocketAddress sockaddr = new InetSocketAddress(addr, port);

    Socket sock = new Socket();

    int timeoutMs = 2000; // 2 seconds
    sock.connect(sockaddr, timeoutMs);/*from w  ww . ja  v a2s. co  m*/
}

From source file:MainClass.java

public static void main(String[] args) {

    String host = "localhost";

    try {// w  w w .j  a va 2  s . c o  m
        InetAddress theAddress = InetAddress.getByName(host);
        for (int i = 1; i < 65536; i++) {
            Socket connection = null;
            connection = new Socket(host, i);
            System.out.println("There is a server on port " + i + " of " + host);
            if (connection != null)
                connection.close();
        } // end for
    } catch (Exception ex) {
        System.err.println(ex);
    }

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int port = 80;

    Socket socket = new Socket("google.com", port, InetAddress.getByName("java2s.com"), port);

    socket.bind(new InetSocketAddress("java2s.com", port));
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    int port = 0;
    InetAddress group = InetAddress.getByName("127.0.0.1");

    MulticastSocket ms = new MulticastSocket(port);
    ms.joinGroup(group);//from w ww .j  a va 2s . c  o m

    byte[] buffer = new byte[8192];
    while (true) {
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
        ms.receive(dp);
        String s = new String(dp.getData());
        System.out.println(s);
    }
    // ms.leaveGroup(group);
    // ms.close();

}

From source file:Main.java

public static void main(String args[]) {
    try {//from  w ww.j  a  v  a2s .  c o m

        InetAddress ia = InetAddress.getByName("www.java2s.com");

        DatagramSocket ds = new DatagramSocket(InetSocketAddress.createUnresolved("google.com", 8080));

        byte buffer[] = "hello".getBytes();
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 80);
        ds.connect(InetSocketAddress.createUnresolved("google.com", 8080));
        ds.send(dp);
        ds.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String args[]) {
    try {/*w ww. ja v  a2  s .c  o m*/

        InetAddress ia = InetAddress.getByName("www.java2s.com");

        DatagramSocket ds = new DatagramSocket(8080, ia);

        byte buffer[] = "hello".getBytes();
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 80);
        ds.connect(InetSocketAddress.createUnresolved("google.com", 8080));
        ds.send(dp);
        ds.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}