Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
C# Wake On Lan Program
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Globalization;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
WakeFunction("AA:EE:CC:DD:FF"); //change me
}
//we derive our class from a standart one
public class WOLClass : UdpClient
{
public WOLClass() : base()
{ }
//this is needed to send broadcast packet
public void SetClientToBrodcastMode()
{
if (this.Active)
this.Client.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.Broadcast, 0);
}
}
//now use this class
//MAC_ADDRESS should look like '013FA049'
private void WakeFunction(string MAC_ADDRESS)
{
WOLClass client = new WOLClass();
client.Connect(new
IPAddress(0xffffffff), //255.255.255.255 i.e broadcast
0x2fff); // port=12287 let's use this one
client.SetClientToBrodcastMode();
//set sending bites
int counter = 0;
//buffer to be send
byte[] bytes = new byte[1024]; // more than enough :-)
//first 6 bytes should be 0xFF
for (int y = 0; y < 6; y++)
bytes[counter++] = 0xFF;
//now repeate MAC 16 times
for (int y = 0; y < 16; y++)
{
int i = 0;
for (int z = 0; z < 6; z++)
{
bytes[counter++] =
byte.Parse(MAC_ADDRESS.Substring(i, 2), NumberStyles.HexNumber);
i += 2;
}
}
//now send wake up packet
int reterned_value = client.Send(bytes, 1024);
}
}
}
@wilsonangga
Copy link

wilsonangga commented Feb 19, 2021

can we wake on lan using IP address?

@AlexKorsakov
Copy link
Author

AlexKorsakov commented Feb 19, 2021

@wilsonangga, as you can see, the "magic packet" consists of the MAC address copied 16 times, so the MAC address is required.
But knowing the IP address, you can find out the MAC address using the "arp" utility with the "-а" flag.
You can take the code from the answers from here.

@wilsonangga
Copy link

wilsonangga commented Feb 23, 2021

in line 60, is it "0xFF" is static for all IP Address? or it depends on IP Address? if its dependson IP Address, what should we do if the ip is 192.168.100.101? thankyou

@AlexKorsakov
Copy link
Author

AlexKorsakov commented Feb 23, 2021

Look at this, section "Magic packet":

The magic packet is a frame that is most often sent as a broadcast and that contains anywhere within its payload 6 bytes of all 255 (FF FF FF FF FF FF in hexadecimal), followed by sixteen repetitions of the target computer's 48-bit MAC address, for a total of 102 bytes.

Answering your question, yes, it "0xFF" is static for all IP Address.

@wilsonangga
Copy link

wilsonangga commented Feb 24, 2021

thanks for the information. i have make it same as the code you give, but i still can't wake my computer on LAN. i see that u have form2.cs in ur github. is it also i have to write in my code or not? thankyou for helping me..

@AlexKorsakov
Copy link
Author

AlexKorsakov commented Feb 24, 2021

No, it's not necessary.
For using Wake-On-Lan, you should enable the corresponding function in the BIOS of the device you want to enable. And just in case, try to send the packet with the firewall turned off.

@AlexKorsakov
Copy link
Author

AlexKorsakov commented Feb 24, 2021

@wilsonangga look here, i made a working project.

@wilsonangga
Copy link

wilsonangga commented Feb 25, 2021

@AlexKorsakov thankyou very much for your help, now its work. Is it possible if i also make for shutdown and restart?

@AlexKorsakov
Copy link
Author

AlexKorsakov commented Feb 25, 2021

You are welcome!
Yes, it's possible, check this project. WOL allow you only start device. If you want to control the power state of your computer, you need application with administrator permissions. In Power-Management-Service I use a Windows Service for this.

@wilsonangga
Copy link

wilsonangga commented Aug 9, 2021

Hello Alex, I want to ask you if i want to make program to shutdown remote PC using magic packet , is it possible use the code as same as the WakeOnLan? If it possible can you please help me to figured it out? Because I was stuck in my project to force shutdown the remote pc. Thankyouuu very much if you can help.

@AlexKorsakov
Copy link
Author

AlexKorsakov commented Aug 9, 2021

Hello! As far as I know, WOL is only used to wake up your PC. Shutting down the remote PC is a little more difficult. There is need to have an access to your operating system. First, you need to define:

  1. How remote access will be arranged
  2. How to shutting down PC
    As I talking earlier, you can check this project, which I've done in study time too. It used UDP protocol for remote connection, desktop application as server and windows service as client (which shutting down client PC).
    Where are you stuck in your project?

@wilsonangga
Copy link

wilsonangga commented Aug 11, 2021

In my project, I have client and server application. What I need to do is, if the user close the client application by end it in task manager (end task), I should make the server detect it and shutdown the client PC. Do you have any idea to simply solve this problem? Thanks for your answer before.

@AlexKorsakov
Copy link
Author

AlexKorsakov commented Aug 14, 2021

@wilsonangga you can use a Windows Service and client app together on a host. Service will detect a running client app and can shutdown host. Service works hiddenly for user, but for installation administration rights could be required.
Check this tutorial.

@wilsonangga
Copy link

wilsonangga commented Aug 16, 2021

I will try it. Thankyou very much for you suggestion. You have helped so much.

@satosCZ
Copy link

satosCZ commented Sep 7, 2021

Thank you, this will help me. I'll try it later. Remote shutdown and restart for me is allready solved. I used server-client, where user send request to shutdown in type of some message eg. "Shutdown-123". Other pc will receive the message and by request it will start command in cmd (all in background > "shutdown -s -f" / "shutdown -r -f"). It's lightway solution which didn't need admin permision. Once again thank you for WOL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment