コード例 #1
0
 /// <summary>
 /// マジックパケット (AMD Magic Packet Format) を送信します。
 /// </summary>
 /// <param name="macAddress">
 /// MAC アドレスの文字列を指定します。
 /// </param>
 /// <param name="port">
 /// リモートマシンのポート番号を指定します。
 /// 既定のポート番号は <c>2304</c> 番です。
 /// </param>
 /// <param name="count">
 /// マジックパケットを送信する回数を指定します。
 /// 既定の回数は <c>1</c> 回です。
 /// </param>
 /// <param name="interval">
 /// マジックパケットを送信する間隔を、ミリ秒単位で指定します。
 /// 既定の回数は <c>0</c> ミリ秒です。
 /// </param>
 /// <returns>
 /// マジックパケットを送信した IP アドレスの文字列配列を返します。
 /// </returns>
 public static string[] Send(string macAddress, int port = 2304, int count = 1, int interval = 0)
 => MagicPacket.SendDatagramAsync(macAddress, port, count, interval, false).Result;
コード例 #2
0
 /// <summary>
 /// マジックパケット (AMD Magic Packet Format) を送信します。
 /// </summary>
 /// <param name="macAddress">
 /// MAC アドレスの文字列を指定します。
 /// </param>
 /// <param name="port">
 /// リモートマシンのポート番号を指定します。
 /// 既定のポート番号は <c>2304</c> 番です。
 /// </param>
 /// <param name="count">
 /// マジックパケットを送信する回数を指定します。
 /// 既定の回数は <c>1</c> 回です。
 /// </param>
 /// <param name="interval">
 /// マジックパケットを送信する間隔を、ミリ秒単位で指定します。
 /// 既定の回数は <c>0</c> ミリ秒です。
 /// </param>
 /// <returns>
 /// マジックパケットを送信した IP アドレスの文字列配列を返します。
 /// </returns>
 public static Task <string[]> SendAsync(string macAddress, int port = 2304, int count = 1, int interval = 0)
 => MagicPacket.SendDatagramAsync(macAddress, port, count, interval, true);
コード例 #3
0
        // Send Datagram
        private static async Task <string[]> SendDatagramAsync(string macAddress, int port, int count, int interval, bool async)
        {
            // GET UDP Datagram (Byte Array)
            var dgram = MagicPacket.GetDatagram(macAddress);

            // List of IP Address sent
            List <string> addresses = new List <string>();

            // for NetworkInterface(s)
            foreach (var address in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                // only for IPv4
                if (address.AddressFamily == AddressFamily.InterNetwork)
                {
                    var lep = new IPEndPoint(address, 0);
                    var udp = new UdpClient(lep);
                    var ep  = new IPEndPoint(IPAddress.Broadcast, port);

                    // Sent Flag
                    bool sent = false;

                    for (int i = 0; i < count; i++)
                    {
                        // Send Datagram (Async or Sync)
                        var bytes = async ? await udp.SendAsync(dgram, dgram.Length, ep) : udp.Send(dgram, dgram.Length, ep);

                        if (bytes > 0)
                        {
                            // Set Sent Flag ON
                            sent = true;

#if DEBUG
                            Debug.Write($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] Send Magic Packet{(async ? " [Async]" : "")} (", DebugInfo.ShortName);
                            Debug.Write($"{i + 1}: MAC Address=\"{macAddress}\", {bytes} Bytes");
                            Debug.WriteLine($", Destination Port={port}, Source IP Address={lep.Address})");
#endif

                            if (i < count - 1)
                            {
                                // Wait for Interval
                                if (async)
                                {
                                    // Async
                                    await Task.Delay(interval);
                                }
                                else
                                {
                                    // NOT Async
                                    Thread.Sleep(interval);
                                }
                            }
                        }
                        else
                        {
                            // ERROR
                            throw new InvalidOperationException();
                        }
                    }

                    // Add IP Address sent
                    if (sent)
                    {
                        addresses.Add(address.ToString());
                    }
                }
            }

            // RETURN
            return(addresses.ToArray());
        }