Create DatagramPacket from byte array : DatagramPacket « Network Protocol « Java






Create DatagramPacket from byte array


import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

class Main {
  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);
    }
  }
}

 








Related examples in the same category

1.Get data from DatagramPacket
2.Get address and port from DatagramPacket
3.Sends the specified text or file as a datagram to the specified port of the specified host
4.Waits to receive datagrams sent the specified portWaits to receive datagrams sent the specified port