public static void sendWakeOnLan(@NonNull String ipStr,@NonNull String macStr,int port,int timeoutMillis,int packets) throws IOException, IllegalArgumentException {
  if (ipStr == null)   throw new IllegalArgumentException("Ip Address cannot be null");
  if (macStr == null)   throw new IllegalArgumentException("MAC Address cannot be null");
  if (port <= 0 || port > 65535)   throw new IllegalArgumentException("Invalid port " + port);
  if (packets <= 0)   throw new IllegalArgumentException("Invalid number of packets to send " + packets);
  byte[] macBytes=getMacBytes(macStr);
  byte[] bytes=new byte[6 + 16 * macBytes.length];
  for (int i=0; i < 6; i++) {
    bytes[i]=(byte)0xff;
  }
  for (int i=6; i < bytes.length; i+=macBytes.length) {
    System.arraycopy(macBytes,0,bytes,i,macBytes.length);
  }
  InetAddress address=InetAddress.getByName(ipStr);
  DatagramPacket packet=new DatagramPacket(bytes,bytes.length,address,port);
  for (int i=0; i < packets; i++) {
    DatagramSocket socket=new DatagramSocket();
    socket.setSoTimeout(timeoutMillis);
    socket.send(packet);
    socket.close();
  }
}

public static void sendWakeOnLan ( @ NonNull String ipStr , @ NonNull String macStr , int port , int timeoutMillis , int packets ) throws IOException , IllegalArgumentException { if ( ipStr == null ) throw new IllegalArgumentException ( "Ip Address cannot be null" ) ; if ( macStr == null ) throw new IllegalArgumentException ( "MAC Address cannot be null" ) ; if ( port <= 0 || port > 65535 ) throw new IllegalArgumentException ( "Invalid port " + port ) ; if ( packets <= 0 ) throw new IllegalArgumentException ( "Invalid number of packets to send " + packets ) ; byte [ ] macBytes = getMacBytes ( macStr ) ; byte [ ] bytes = new byte [ 6 + 16 * macBytes . length ] ; for ( int i = 0 ; i < 6 ; i ++ ) { bytes [ i ] = ( byte ) 0xff ; } for ( int i = 6 ; i < bytes . length ; i += macBytes . length ) { System . arraycopy ( macBytes , 0 , bytes , i , macBytes . length ) ; } InetAddress address = InetAddress . getByName ( ipStr ) ; DatagramPacket packet = new DatagramPacket ( bytes , bytes . length , address , port ) ; for ( int i = 0 ; i < packets ; i ++ ) { DatagramSocket socket = new DatagramSocket ( ) ; socket . setSoTimeout ( timeoutMillis ) ; socket . send ( packet ) ; socket . close ( ) ; } }
send a wake - on - lan packet
