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
|
{
|
/// <summary>
|
/// 用于UDP发送的网络服务类
|
/// </summary>
|
UdpClient udpcSend = null;
|
|
IPEndPoint localIpep = null;
|
|
/// <summary>
|
/// 发送信息
|
/// </summary>
|
/// <param name="obj"></param>
|
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 { }
|
}
|
|
/// <summary>
|
/// 发送信息
|
/// </summary>
|
/// <param name="obj"></param>
|
public void SendMessage(byte[] data)
|
{
|
try
|
{
|
var config = Helpers.Tool.GetSystemConfig().baseSysConfg;
|
var ipConfig = "127.0.0.1";
|
var portConfig = 53470;
|
var toIp = "255.255.255.255";
|
var toPort = 53490;
|
if (config != null)
|
{
|
if (!string.IsNullOrWhiteSpace(config.sendIp))
|
{
|
ipConfig = config.sendIp;
|
}
|
if (!string.IsNullOrWhiteSpace(config.sendPort))
|
{
|
int.TryParse(config.sendPort, out portConfig);
|
}
|
if (!string.IsNullOrWhiteSpace(config.toIp))
|
{
|
toIp = config.toIp;
|
}
|
if (!string.IsNullOrWhiteSpace(config.toPort))
|
{
|
int.TryParse(config.toPort, out toPort);
|
}
|
}
|
else
|
{
|
ipConfig = "127.0.0.1";
|
}
|
localIpep = new IPEndPoint(IPAddress.Parse(ipConfig), portConfig); // 本机IP和监听端口号
|
|
udpcSend = new UdpClient(localIpep);
|
|
byte[] sendbytes = data;
|
IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Parse(toIp), toPort); // 发送到的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 { }
|
}
|
}
|
}
|