fix bug: couldn't convert package
This commit is contained in:
@@ -2,28 +2,54 @@ using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
|
||||
/// <summary>
|
||||
/// UDP客户端发送池
|
||||
/// </summary>
|
||||
public class UDPClientPool
|
||||
{
|
||||
private static IPAddress localhost = IPAddress.Parse("127.0.0.1");
|
||||
|
||||
/// <summary>
|
||||
/// 发送字符串
|
||||
/// </summary>
|
||||
/// <param name="endPoint">IP端点(IP地址与端口)</param>
|
||||
/// <param name="stringArray">字符串数组</param>
|
||||
/// <returns>是否成功</returns>
|
||||
public static bool SendString(IPEndPoint endPoint, string[] stringArray)
|
||||
{
|
||||
var isSuccessful = true;
|
||||
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
||||
|
||||
byte[] sendbuf = Encoding.ASCII.GetBytes(stringArray[0]);
|
||||
foreach (var str in stringArray)
|
||||
{
|
||||
byte[] sendbuf = Encoding.ASCII.GetBytes(str);
|
||||
var sendLen = socket.SendTo(sendbuf, endPoint);
|
||||
if (str.Length != sendLen) isSuccessful = false;
|
||||
}
|
||||
|
||||
var sendLen = socket.SendTo(sendbuf, endPoint);
|
||||
socket.Close();
|
||||
|
||||
if (sendLen == stringArray[0].Length) { return true; }
|
||||
if (isSuccessful) { return true; }
|
||||
else { return false; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步发送字符串
|
||||
/// </summary>
|
||||
/// <param name="endPoint">IP端点(IP地址与端口)</param>
|
||||
/// <param name="stringArray">字符串数组</param>
|
||||
/// <returns>是否成功</returns>
|
||||
public async static ValueTask<bool> SendStringAsync(IPEndPoint endPoint, string[] stringArray)
|
||||
{
|
||||
return await Task.Run(() => { return SendString(endPoint, stringArray); });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送二进制字节
|
||||
/// </summary>
|
||||
/// <param name="endPoint">IP端点(IP地址与端口)</param>
|
||||
/// <param name="buf">二进制字节</param>
|
||||
/// <returns>是否成功</returns>
|
||||
public static bool SendBytes(IPEndPoint endPoint, byte[] buf)
|
||||
{
|
||||
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
||||
@@ -34,11 +60,23 @@ public class UDPClientPool
|
||||
else { return false; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步发送二进制字节
|
||||
/// </summary>
|
||||
/// <param name="endPoint">IP端点(IP地址与端口)</param>
|
||||
/// <param name="buf">二进制字节</param>
|
||||
/// <returns>是否成功</returns>
|
||||
public async static ValueTask<bool> SendBytesAsync(IPEndPoint endPoint, byte[] buf)
|
||||
{
|
||||
return await Task.Run(() => { return SendBytes(endPoint, buf); });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送地址包
|
||||
/// </summary>
|
||||
/// <param name="endPoint">IP端点(IP地址与端口)</param>
|
||||
/// <param name="pkg">地址包</param>
|
||||
/// <returns>是否成功</returns>
|
||||
public static bool SendAddrPack(IPEndPoint endPoint, WebProtocol.SendAddrPackage pkg)
|
||||
{
|
||||
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
||||
@@ -50,11 +88,24 @@ public class UDPClientPool
|
||||
else { return false; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步发送地址包
|
||||
/// </summary>
|
||||
/// <param name="endPoint">IP端点(IP地址与端口)</param>
|
||||
/// <param name="pkg">地址包</param>
|
||||
/// <returns>是否成功</returns>
|
||||
public async static ValueTask<bool> SendAddrPackAsync(IPEndPoint endPoint, WebProtocol.SendAddrPackage pkg)
|
||||
{
|
||||
return await Task.Run(() => { return SendAddrPack(endPoint, pkg); });
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 发送数据包
|
||||
/// </summary>
|
||||
/// <param name="endPoint">IP端点(IP地址与端口)</param>
|
||||
/// <param name="pkg">数据包</param>
|
||||
/// <returns>是否成功</returns>
|
||||
public static bool SendDataPack(IPEndPoint endPoint, WebProtocol.SendDataPackage pkg)
|
||||
{
|
||||
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
||||
@@ -66,38 +117,48 @@ public class UDPClientPool
|
||||
else { return false; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步发送数据包
|
||||
/// </summary>
|
||||
/// <param name="endPoint">IP端点(IP地址与端口)</param>
|
||||
/// <param name="pkg">数据包</param>
|
||||
/// <returns>是否成功</returns>
|
||||
public async static ValueTask<bool> SendDataPackAsync(IPEndPoint endPoint, WebProtocol.SendDataPackage pkg)
|
||||
{
|
||||
return await Task.Run(() => { return SendDataPack(endPoint, pkg); });
|
||||
}
|
||||
|
||||
public static bool SendLocalHost(int port, string[] stringArray)
|
||||
/// <summary>
|
||||
/// 发送字符串到本地
|
||||
/// </summary>
|
||||
/// <param name="port">端口</param>
|
||||
/// <param name="stringArray">字符串数组</param>
|
||||
/// <returns>是否成功</returns>
|
||||
public static bool SendStringLocalHost(int port, string[] stringArray)
|
||||
{
|
||||
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
||||
|
||||
byte[] sendBytes = Encoding.ASCII.GetBytes(stringArray[0]);
|
||||
IPEndPoint ep = new IPEndPoint(localhost, port);
|
||||
|
||||
var sendLen = socket.SendTo(sendBytes, ep);
|
||||
socket.Close();
|
||||
|
||||
if (sendLen == sendBytes.Length) { return true; }
|
||||
else { return false; }
|
||||
return SendString(new IPEndPoint(localhost, port), stringArray);
|
||||
}
|
||||
|
||||
public static void CycleSendLocalHost(int times, int sleepMilliSeconds, int port, string[] stringArray)
|
||||
/// <summary>
|
||||
/// 循环发送字符串到本地
|
||||
/// </summary>
|
||||
/// <param name="times">发送总次数</param>
|
||||
/// <param name="sleepMilliSeconds">间隔时间</param>
|
||||
/// <param name="port">端口</param>
|
||||
/// <param name="stringArray">字符串数组</param>
|
||||
/// <returns>是否成功</returns>
|
||||
public static bool CycleSendStringLocalHost(int times, int sleepMilliSeconds, int port, string[] stringArray)
|
||||
{
|
||||
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
||||
byte[] sendbuf = Encoding.ASCII.GetBytes(stringArray[0]);
|
||||
IPEndPoint ep = new IPEndPoint(localhost, port);
|
||||
var isSuccessful = true;
|
||||
|
||||
while (times-- >= 0)
|
||||
{
|
||||
socket.SendTo(sendbuf, ep);
|
||||
isSuccessful = SendStringLocalHost(port, stringArray);
|
||||
if (!isSuccessful) break;
|
||||
|
||||
Thread.Sleep(sleepMilliSeconds);
|
||||
}
|
||||
|
||||
socket.Close();
|
||||
return isSuccessful;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user