从 Android 向 PC 发送 LAN 唤醒数据包

发布于 2024-11-01 20:11:10 字数 476 浏览 0 评论 0原文

我的 Android 应用程序使用 HTTP 向用户自己的 PC 发送数据/从用户自己的 PC 检索数据,并且在少数 Beta 测试人员的支持下运行良好。我现在需要考虑 PC 休眠的情况。

我以前从未这样做过,但我在 google 上搜索找到了有关 WOL“魔术包”的信息和一些用 C 编写的简单源代码(在客户端使用 CAsyncSocket)。通过用户家庭网络上的 Wi-Fi 连接执行此操作可能相对简单,但理想情况下我希望它能够通过移动互联网工作(假设用户可以配置其家庭路由器来接受/转发数据包)。

我猜我需要使用一些通用的 Java 网络代码,并且我一直在研究 java.net

此时我无法决定是否应该使用 java.net.Socket 还是 java.net.DatagramSocket。所以问题是,我是否以正确的方式处理这个问题以及我应该使用两种套接字类型中的哪一种(或者两者都足够)?非常感谢。

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.

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).

I'm guessing I need to use some generic Java network code and I've been looking at java.net.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丑丑阿 2024-11-08 20:11:10

我不能把它归功于太多,因为它来自这个 网站

但是这个是一个 java 版本的 LAN 唤醒类:

public static final int PORT = 9;    

public static void main(String[] args) {
    
    if (args.length != 2) {
        System.out.println("Usage: java WakeOnLan <broadcast-ip> <mac-address>");
        System.out.println("Example: java WakeOnLan 192.168.0.255 00:0D:61:08:22:4A");
        System.out.println("Example: java WakeOnLan 192.168.0.255 00-0D-61-08-22-4A");
        System.exit(1);
    }
    
    String ipStr = args[0];
    String macStr = args[1];
    
    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);
        try (DatagramSocket socket = new DatagramSocket()) {
            socket.send(packet);
        }
        
        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;
}

当然,您需要修改它才能与 android 一起使用(需要很少的工作),但我发现它比 @Bear 的答案更好。

I can't take too much credit for it as its from this site

But this is a java version of wake on lan class:

public static final int PORT = 9;    

public static void main(String[] args) {
    
    if (args.length != 2) {
        System.out.println("Usage: java WakeOnLan <broadcast-ip> <mac-address>");
        System.out.println("Example: java WakeOnLan 192.168.0.255 00:0D:61:08:22:4A");
        System.out.println("Example: java WakeOnLan 192.168.0.255 00-0D-61-08-22-4A");
        System.exit(1);
    }
    
    String ipStr = args[0];
    String macStr = args[1];
    
    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);
        try (DatagramSocket socket = new DatagramSocket()) {
            socket.send(packet);
        }
        
        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;
}

Of course you will need to modify this to work with android (very little work needed) but I found it works better than @Bear's answer.

朮生 2024-11-08 20:11:10

这是我过去使用过的一些 C# 代码。转换成java并使用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....");
        }
    }
}

希望这会有所帮助

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

生生漫 2024-11-08 20:11:10

java.net.DatagramSocket 可能会工作得足够好,因为 WoL 不提供传递确认。您能否让它在本地网络之外工作值得怀疑,因为 WoL 数据包在网络上广播,目标地址是目标计算机的 MAC 地址,并且大多数路由器都设置为阻止来自 WAN 的广播数据包。

java.net.DatagramSocket would probably work well enough, since WoL does not provide delivery confirmation. It's doubtful you'll be able to get it to work outside the local network, since WoL packets are broadcast across the network with the destination address as the MAC address of the target computer and most routers are setup to block broadcast packets from the WAN.

我的影子我的梦 2024-11-08 20:11:10

你可以使用用java编写的代码(在android studio中)

private void SendWoL(String macStr) {
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                SendWoL_(macStr, "255.255.255.0", 40000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    thread.start();
}
private void SendWoL(String macStr, String ipStr, int PORT) {
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                SendWoL_(macStr, ipStr, PORT);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    thread.start();
}
private void SendWoL_(String macStr, String ipStr, int PORT) {
    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);
        try (DatagramSocket socket = new DatagramSocket()) {
            socket.send(packet);
        }
        //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;
}

用法:

SendWoL("B7-3E-99-5B-AA-C8");
SendWoL("B7-3E-99-5B-AA-C8", "192.168.1.255", 40000);

you can use this code written in java (in android studio)

private void SendWoL(String macStr) {
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                SendWoL_(macStr, "255.255.255.0", 40000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    thread.start();
}
private void SendWoL(String macStr, String ipStr, int PORT) {
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                SendWoL_(macStr, ipStr, PORT);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    thread.start();
}
private void SendWoL_(String macStr, String ipStr, int PORT) {
    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);
        try (DatagramSocket socket = new DatagramSocket()) {
            socket.send(packet);
        }
        //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;
}

usage:

SendWoL("B7-3E-99-5B-AA-C8");
SendWoL("B7-3E-99-5B-AA-C8", "192.168.1.255", 40000);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文