Example usage for java.net DatagramPacket getData

List of usage examples for java.net DatagramPacket getData

Introduction

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

Prototype

public synchronized byte[] getData() 

Source Link

Document

Returns the data buffer.

Usage

From source file:MulticastClient.java

 public static void main(String[] args) throws IOException {

   MulticastSocket socket = new MulticastSocket(4446);
   InetAddress address = InetAddress.getByName("230.0.0.1");
   socket.joinGroup(address);//from  www .ja  v a  2 s . co  m

   DatagramPacket packet;

   // get a few quotes
   for (int i = 0; i < 5; i++) {

      byte[] buf = new byte[256];
      packet = new DatagramPacket(buf, buf.length);
      socket.receive(packet);

      String received = new String(packet.getData());
      System.out.println("Quote of the Moment: " + received);
   }

   socket.leaveGroup(address);
   socket.close();
}

From source file:ChatClient.java

  public static void main(String[] args) throws Exception {
  DatagramSocket s = new DatagramSocket();
  byte[] buf = new byte[1000];
  DatagramPacket dp = new DatagramPacket(buf, buf.length);

  InetAddress hostAddress = InetAddress.getByName("localhost");
  while (true) {/* w ww . ja v a 2 s . c  om*/
    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    String outMessage = stdin.readLine();

    if (outMessage.equals("bye"))
      break;
    String outString = "Client say: " + outMessage;
    buf = outString.getBytes();

    DatagramPacket out = new DatagramPacket(buf, buf.length, hostAddress, 9999);
    s.send(out);

    s.receive(dp);
    String rcvd = "rcvd from " + dp.getAddress() + ", " + dp.getPort() + ": "
        + new String(dp.getData(), 0, dp.getLength());
    System.out.println(rcvd);
  }
}

From source file:MulticastSniffer.java

public static void main(String[] args) {

    InetAddress ia = null;/*from  ww  w . j a v a  2 s .c  o  m*/
    byte[] buffer = new byte[65509];
    DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
    int port = 0;

    try {
        try {
            ia = InetAddress.getByName(args[0]);
        } catch (UnknownHostException e) {
            //
        }
        port = Integer.parseInt(args[1]);
    } // end try
    catch (Exception e) {
        System.err.println(e);
        System.err.println("Usage: java MulticastSniffer MulticastAddress port");
        System.exit(1);
    }

    try {
        MulticastSocket ms = new MulticastSocket(port);
        ms.joinGroup(ia);
        while (true) {
            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:MulticastSniffer.java

public static void main(String[] args) {
    InetAddress ia = null;//w  w  w . j  a va2  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   www  .  jav  a  2 s .  c  o 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:SDRecord.java

private static byte[] basicFilter(DatagramPacket packet) {
    for (byte b : packet.getData())
        if (b != 0)
            return packet.getData();
    return null;//from w ww.java  2s .co  m
}

From source file:SDRecord.java

private static long recordToStdout(DatagramPacket packet) {
    System.out.write(packet.getData(), 0, packet.getLength());
    System.out.flush();//from   w  ww  .  j  a va  2s  .co  m
    return packet.getLength();
}

From source file:SDRecord.java

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