Java code for Remote Power on the network

Source: Internet
Author: User

http://my.oschina.net/kingfire/blog/156764

Overview

Remote Power On (Wake Onlan) refers to the network implementation of the server or PC to start running, now a lot of network cards are supported by this feature. The simple principle is to send a specially formatted packet to the target host, and then start the system after the target host receives it.

Specific methods.
    1. Setting the BIOS on Wake Onlan (WOL) feature

    2. View the MAC address of the host network card

    3. Connect the host to the Ethernet via the NIC

    4. send a power-on code to boot the system
      In fact, through the socket to the target machine to send Magicpacket (Magic Packet), the format of the Magic Packet, contains a continuous 6 bytes of "FF" and repeated 16 times the MAC address. You can use this protocol to make a magicpacket that uses this protocol by filling in "FFFFFFFFFFFF" + 16 consecutive MAC addresses in any protocol packet (such as TCP/IP, IPX packets). The computer wakes up whenever the NIC detects that there is such a fragment anywhere in the packet.

    5. Java code to send a packet

?
12345678910111213141516171819202122232425262728293031323334353637383940414243444546 public static void main(String[] args) throws IOException {    int port = 20105;    String macAddress = "01-12-43-44-D5-56";    // String destIP = "255.255.255.255";// 广播地址    String destIP = "112.11.15.28";// 广播地址    // 检测 mac 地址,并将其转换为二进制    byte[] destMac = getMacBytes(macAddress);    if (destMac == null)        return;    InetAddress destHost = InetAddress.getByName(destIP);    // construct packet data    byte[] magicBytes = new byte[102];    // 将数据包的前6位放入0xFF即 "FF"的二进制    for (int i = 0; i < 6; i++)        magicBytes[i] = (byte) 0xFF;        // 从第7个位置开始把mac地址放入16次        for (int i = 0; i < 16; i++) {            for (int j = 0; j < destMac.length; j++) {                magicBytes[6 + destMac.length * i + j] = destMac[j];            }        }        // create packet        DatagramPacket dp = null;        dp = new DatagramPacket(magicBytes, magicBytes.length, destHost, port);        DatagramSocket ds = new DatagramSocket();        ds.send(dp);        ds.close();        System.out.println("ok");}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;}
Problems that you may encounter
    1. in the router environment, want to be in the public network to achieve internal network computer boot
      The router's IP mapping needs to be set to map the extranet address to an intranet address, such as the Tp-link DMZ host setting.
      Set the IP address of the destination host (mac:01-12-43-44-d5-56) to a static IP, such as 192.168.0.99, and then bind the Mac and IP on the router

    2. the public IP at home has been changing
      You can bind the peanut shell inside the router (the mobile CRC may have to pay for the server to resolve the IP correctly).
      can also be used to become, timed access IP address resolution Web pages such as: Http://www.ip138.com, and then the IP address reported to the public Web site.
      PS: I used an abandoned Android phone to write a periodic report home IP address of the program, open every day, to their own public network Web server report home IP hehe

Resources

Http://zhidao.baidu.com/link?url=5EAPJZ-y_62ESu4fKYZqiAe19qvKrI9sBc9eeGbW0g8nxu2IikubcSbpzgSVmoEEAq5l7epLNWPcSw9klXS3Sa

Java code for Remote Power on the network

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.