Source Viewer:  wakeup.cs

Font Size:
Arrays.asmx  asmx2wsdl.cs  assyq.cs  hash.cs  ListTest.cs  ListTest.VB.vb  MyIpConfig.cs  Printer.cs  Printers.aspx  PrivateReflection2.cs  RegexVerifier.cs  rtfcontrol.cs  wakeup.cs 
AES-example  books  CustomTabControl  data  emacs  Embedded Resources  ftp  interop  itext  Java  js-unzip  misc  MQ  MSOffice  Mvc.HelloWorld  perl  streams  Tar  timer  web-services  WPF  xml  xml-serialization  xml1  zips 

// wakeup.cs // // Wakes a Wake-on-Lan capable device on the LAN, using the magic packet // // Thu, 16 Oct 2003 15:11 // using System; using System.Net; using System.Net.Sockets; namespace Ionic { public class WakeOnLan { public const int MAC_ADDR_BYTES= 6; /// <remarks> /// Constructs and returns a magic packet for the given MAC address. /// A Magic Packet is 6 bytes of FF followed by the MAC address 16 times. /// </remarks> public static byte[] GetMagicPacket(string macaddr) { byte[] packet = new byte[6 + 16 * MAC_ADDR_BYTES]; int i,j; string[] bytes= macaddr.Split(new char[] {':',',',';'}); byte[] macbytes= new byte[6]; if (bytes.Length != 6) throw(new Exception("Bad MAC Addr.")); for (i=0; i < 6; i++) { packet[i]=0xff; string b= bytes[i]; if (b.StartsWith("0x")) b= b.Substring(2,2); macbytes[i]= (byte) Int32.Parse(b, System.Globalization.NumberStyles.HexNumber); } for (j=0; j < 16; j++) { for (i=0; i < 6; i++) packet[j*6+i]=macbytes[i]; } return packet; } public static void Wakeup(string mac) { Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp ); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); byte[] message= GetMagicPacket(mac); IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, 0x2fff); socket.SendTo(message,iep); } public static void Main(string[] args) { string s= "00:d0:59:12:0f:16"; // string representation of mac addr try { if (args.Length == 0) Wakeup(s); else { foreach (string mac in args) Wakeup(mac); } } catch (Exception e) { System.Console.WriteLine("Exception: " + e); } } } }

The srcview page has been enjoyed 291574 times since 18 September 2003