using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; namespace WOLAwaken.Helper { public class SendUdpPack { /// /// 用于UDP发送的网络服务类 /// UdpClient udpcSend = null; IPEndPoint localIpep = null; /// /// 发送信息 /// /// public void SendMessage(string obj) { try { localIpep = new IPEndPoint(IPAddress.Parse("192.168.2.111"), 53470); // 本机IP和监听端口号 udpcSend = new UdpClient(localIpep); string message = obj; byte[] sendbytes = Encoding.Unicode.GetBytes(message); IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 53490); // 发送到的IP地址和端口号 udpcSend.Send(sendbytes, sendbytes.Length, remoteIpep); //udpcSend.Close(); } catch { } } /// /// 发送信息 /// /// public void SendMessage(byte[] data) { try { localIpep = new IPEndPoint(IPAddress.Parse("192.168.2.111"), 53470); // 本机IP和监听端口号 udpcSend = new UdpClient(localIpep); byte[] sendbytes = data; IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 53490); // 发送到的IP地址和端口号 string str = ""; foreach (var a in sendbytes) { str += $"|{Convert.ToInt32(a)}"; } Console.WriteLine($"即将发送开机包:{Convert.ToString(str)}"); udpcSend.Send(sendbytes, sendbytes.Length, remoteIpep); Console.WriteLine($"已发送:{str}"); udpcSend.Close(); } catch { } } } }