You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.8 KiB

  1. using Apewer.Internals;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. namespace Apewer.Network
  8. {
  9. /// <summary>UDP 客户端。</summary>
  10. public static class UdpClient
  11. {
  12. /// <summary>向所有服务端广播数据。</summary>
  13. /// <param name="bytes">数据。</param>
  14. /// <param name="port">服务端端口。</param>
  15. public static bool Broadcast(byte[] bytes, int port)
  16. {
  17. if (bytes == null) return false;
  18. if (bytes.LongLength == 0) return false;
  19. if (port < 0) port = 0;
  20. if (port > 65535) port = 65535;
  21. try
  22. {
  23. var uc = new System.Net.Sockets.UdpClient();
  24. var ep = new IPEndPoint(IPAddress.Parse("255.255.255.255"), port);
  25. uc.EnableBroadcast = true;
  26. uc.Send(bytes, bytes.Length, ep);
  27. uc.Close();
  28. return true;
  29. }
  30. catch { return false; }
  31. }
  32. /// <summary>向指定服务端发送数据。</summary>
  33. /// <param name="ip">服务端 IP 地址。</param>
  34. /// <param name="port">服务端端口。</param>
  35. /// <param name="bytes">数据。</param>
  36. /// <param name="await">阻塞当前线程,等待服务端返回。</param>
  37. public static byte[] Send(string ip, int port, byte[] bytes, bool await = false)
  38. {
  39. if (bytes == null) return null;
  40. if (bytes.LongLength == 0) return null;
  41. if (port < 0) port = 0;
  42. if (port > 65535) port = 65535;
  43. try
  44. {
  45. var uc = new System.Net.Sockets.UdpClient();
  46. var ep = new IPEndPoint(IPAddress.Parse(ip), port);
  47. uc.EnableBroadcast = false;
  48. uc.Send(bytes, bytes.Length, ep);
  49. var received = null as byte[];
  50. if (await)
  51. {
  52. var rep = null as IPEndPoint;
  53. received = await ? uc.Receive(ref rep) : null;
  54. }
  55. uc.Close();
  56. return received;
  57. }
  58. catch { return null; }
  59. }
  60. /// <summary>向指定服务端发送数据。</summary>
  61. /// <param name="ip">服务端 IP 地址。</param>
  62. /// <param name="port">服务端端口。</param>
  63. /// <param name="bytes">数据。</param>
  64. public static byte[] Send(byte[] bytes, int port, string ip = "127.0.0.1", bool await = false)
  65. {
  66. return Send(ip, port, bytes, false);
  67. }
  68. /// <summary>向指定服务端发送数据。</summary>
  69. /// <param name="ip">服务端 IP 地址。</param>
  70. /// <param name="port">服务端端口。</param>
  71. /// <param name="bytes">数据。</param>
  72. public static byte[] Send(int port, byte[] bytes, string ip = "127.0.0.1", bool await = false)
  73. {
  74. return Send(ip, port, bytes, false);
  75. }
  76. /// <summary>唤醒局域网中拥有指定 MAC 地址的设备。</summary>
  77. /// <param name="mac">被唤醒设备的 MAC 地址,必须是长度为 6 的字节数组。</param>
  78. public static void WakeOnLan(byte[] mac)
  79. {
  80. if (mac.Length != 6) return;
  81. var uc = new System.Net.Sockets.UdpClient();
  82. uc.Connect(IPAddress.Broadcast, 65535);
  83. var pack = new List<byte>();
  84. // 前 6 字节为 0xFF。
  85. for (int i = 0; i < 6; i++) pack.Add(255);
  86. // 目标 MAC 地址重复 16 次。
  87. for (int i = 0; i < 16; i++)
  88. {
  89. for (int j = 0; j < 6; j++) pack.Add(mac[j]);
  90. }
  91. // 发送 102 字节数据。
  92. uc.Send(pack.ToArray(), pack.Count);
  93. uc.Close();
  94. }
  95. }
  96. }