Example usage for java.net DatagramPacket DatagramPacket

List of usage examples for java.net DatagramPacket DatagramPacket

Introduction

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

Prototype

public DatagramPacket(byte buf[], int length, InetAddress address, int port) 

Source Link

Document

Constructs a datagram packet for sending packets of length length to the specified port number on the specified host.

Usage

From source file:Main.java

public static void main(String args[]) {
    try {/*  ww w .  j  ava  2  s .c o  m*/

        int port = 80;

        DatagramSocket ds = new DatagramSocket(port);

        while (true) {
            byte buffer[] = new byte[BUFSIZE];

            DatagramPacket dp = new DatagramPacket(buffer, buffer.length, InetAddress.getByName("google.com"),
                    8080);

            ds.receive(dp);

            String str = new String(dp.getData());

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

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  av  a2 s  . c om*/
    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);/*from  w w  w.  j a v  a 2s  .c  om*/
    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);//  ww w . java2s .c o  m
    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:GetDate.java

public static void main(String args[]) throws Exception {
    byte msg[] = new byte[256];
    DatagramSocket dgSocket = new DatagramSocket();
    InetAddress destination = InetAddress.getByName("web.mit.edu");
    DatagramPacket datagram = new DatagramPacket(msg, msg.length, destination, PortDayTime);
    dgSocket.send(datagram);/*from  w w  w.  ja v  a 2s. com*/
    datagram = new DatagramPacket(msg, msg.length);
    dgSocket.receive(datagram);
    String received = new String(datagram.getData());
    System.out.println("The time in Cambridge is now: " + received);
    dgSocket.close();
}

From source file:Main.java

License:asdf

public static void main(String args[]) throws Exception {
    InetAddress ia = InetAddress.getByName(args[0]);
    int port = Integer.parseInt(args[1]);
    DatagramSocket ds = new DatagramSocket();
    while (true) {
        String s = "asdf";
        byte buffer[] = s.getBytes();
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, port);
        ds.send(dp);/*from w  w  w .  j  a  v  a2s. com*/
    }
}

From source file:Main.java

public static void main(String args[]) throws IOException {
    String host = args[0];//  w  ww . ja  v a2s .  c om
    byte message[] = new byte[256];
    InetAddress address = InetAddress.getByName(host);
    System.out.println("Checking at: " + address);
    DatagramPacket packet = new DatagramPacket(message, message.length, address, DAYTIME_PORT);
    DatagramSocket socket = new DatagramSocket();
    socket.send(packet);
    packet = new DatagramPacket(message, message.length);
    socket.receive(packet);
    String time = new String(packet.getData());
    System.out.println(time);
    socket.close();
}

From source file:Main.java

public static void main(String args[]) {
    try {/*from w  w w .  ja v a 2s  .  com*/

        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  w  w.  j  ava 2  s . co  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));

        DatagramChannel channel = ds.getChannel();

        ds.send(dp);
        ds.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:UDPSend.java

public static void main(String args[]) {
    try {/*from w  w  w .j  a  va  2 s. c  o  m*/
        String host = "www.java2s.com";
        int port = 90;

        byte[] message = "Java Source and Support".getBytes();

        // Get the internet address of the specified host
        InetAddress address = InetAddress.getByName(host);

        // Initialize a datagram packet with data and address
        DatagramPacket packet = new DatagramPacket(message, message.length, address, port);

        // Create a datagram socket, send the packet through it, close it.
        DatagramSocket dsocket = new DatagramSocket();
        dsocket.send(packet);
        dsocket.close();
    } catch (Exception e) {
        System.err.println(e);
    }
}