远程唤醒实现开机

远程唤醒实现开机packagecom.lh.openOS;importjava.net.DatagramPacket;importjava.net.DatagramSocket;importjava.net.InetAddress;importjava.net.SocketException;publicclassTestOpenOS{ privateintport=100

大家好,又见面了,我是你们的朋友全栈君。

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();
	}
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/137927.html原文链接:https://javaforall.net

(0)
上一篇 2022年5月6日 上午9:04
下一篇 2022年5月6日 上午9:04


相关推荐

  • chmod命令之rwx

    chmod命令之rwxchmod命令[root@localhosttest]#chmodg+wxdata1.txt[root@localhosttest]#chmod777dir1rwx总结

    2022年6月2日
    36
  • 如何将Python算法模型注册成Spark UDF函数实现全景模型部署

    如何将Python算法模型注册成Spark UDF函数实现全景模型部署背景 Background 对于算法业务团队来说 将训练好的模型部署成服务的业务场景是非常常见的 通常会应用于三个场景 部署到流式程序里 比如风控需要通过流式处理来实时监控 部署到批任务中 部署成 API 服务然而在现实世界中 很多算法工程师都会遇到需要花费很多精力在模型部署工程上的问题 平台割裂 训练和部署是在不同平台完成的 配合问题 部署一个模型 需要研发工程师 运维配合 才能完成这件事 技术问题 一般地 大数据里的批流亦或是 Web 服务一般用 Java Scala C 偏多些 而 A

    2026年3月17日
    2
  • javascript框架–brook

    javascript框架–brookbrookbrook 是一个针对大规模 web 开发的 Javascript 框架 而不是一个工具集 brook 引用了 UNIX 下的 pipe 概念 轻松把所有的处理都串联起来以共同完成任务 前一个处理的输出作为后一个处理的输入来完成参数的传递 通过 brook 你可以以 MVC 的方式来写你的 javascript 程序 http hirokidaichi github com brook b

    2026年3月17日
    1
  • 免费天气api 真正可用

    免费天气api 真正可用天气 api 免费 2022 真正可用天气 api

    2026年3月16日
    2
  • android:ListView排序[通俗易懂]

    android:ListView排序[通俗易懂]
    Android:ListView排序

    ListView通过适配器得到内容数据,可以对数据list先排序再提供给ListView:
    privateList>getData(){
    List>list=newArrayList>();
    //addData…
    //对list进行排序

    2026年4月15日
    8
  • instanceof 和 isInstance 区别

    instanceof 和 isInstance 区别其实二者的功能是一致的 isInstance 源码中有说明 Thismethodis codeinstance operator 对应的翻译 这个方法是 Java 语言 instanceof 操作符的动态等价物 下面具体介绍一下二者 1 instanceofin

    2026年3月17日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号