A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
The Mikado Method
this week in the
Agile and other Processes
forum!
JavaRanch
»
Java Forums
»
Java
»
Sockets and Internet Protocols
Author
Wake on lan
david luis
Greenhorn
Joined: Mar 10, 2011
Posts: 22
posted
Apr 07, 2011 17:37:36
0
Hi,
I'm tryng to developp an application with
java
to WOL but doesn't works. The code:
String ipStr = "X.Y.Z.H"; String macStr = "00:1B:FC:53:5A:11"; try { 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); DatagramSocket socket = new DatagramSocket(); socket.send(packet); socket.close(); System.out.println("Wake-on-LAN packet sent."); } catch (Exception e) { System.out.println("Failed to send Wake-on-LAN packet: " + e); System.exit(1); } } private static byte[] getMacBytes(String macStr) throws IllegalArgumentException { byte[] bytes = new byte[6]; String[] hex = macStr.split("(\\:|\\-)"); if (hex.length != 6) { throw new IllegalArgumentException("Invalid MAC address."); } try { for (int i = 0; i < 6; i++) { bytes[i] = (byte) Integer.parseInt(hex[i], 16); } } catch (NumberFormatException e) { throw new IllegalArgumentException("Invalid hex digit in MAC address."); } return bytes; } }
Could you help me please?
Many thanks and sorry for my english!
I agree. Here's the link:
http://aspose.com/file-tools
subject: Wake on lan
Similar Threads
Hint needed: TCP -> UDP , test on localhost
Network assignment HELP NEEDED
Broadcasting to addresses
Invoking socket.receive only when...
DNS query, protocol
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter