コード例 #1
0
        // *********************************************************************
        /// <summary>
        /// Wakes up the machine with the given <paramref name="macAddress"/>.
        /// </summary>
        /// <remarks>
        /// <para>
        /// <note>
        /// <list type="number">
        /// <item> The motherboard must support Wake On LAN.</item>
        /// <item> The NIC must support Wake On LAN.</item>
        /// <item> There must be a wire connecting the motherboard's WOL port to
        /// the NIC's WOL port. Usually there always is a connection on most of
        /// the PCs.</item>
        /// <item> The Wake On LAN feature must be enabled in the motherboard's
        /// BIOS. Usually this is also enabled by default, but you might like to
        /// check again.</item>
        /// <item> The "Good Connection" light on the back of the NIC must be lit
        /// when the machine is off. (By default always good if you are not
        /// facing any network issues)</item>
        /// <item> Port 12287 (0x2FFF) must be open. (By default it should be
        /// open unless some antivirus or any other such program has changed
        /// settings.)</item>
        /// <item> Packets cannot be broadcast across the Internet.  That's why
        /// it's called Wake On Lan, not Wake On Internet.</item>
        /// <item> To find your MAC address, run the MSINFO32.EXE tool that is a
        /// part of Windows and navigate to Components > Network > Adapteror
        /// or simply type nbtstat -a &lt;your hostname &lt at command prompt.
        /// e.g. nbtstat -a mymachinename or nbtstat -A 10.2.100.213.
        /// <param name="macAddress">The MAC address of the host which has to be
        /// woken up.</param>
        ///
        private static void Wakeup(string macAddress)
        {
            WOLUdpClient client = new WOLUdpClient();

            client.Connect(
                new IPAddress(0xffffffff),     //255.255.255.255  i.e broadcast
                0x2fff);                       // port = 12287
            if (client.IsClientInBrodcastMode())
            {
                int    byteCount = 0;
                byte[] bytes     = new byte[102];
                for (int trailer = 0; trailer < 6; trailer++)
                {
                    bytes[byteCount++] = 0xFF;
                }
                for (int macPackets = 0; macPackets < 16; macPackets++)
                {
                    int i = 0;
                    for (int macBytes = 0; macBytes < 6; macBytes++)
                    {
                        bytes[byteCount++] =
                            byte.Parse(macAddress.Substring(i, 2),
                                       NumberStyles.HexNumber);
                        i += 2;
                    }
                }

                int returnValue = client.Send(bytes, byteCount);
                Console.WriteLine(returnValue + " bytes sent to " + macAddress +
                                  Environment.NewLine + "Check if it's woken up. If not, try again!" +
                                  Environment.NewLine);
            }
            else
            {
                Console.WriteLine("Remote client could not be set in broadcast mode!");
            }
        }
コード例 #2
0
        /// <summary>
        /// Wake button function
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnWake_Click(object sender, RoutedEventArgs e)
        {
            _IP   = TbIPTarget.Text; //loading new imput ip
            _MAC  = TbMAC.Text;      //loading new imput MAC
            _Port = TbPort.Text;     //loading new imput Port

            if (_MAC != file.IniReadValue("CONNECTION", "MAC") || _Port != file.IniReadValue("CONNECTION", "Port") || _IP != file.IniReadValue("CONNECTION", "IPTarget"))
            {
                //Saving settings from textboxes to ini

                file.IniWriteValue("CONNECTION", "Port", TbPort.Text);
                file.IniWriteValue("CONNECTION", "MAC", TbMAC.Text);
                file.IniWriteValue("CONNECTION", "IPTarget", TbIPTarget.Text);
            }


            if (_MAC.Length > 0 && _Port.Length > 0 && _IP.Length > 0)//check texboxes for null
            {
                try
                {
                    IPAddress ip2  = IPAddress.Parse(_IP);
                    string    port = _Port;
                    int       p    = Convert.ToInt32(port);
                    if (_MAC.Length == 17)
                    {
                        macAddress = _MAC.Replace(":", "");
                        WOLUdpClient client = new WOLUdpClient();
                        client.Connect(ip2, //255.255.255.255  i.e broadcast
                                       p);  // port = 12287
                        if (client.IsClientInBrodcastMode())
                        {
                            int    byteCount = 0;
                            byte[] bytes     = new byte[102];
                            for (int trailer = 0; trailer < 6; trailer++)
                            {
                                bytes[byteCount++] = 0xFF;
                            }
                            for (int macPackets = 0; macPackets < 16; macPackets++)
                            {
                                int i = 0;
                                for (int macBytes = 0; macBytes < 6; macBytes++)
                                {
                                    bytes[byteCount++] =
                                        byte.Parse(macAddress.Substring(i, 2),
                                                   NumberStyles.HexNumber);
                                    i += 2;
                                }
                            }

                            int returnValue = client.Send(bytes, byteCount);
                            TbLog.Text = "Magic packet of " + returnValue + " bytes is sent to: " + Environment.NewLine + _IP + " / " + _MAC;
                        }
                    }
                    else
                    {
                        TbLog.Text = "Remote client could not" + Environment.NewLine + " be set in broadcast mode. Please check your settings!";
                    }
                }
                catch (Exception)
                {
                    TbLog.Text = Environment.NewLine + "Something went wrong ..check settings!";
                }
            }
            else
            {
                TbLog.Text = "All text boxes must be filled!";
            }
        }