Example usage for java.net DatagramSocket send

List of usage examples for java.net DatagramSocket send

Introduction

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

Prototype

public void send(DatagramPacket p) throws IOException 

Source Link

Document

Sends a datagram packet from this socket.

Usage

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 source file:DatagramSender.java

public static void main(String args[]) throws Exception {
    InetAddress ia = InetAddress.getByName(args[0]);
    // Obtain destination port
    int port = Integer.parseInt(args[1]);
    // Create a datagram socket
    DatagramSocket ds = new DatagramSocket();
    // Create a datagram packet
    byte buffer[] = args[2].getBytes();
    DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, port);
    ds.send(dp);
}

From source file:UdpEchoClient.java

public static void main(String[] args) {
    InetAddress address;//from w ww  .ja v  a 2  s.  co  m
    try {
        address = InetAddress.getByName(args[0]);
    } catch (UnknownHostException host) {
        System.out.println(host);
        return;
    }
    DatagramPacket pack = new DatagramPacket(testString.getBytes(), testString.length(), address, 7);
    DatagramPacket incoming = new DatagramPacket(new byte[256], 256);
    DatagramSocket sock = null;
    try {
        Calendar start, end;
        sock = new DatagramSocket();
        start = Calendar.getInstance();
        sock.send(pack);
        sock.setSoTimeout(5000);
        sock.receive(incoming);
        end = Calendar.getInstance();
        String reply = new String(incoming.getData());
        reply = reply.substring(0, testString.length());
        if (reply.equals(testString)) {
            System.out.println("Success");
            System.out.println("Time = " + (end.getTime().getTime() - start.getTime().getTime()) + "mS");
        } else
            System.out.println("Reply data did not match");
    } catch (SocketException socke) {
        System.out.println(socke);
    } catch (IOException ioe) {
        System.out.println(ioe);
    } finally {
        sock.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);
    datagram = new DatagramPacket(msg, msg.length);
    dgSocket.receive(datagram);/*from  w  ww  . j a  va  2 s. c om*/
    String received = new String(datagram.getData());
    System.out.println("The time in Cambridge is now: " + received);
    dgSocket.close();
}

From source file:Main.java

public static void main(String args[]) {
    try {/*from www.ja  v a 2  s .co  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);
        ds.setSoTimeout(1000);
        ds.send(dp);

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

From source file:Main.java

public static void main(String args[]) {
    try {//w  w w .j  a va2  s  . c om

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

        DatagramSocket ds = new DatagramSocket();

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

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

From source file:Main.java

public static void main(String args[]) {
    try {/* w w  w  .j  a  va2s  .  c  om*/

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

        DatagramSocket ds = new DatagramSocket();

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

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

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 ww. j av  a 2  s . c  om*/
}

From source file:Main.java

public static void main(String args[]) {
    try {/* ww w. j  a  v a  2s.  c  om*/

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

        DatagramSocket ds = new DatagramSocket();

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

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

From source file:Main.java

public static void main(String args[]) {
    try {/*from www . j  av a 2  s .  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);
        ds.setSendBufferSize(1000);
        ds.send(dp);

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