Example usage for java.net DatagramPacket getLength

List of usage examples for java.net DatagramPacket getLength

Introduction

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

Prototype

public synchronized int getLength() 

Source Link

Document

Returns the length of the data to be sent or the length of the data received.

Usage

From source file:DaytimeClient.java

public static void main(String args[]) throws java.io.IOException {
    // Figure out the host and port we're going to talk to
    String host = args[0];/*from   w w w  .j  a  v  a  2  s.  c  om*/
    int port = 13;
    if (args.length > 1)
        port = Integer.parseInt(args[1]);

    // Create a socket to use
    DatagramSocket socket = new DatagramSocket();

    // Specify a 1-second timeout so that receive() does not block forever.
    socket.setSoTimeout(1000);

    // This buffer will hold the response. On overflow, extra bytes are
    // discarded: there is no possibility of a buffer overflow attack here.
    byte[] buffer = new byte[512];
    DatagramPacket packet = new DatagramPacket(buffer, buffer.length, new InetSocketAddress(host, port));

    // Try three times before giving up
    for (int i = 0; i < 3; i++) {
        try {
            // Send an empty datagram to the specified host (and port)
            packet.setLength(0); // make the packet empty
            socket.send(packet); // send it out

            // Wait for a response (or timeout after 1 second)
            packet.setLength(buffer.length); // make room for the response
            socket.receive(packet); // wait for the response

            // Decode and print the response
            System.out.print(new String(buffer, 0, packet.getLength(), "US-ASCII"));
            // We were successful so break out of the retry loop
            break;
        } catch (SocketTimeoutException e) {
            // If the receive call timed out, print error and retry
            System.out.println("No response");
        }
    }

    // We're done with the channel now
    socket.close();
}

From source file:MulticastSniffer.java

public static void main(String[] args) {
    InetAddress ia = null;// w  w  w  . j ava2  s . c o m
    byte[] buffer = new byte[65535];
    DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
    int port = 0;

    // read the address from the command line
    try {
        try {
            ia = InetAddress.getByName(args[0]);
        } catch (UnknownHostException e) {
            System.err.println(e);
        }
        port = Integer.parseInt(args[1]);
    } catch (Exception e) {
        System.err.println(e);
        System.err.println("Usage: java MulticastSniffer mcast-addr port");
        System.exit(1);
    }
    System.out.println("About to join " + ia);

    try {
        MulticastSocket ms = new MulticastSocket(port);
        ms.joinGroup(ia);
        while (true) {
            System.out.println("About to receive...");
            ms.receive(dp);
            String s = new String(dp.getData(), 0, 0, dp.getLength());
            System.out.println(s);
        }
    } catch (SocketException se) {
        System.err.println(se);
    } catch (IOException ie) {
        System.err.println(ie);
    }
}

From source file:Main.java

public static void displayPacketDetails(DatagramPacket packet) {
    byte[] msgBuffer = packet.getData();
    int length = packet.getLength();
    int offset = packet.getOffset();

    int remotePort = packet.getPort();
    InetAddress remoteAddr = packet.getAddress();
    String msg = new String(msgBuffer, offset, length);

    System.out.println(//from w  w  w  .  j a v  a 2s.co m
            "Received a  packet:[IP Address=" + remoteAddr + ", port=" + remotePort + ", message=" + msg + "]");
}

From source file:Main.java

public static void displayPacketDetails(DatagramPacket packet) {
    byte[] msgBuffer = packet.getData();
    int length = packet.getLength();
    int offset = packet.getOffset();
    int remotePort = packet.getPort();
    InetAddress remoteAddr = packet.getAddress();
    String msg = new String(msgBuffer, offset, length);
    System.out.println("[Server at IP  Address=" + remoteAddr + ", port=" + remotePort + "]: " + msg);
}

From source file:org.openehealth.ipf.commons.ihe.core.atna.UdpServer.java

private static String packetToString(DatagramPacket packet) {
    return new String(packet.getData(), packet.getOffset(), packet.getLength());
}

From source file:Main.java

private static void receive(Map<Integer, byte[]> map, DatagramSocket socket, int[] length) {
    byte[] b = new byte[limit + 4];
    DatagramPacket p = new DatagramPacket(b, b.length);
    try {/*w  ww  .  j a  v  a2  s .  c  om*/
        socket.receive(p);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    if (p.getLength() != 0) {
        ByteBuffer buf = ByteBuffer.wrap(b);
        int key = buf.getInt();
        byte[] bytes = new byte[p.getLength() - 4];
        length[0] += bytes.length;
        buf.get(bytes);
        map.put(key, bytes);
        receive(map, socket, length);
    }
}

From source file:SDRecord.java

private static long recordToStdout(DatagramPacket packet) {
    System.out.write(packet.getData(), 0, packet.getLength());
    System.out.flush();//from   w  w  w. j  av  a2  s.  c om
    return packet.getLength();
}

From source file:SDRecord.java

private static long recordToFile(DatagramPacket packet, OutputStream writer) {
    try {//from ww  w.  j ava 2s.  co m
        writer.write(packet.getData(), 0, packet.getLength());
        writer.flush();
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(5);
    }
    return packet.getLength();
}

From source file:org.eredlab.g4.ccl.net.tftp.TFTPPacket.java

/***
 * When you receive a datagram that you expect to be a TFTP packet, you use
 * this factory method to create the proper TFTPPacket object
 * encapsulating the data contained in that datagram.  This method is the
 * only way you can instantiate a TFTPPacket derived class from a
 * datagram./*from  w  w  w. jav a  2 s  .c om*/
 * <p>
 * @param datagram  The datagram containing a TFTP packet.
 * @return The TFTPPacket object corresponding to the datagram.
 * @exception TFTPPacketException  If the datagram does not contain a valid
 *             TFTP packet.
 ***/
public final static TFTPPacket newTFTPPacket(DatagramPacket datagram) throws TFTPPacketException {
    byte[] data;
    TFTPPacket packet = null;

    if (datagram.getLength() < MIN_PACKET_SIZE)
        throw new TFTPPacketException("Bad packet. Datagram data length is too short.");

    data = datagram.getData();

    switch (data[1]) {
    case READ_REQUEST:
        packet = new TFTPReadRequestPacket(datagram);
        break;
    case WRITE_REQUEST:
        packet = new TFTPWriteRequestPacket(datagram);
        break;
    case DATA:
        packet = new TFTPDataPacket(datagram);
        break;
    case ACKNOWLEDGEMENT:
        packet = new TFTPAckPacket(datagram);
        break;
    case ERROR:
        packet = new TFTPErrorPacket(datagram);
        break;
    default:
        throw new TFTPPacketException("Bad packet.  Invalid TFTP operator code.");
    }

    return packet;
}

From source file:SDRecord.java

private static long recordToSocket(DatagramPacket packet, DatagramSocket socket, InetAddress rhost,
        int destPort) {
    packet.setPort(destPort);/* w  w  w .j a v  a2 s . c o  m*/
    packet.setAddress(rhost);
    try {
        socket.send(packet);
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(5);
    }
    return packet.getLength();
}