ilonic370

Untitled

Aug 26th, 2025 (edited)
5
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.20 KB | None | 0 0
  1. Agent.cs
  2.  
  3.             UdpClient client = new UdpClient(9); // UdpClient that listens to port 9
  4.             IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0); // Receive from any IP
  5.             TcpClient answerer;
  6.             byte[] received;
  7.             byte[] answerNonMagicPacket = Encoding.UTF8.GetBytes("WolUtilities");
  8.             byte[] answerMagicPacket = Encoding.UTF8.GetBytes("ShuttingDown");
  9.             string mac = GetMachineMacAddress();
  10.  
  11.             Console.WriteLine("Listening to magic packets... MAC address of this machine is: {0}", mac);
  12.  
  13.             while (true)
  14.             {
  15.                 received = client.Receive(ref endPoint); // Blocks until message is got
  16.  
  17.                 if (IsMagicPacket(received, mac))
  18.                 {
  19.                     Console.WriteLine($"Received a magic packet from {endPoint.Address}:{endPoint.Port}, answering.");
  20.                     using (answerer = new TcpClient(endPoint.Address.ToString(), endPoint.Port))
  21.                     {
  22.                         NetworkStream stream = answerer.GetStream();
  23.                         stream.Write(answerMagicPacket, 0, answerMagicPacket.Length);
  24.                     }
  25.                 }
  26.                 else
  27.                 {
  28.                     Console.WriteLine($"Received a non-magic packet from {endPoint.Address}:{endPoint.Port}, answering.");
  29.                     using (answerer = new TcpClient(endPoint.Address.ToString(), endPoint.Port))
  30.                     {
  31.                         NetworkStream stream = answerer.GetStream();
  32.                         stream.Write(answerNonMagicPacket, 0, answerNonMagicPacket.Length);
  33.                     }
  34.                 }
  35.             }
  36.  
  37. Program.cs
  38. Console.WriteLine("Hello, World!\nSending Wol packet");
  39. WakeOnLan wol = new WakeOnLan("IP", Port, "MacAddress");
  40.  
  41. wol.Send();
  42. IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0); // Receive from any IP
  43. TcpListener listener = null;
  44.  
  45. try
  46. {
  47.     listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 9);
  48.     listener.Start();
  49.  
  50.     byte[] received = new byte[512];
  51.     string data;
  52.  
  53.     using (TcpClient client = listener.AcceptTcpClient())
  54.     {
  55.         Console.WriteLine("Answerer connected!");
  56.  
  57.         NetworkStream stream = client.GetStream();
  58.  
  59.         int i;
  60.  
  61.         while ((i = stream.Read(received, 0, received.Length)) != 0)
  62.         {
  63.             // Translate data bytes to a UTF8 string.
  64.             data = Encoding.UTF8.GetString(received, 0, i);
  65.             Console.WriteLine("Received: {0}", data);
  66.            
  67.             if (data == "WolUtilities")
  68.             {
  69.                 Console.WriteLine("The client received a non-magic packet, success.");
  70.                 break;
  71.             }
  72.             else if (data == "ShuttingDown")
  73.             {
  74.                 Console.WriteLine("The client received a magic-packet, success.");
  75.                 break;
  76.             }
  77.             else
  78.             {
  79.                 Console.WriteLine(data);
  80.                 Console.WriteLine("I don't know what to do with that :(");
  81.             }
  82.  
  83.         }
  84.     }
  85. }
  86. catch (Exception ex)
  87. {
  88.     Console.WriteLine($"{ex.GetType()}: {ex.Message}");
  89. }
  90. finally
  91. {
  92.     Console.ReadLine();
  93. }
Advertisement
Add Comment
Please, Sign In to add comment