MagicPacket
マジックパケット。いわゆる Wakeup On LAN(WOL)
ブロードキャストで投げる。
届かないときは諦めが肝心。
MagicPacket仕様
0xff * 6
MAC * 6
class MagicPacket { public MagicPacket() { } public void SendPacket( string macString ) { byte[] mac = new byte[6]; string[] macSplitStrings = macString.Split( ':', '-' ); if ( macSplitStrings.Length != 6 ) { throw new ArgumentException( "mac アドレスの指定が不正です。", "macString" ); } for ( int i = 0; i < mac.Length; i++ ) { mac[i] = byte.Parse( macSplitStrings[i], NumberStyles.HexNumber); } SendPacket( mac ); } public void SendPacket( byte[] mac ) { byte[] packetData = CreatePacketData( mac ); Socket socket = new Socket( AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp ); socket.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1 ); socket.SendTo(packetData, SocketFlags.None, new IPEndPoint( IPAddress.Parse("255.255.255.255" ), 7 )); socket.Close(); } private static byte[] CreatePacketData( byte[] mac ) { const int HEADER_SIZE = 6; const int MAC_SEND_COUNT = 16; byte[] packetData = new byte[HEADER_SIZE + ( MAC_SEND_COUNT * mac.Length )]; for ( int i = 0; i < HEADER_SIZE; i++ ) { packetData[i] = 0xff; } for ( int i = 0; i < MAC_SEND_COUNT; i++ ) { Array.Copy( mac, 0, packetData, i * mac.Length + HEADER_SIZE, mac.Length ); } return packetData; } }