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 {
    InetAddress dst = InetAddress.getLocalHost();
    int port = 8080;
    byte[] outbuf = new byte[1024];
    int len = 1024;

    DatagramPacket request = new DatagramPacket(outbuf, len, dst, port);
    DatagramSocket socket = new DatagramSocket();
    socket.send(request);
}

From source file:PacketReceiver.java

public static void main(String[] args) throws Exception {
    byte[] buffer = "data".getBytes();
    DatagramPacket packet = new DatagramPacket(buffer, buffer.length, new InetSocketAddress("localhost", 5002));
    DatagramSocket socket = new DatagramSocket(5003);
    socket.send(packet);
}

From source file:Main.java

public static void main(String args[]) throws IOException {
    String host = args[0];//w  ww  .j a  va2 s.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:UDPSend.java

public static void main(String args[]) {
    try {//from  w  w w . j ava 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);
    }
}

From source file:MainClass.java

public static void main(String[] args) {

    try {/*from ww w.j a  va 2 s.  c  o  m*/
        String data = "data in UDP";
        byte[] buffer = data.getBytes();

        DatagramPacket packet = new DatagramPacket(buffer, buffer.length,
                new InetSocketAddress("localhost", 5002));

        DatagramSocket socket = new DatagramSocket(5003);

        System.out.println("Sending a packet...");
        socket.send(packet);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DatagramPacket pack = new DatagramPacket(new byte[BUFFERSIZE], BUFFERSIZE);
    DatagramSocket sock = new DatagramSocket(7);
    // echo back everything
    while (true) {
        sock.receive(pack);/*from   ww  w .  ja v  a  2  s  .c om*/
        sock.send(pack);
    }
}

From source file:UDPSend.java

public static void main(String args[]) {
    try {//from w  w w.ja  va2 s. c o m
        // Check the number of arguments
        if (args.length < 3)
            throw new IllegalArgumentException("Wrong number of args");

        // Parse the arguments
        String host = args[0];
        int port = Integer.parseInt(args[1]);

        // Figure out the message to send.
        // If the third argument is -f, then send the contents of the file
        // specified as the fourth argument. Otherwise, concatenate the
        // third and all remaining arguments and send that.
        byte[] message;
        if (args[2].equals("-f")) {
            File f = new File(args[3]);
            int len = (int) f.length(); // figure out how big the file is
            message = new byte[len]; // create a buffer big enough
            FileInputStream in = new FileInputStream(f);
            int bytes_read = 0, n;
            do { // loop until we've read it all
                n = in.read(message, bytes_read, len - bytes_read);
                bytes_read += n;
            } while ((bytes_read < len) && (n != -1));
        } else { // Otherwise, just combine all the remaining arguments.
            String msg = args[2];
            for (int i = 3; i < args.length; i++)
                msg += " " + args[i];
            // Convert the message to bytes using UTF-8 encoding
            message = msg.getBytes("UTF-8");
        }

        // 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);
        System.err.println(usage);
    }
}

From source file:Main.java

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

        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.send(dp);
        ds.disconnect();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

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

        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.send(dp);
        System.out.println(ds.isConnected());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String args[]) {
    try {/*from   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.send(dp);
        System.out.println(ds.isClosed());
    } catch (Exception e) {
        e.printStackTrace();
    }
}