发送网络唤醒数据包从安卓到电脑 [英] Sending Wake on LAN packet from Android to PC
问题描述
我的Android应用程序发送到/从用户的个人电脑使用HTTP和它的正常工作与少数测试者/检索数据。我现在需要考虑的情况下将PC冬眠。
My Android app sends/retrieves data to/from the user's own PC using HTTP and it's working fine with a handful of beta testers. I now need to consider a situation where the PC is hibernating.
我从来没有这样做过,但我GOOGLE查找有关WOL魔术包和(使用CAsyncSocket类在客户端)用C语言编写一些简单的源代码信息。在用户的家庭网络上的Wi-Fi连接这样做很可能是相对直接的,但最好我想这是工作在移动互联网(假设用户可以配置自己的家庭路由器接收/转发的数据包)。
I've never done this before but I've googled to find info about the WOL 'magic packet' and some simple source written in C (using CAsyncSocket at the client end). Doing this over a wi-fi connection on the user's home network is likely to be relatively straight-forward but ideally I want this to work over mobile internet (assuming the user can configure their home router to accept / forward the packet).
我猜我需要使用一些通用的Java网络code和我一直在看 java.net
。
I'm guessing I need to use some generic Java network code and I've been looking at java.net
.
在这一点上我不能决定我是否应该使用的java.net.Socket
或 java.net.DatagramSocket中
。所以现在的问题是,我会处理这个正确的方式和其中两个插槽类型我应该使用(或将两者足够了)?非常感谢。
At this point I can't decide whether I should be using java.net.Socket
or java.net.DatagramSocket
. So the question is, am I approaching this the right way and which of the two socket types should I be using (or would both suffice)? Many thanks.
推荐答案
下面是我在过去使用一些C#code。它应该是比较容易转换成Java和发送使用的DatagramPacket
Here is some C# code that I have used in the past. It should be relatively easy to convert into java and send using a DatagramPacket
namespace WakeOnLan
{
class Program
{
static void Main(string[] args)
{
byte[] mac = new byte[] { mac address goes here i.e 0x00, and so on };
WakeUp(mac);
}
private static void WakeUp(byte[] mac)
{
//
// WOL packet is sent over UDP 255.255.255.0:40000.
//
Console.WriteLine("Waking Up.......");
UdpClient client = new UdpClient();
client.Connect(IPAddress.Broadcast, 40000);
//
// WOL packet contains a 6-bytes trailer and 16 times a 6-bytes sequence containing the MAC address.
//
byte[] packet = new byte[17 * 6];
//
// Trailer of 6 times 0xFF.
//
for (int i = 0; i < 6; i++)
packet[i] = 0xFF;
//
// Body of magic packet contains 16 times the MAC address.
//
for (int i = 1; i <= 16; i++)
for (int j = 0; j < 6; j++)
packet[i * 6 + j] = mac[j];
//
// Submit WOL packet.
//
client.Send(packet, packet.Length);
Console.WriteLine("Machine Woke Up....");
}
}
}
希望这有助于
Hope this helps
这篇关于发送网络唤醒数据包从安卓到电脑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!