远程发送开机指令

转发一段用java发送远程开机数据包的代码:

另外,在线发送的网址:http://www.depicus.com/wake-on-lan/woli.aspx

 

package com.lh.openOS;
 
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
 
public class TestOpenOS
{
    private int port = 10000;
    private String mac = "8C-89-A5-19-82-9A";
    private String ipAddress = "255.255.255.255";
 
    public TestOpenOS()
    {
        // TODO Auto-generated constructor stub
    }
     
    private void setMac(String mac)
    {
        this.mac = mac;
    }
     
    public TestOpenOS(int port, String mac, String ipAddress)
    {
        this.port = port;
        this.mac = mac;
        this.ipAddress = ipAddress;
    }
 
    public void sendOpenOS()
    {
        InetAddress destHost = null;
        DatagramSocket ds = null;
        try
        {
            destHost = InetAddress.getByName(ipAddress);
            byte[] destMac = getMacBytes(mac);
            byte[] magicBytes = new byte[102];
 
            // 将数据包的前六位放入0xFF即“FF”的二进制
            // 发一个BroadCast包,包的内容包括以下数据就可以了。
            // FF FF FF FF FF FF,6个FF是数据的开始,
            // 紧跟着16次MAC地址就可以了。
            for (int i = 0; i < 16; i++)
            {
                for (int j = 0; j < destMac.length; j++)
                {
                    magicBytes[6 + destMac.length * i + j] = destMac[j];
                    magicBytes[j] = (byte) 0xFF;
                }
            }
 
            // --------------------查看数据报发出的数据---------------------
            for (int i = 1; i <= 102; i++)
            {
                if (((i - 1) % 6) == 0)
                {
                    System.out.println();
                }
                System.out.print(magicBytes[i - 1] + "===");
            }
 
//          System.out.println("port =" + port + ", destHost=" + destHost + ",magicBytes.length = " + magicBytes.length);
             
             
            DatagramPacket dp = null;
            try
            {
                dp = new DatagramPacket(magicBytes, magicBytes.length, destHost, port);
                ds = new DatagramSocket();
                ds.send(dp);
            }
            catch (Exception e)
            {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
        catch (Exception e)
        {
            // TODO: handle exception
            e.printStackTrace();
        }
        finally
        {
            if (null != ds)
            {
                //关闭数据报流
                ds.close();
            }
        }
    }
 
    // 远程关机
    public void sendCloseOS()
    {
 
    }
     
    /**
     *  将16进制的mac地址转换为二进制 
     * @param macStr
     * @return 
     * @throws IllegalArgumentException
     */
    private static byte[] getMacBytes(String macStr) throws IllegalArgumentException
    {
        byte[] bytes = new byte[6];
        String[] hex = macStr.split("(\\:|\\-)");
        if (hex.length != 6)
        {
            throw new IllegalArgumentException("mac 地址有问题,你看看对不对?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;
    }
 
    public static void main(String[] args)
    {
        TestOpenOS w = new TestOpenOS();
        w.sendOpenOS();
         
        w.setMac("8C-89-A5-19-80-3D");
        w.sendOpenOS();
    }
}

 

 

你可能感兴趣的:(发送)