예제 #1
0
 public void Button1_Click(object sender, EventArgs e)
 {
     mac_f5 = textBox6.Text.ToString();
     Form2.GetMac1(mac_f5); //5C-B9-01-F5-4C-4D
     Form2.WakeUp(mac_f5, Form2.network, Form2.udpPort, Form2.ttl);
     this.Hide();
 }
예제 #2
0
        public static void WakeUp(string mac, string network, int udpPort, int ttl)
        {
            UdpClient  client        = default(UdpClient);
            IPEndPoint localEndPoint = default(IPEndPoint);

            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); //GetAllNetworkInterfaces retourne un tableau qui contint une instance de cet classe

            byte[] packet = new byte[102];
            int    i = default(int), j = default(int);

            byte[] macBytes = default(byte[]);

            try
            {
                macBytes = Form2.GetMac1(mac);
                // WOL packet contains a 6-bytes header and 16 times a 6-bytes sequence containing the MAC address.
                // packet =  byte(17 * 6)
                // Header of 0xFF 6 times.

                for (i = 0; i <= 5; i++)
                {
                    packet[i] = 255;
                }

                // Body of magic packet contains the MAC address repeated 16 times.

                for (i = 1; i <= 16; i++)
                {
                    for (j = 0; j <= 5; j++)
                    {
                        packet[(i * 6) + j] = macBytes[j];
                    }
                }

                /*
                 * for (int p = 0; p < packet.Length; p++)
                 * { MessageBox.Show(packet[p].ToString()); }
                 */

                foreach (NetworkInterface adapter in nics)
                {
                    // Only display informatin for interfaces that support IPv4.
                    if (adapter.Supports(NetworkInterfaceComponent.IPv4) == false)
                    {
                        continue;
                    }

                    //  UnicastIPAddressInformationCollection addresses = adapter.GetIPProperties.UnicastAddresses;
                    UnicastIPAddressInformationCollection addresses = adapter.GetIPProperties().UnicastAddresses;

                    foreach (UnicastIPAddressInformation address in addresses)
                    {
                        if (address.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            localEndPoint = new IPEndPoint(IPAddress.Parse(address.Address.ToString()), udpPort);
                            //  Debug.WriteLine("Interface: " + localEndPoint.ToString());

                            try
                            {
                                client = new UdpClient();
                                client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                                client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
                                client.ExclusiveAddressUse = false;
                                client.Client.Bind(localEndPoint);
                                client.Connect(network, udpPort);
                                client.EnableBroadcast = true;
                                client.Ttl             = (short)ttl;

                                client.Send(packet, packet.Length);
                            }
                            catch
                            {
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }