297 lines
9.8 KiB
C#
297 lines
9.8 KiB
C#
|
|
using AUTS.Services.Manager;
|
|||
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Net;
|
|||
|
|
using System.Net.NetworkInformation;
|
|||
|
|
using System.Net.Sockets;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Threading;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
|
|||
|
|
namespace AUTS.Services
|
|||
|
|
{
|
|||
|
|
public partial class UDPHelp
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 用于UDP发送的网络服务类Socket
|
|||
|
|
/// </summary>
|
|||
|
|
static Socket socketRecv = null;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 本机IP端口信息
|
|||
|
|
/// </summary>
|
|||
|
|
static IPEndPoint localIpep = null;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 开关:在监听UDP报文阶段为true,否则为false
|
|||
|
|
/// </summary>
|
|||
|
|
static bool IsUdpcRecvStart = false;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 线程:不断监听UDP报文
|
|||
|
|
/// </summary>
|
|||
|
|
static Thread thrRecv;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 开启监听
|
|||
|
|
/// </summary>
|
|||
|
|
public static void StartReceive()
|
|||
|
|
{
|
|||
|
|
//if (!IsUdpcRecvStart) // 未监听的情况,开始监听
|
|||
|
|
//{
|
|||
|
|
//localIpep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 7254); // 本机IP和监听端口号
|
|||
|
|
localIpep = new IPEndPoint(IPAddress.Any, 5920); // 本机IP和监听端口号
|
|||
|
|
|
|||
|
|
IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
|
|||
|
|
//IPEndPoint[] ipEndPoints = ipProperties.GetActiveUdpListeners();
|
|||
|
|
var portCount = ipProperties.GetActiveUdpListeners().Where(x => x.Port == 5920).Count();
|
|||
|
|
|
|||
|
|
if (portCount == 0)
|
|||
|
|
{
|
|||
|
|
socketRecv = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
|||
|
|
socketRecv.Bind(localIpep);
|
|||
|
|
|
|||
|
|
thrRecv = new Thread(new ThreadStart(ReceiveMessage)); //开辟一个新线程
|
|||
|
|
|
|||
|
|
if (thrRecv.ThreadState != ThreadState.Running)
|
|||
|
|
{
|
|||
|
|
thrRecv.Start();
|
|||
|
|
IsUdpcRecvStart = true;
|
|||
|
|
Logs.WriteTimingUDPLog("UDP监听器已成功启动,IP:" + localIpep);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
//}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 关闭监听
|
|||
|
|
/// </summary>
|
|||
|
|
public static void StopReceive()
|
|||
|
|
{
|
|||
|
|
if (IsUdpcRecvStart)
|
|||
|
|
{
|
|||
|
|
thrRecv.Abort(); // 必须先关闭这个线程,否则会异常
|
|||
|
|
//udpcRecv.Close();
|
|||
|
|
socketRecv.Close();
|
|||
|
|
IsUdpcRecvStart = false;
|
|||
|
|
Logs.WriteTimingUDPLog("UDP监听器已成功关闭");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 接收数据
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="obj"></param>
|
|||
|
|
private static void ReceiveMessage()
|
|||
|
|
{
|
|||
|
|
while (IsUdpcRecvStart)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
// 接收
|
|||
|
|
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
|
|||
|
|
EndPoint Remote = (EndPoint)sender;
|
|||
|
|
byte[] data = new byte[256];
|
|||
|
|
int len = socketRecv.ReceiveFrom(data, ref Remote);
|
|||
|
|
|
|||
|
|
string result = BitConverter.ToString(data, 0, len);
|
|||
|
|
|
|||
|
|
UdpMsgHandle(data, len, Remote);
|
|||
|
|
|
|||
|
|
Logs.WriteTimingUDPLog("UDP接收数据:{来源IP:" + Remote + " ,数据包:" + result + " }");
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
Logs.WriteErrorTimingUDPLog("UDP接收错误:", ex);
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 发送信息
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="obj"></param>
|
|||
|
|
private static void SendMessage(object obj)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
string message = (string)obj;
|
|||
|
|
byte[] sendbytes = Encoding.Unicode.GetBytes(message);
|
|||
|
|
IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8819); // 发送到的IP地址和端口号
|
|||
|
|
socketRecv.SendTo(sendbytes, sendbytes.Length, SocketFlags.None, remoteIpep);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
Logs.WriteErrorTimingUDPLog("UDP发送", ex);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 处理UDP信息
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="data"></param>
|
|||
|
|
/// <param name="len"></param>
|
|||
|
|
/// <param name="Remote"></param>
|
|||
|
|
public static void UdpMsgHandle(byte[] data, int len, EndPoint Remote)
|
|||
|
|
{
|
|||
|
|
var keyNum = Convert.ToInt32(data[2]);//数据包长度
|
|||
|
|
|
|||
|
|
var checkValue = data[3];//校验位
|
|||
|
|
Array.Clear(data, 3, 1);//data 从起始位置4, 删除1个位置
|
|||
|
|
var packetSum = AddMsgByte(data);//数据包累加
|
|||
|
|
var checkSum = (byte)((255 - packetSum) & 0xFF);
|
|||
|
|
|
|||
|
|
if ((len - keyNum) == 5 && checkSum == checkValue)
|
|||
|
|
{
|
|||
|
|
var sID = BitConverter.ToInt32(data, 5);
|
|||
|
|
DevManageHelp.UdpDevServiceHandle(sID);
|
|||
|
|
|
|||
|
|
IPEndPoint endpoint = Remote as IPEndPoint;
|
|||
|
|
|
|||
|
|
var ipByte = endpoint.Address.GetAddressBytes();
|
|||
|
|
|
|||
|
|
var retMsg = new byte[256];
|
|||
|
|
Buffer.BlockCopy(data, 0, retMsg, 0, 3);
|
|||
|
|
retMsg[4] = Convert.ToByte(data[4] + 0xA0);
|
|||
|
|
|
|||
|
|
Buffer.BlockCopy(ipByte, 0, retMsg, 5, ipByte.Length);
|
|||
|
|
|
|||
|
|
var num = AddMsgByte(retMsg);
|
|||
|
|
retMsg[3] = (byte)((255 - num) & 0xFF);
|
|||
|
|
socketRecv.SendTo(retMsg, len, SocketFlags.None, Remote);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 累加十六进制数据
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="retMsg"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static int AddMsgByte(byte[] retMsg)
|
|||
|
|
{
|
|||
|
|
//var len = retMsg.Length;
|
|||
|
|
var num = 0;
|
|||
|
|
for (var i = 0; i < retMsg.Length; i++)
|
|||
|
|
{
|
|||
|
|
num += retMsg[i];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return num;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public partial class UDPHelp_New
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 累加十六进制数据
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="retMsg"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static int AddMsgByte(byte[] retMsg)
|
|||
|
|
{
|
|||
|
|
//var len = retMsg.Length;
|
|||
|
|
var num = 0;
|
|||
|
|
for (var i = 0; i < retMsg.Length; i++)
|
|||
|
|
{
|
|||
|
|
num += retMsg[i];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return num;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static void CreatSocket()
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
int port = 5930;
|
|||
|
|
string host = "127.0.0.1";
|
|||
|
|
|
|||
|
|
/**/
|
|||
|
|
///创建终结点(EndPoint)
|
|||
|
|
IPAddress ip = IPAddress.Parse(host); //把ip地址字符串转换为IPAddress类型的实例
|
|||
|
|
IPEndPoint ipe = new IPEndPoint(ip, port); //用指定的端口和ip初始化IPEndPoint类的新实例
|
|||
|
|
|
|||
|
|
/**/
|
|||
|
|
///创建终结点(EndPoint)
|
|||
|
|
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
|||
|
|
|
|||
|
|
foreach (IPAddress address in Dns.GetHostEntry(host).AddressList)
|
|||
|
|
{
|
|||
|
|
if (address.AddressFamily.ToString() == "InterNetwork")
|
|||
|
|
{
|
|||
|
|
IPAddress hostIP = address;
|
|||
|
|
IPEndPoint ipe2 = new IPEndPoint(address, 5930);
|
|||
|
|
socket.Bind(ipe2); //绑定EndPoint对像
|
|||
|
|
//socket.Listen(0); //开始监听
|
|||
|
|
|
|||
|
|
//接受到client连接,为此连接建立新的socket,并接受信息
|
|||
|
|
while (true) //定义循环,以便可以简历N次连接
|
|||
|
|
{
|
|||
|
|
Socket temp = socket.Accept(); //为新建连接创建新的socket
|
|||
|
|
|
|||
|
|
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
|
|||
|
|
EndPoint Remote = (EndPoint)sender;
|
|||
|
|
|
|||
|
|
byte[] recvBytes = new byte[256];
|
|||
|
|
int bytes = temp.ReceiveFrom(recvBytes, ref Remote); //从客户端接受信息
|
|||
|
|
|
|||
|
|
var sID = BitConverter.ToInt32(recvBytes, 5);
|
|||
|
|
DevManageHelp.UdpDevServiceHandle(sID);
|
|||
|
|
|
|||
|
|
string recvStr = BitConverter.ToString(recvBytes, 0, bytes);
|
|||
|
|
|
|||
|
|
var retMsg = UdpMsgHandle_New(recvBytes, bytes, Remote);
|
|||
|
|
temp.Send(retMsg, bytes, 0);//返回信息给客户端
|
|||
|
|
|
|||
|
|
if (temp != null)
|
|||
|
|
temp.Close();
|
|||
|
|
|
|||
|
|
Logs.WriteTimingUDPLog("UDP接收-IP:" + Remote + ",数据包:" + recvStr);
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
Logs.WriteErrorTimingUDPLog("UDP监听器开启错误:", ex);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static byte[] UdpMsgHandle_New(byte[] data, int len, EndPoint Remote)
|
|||
|
|
{
|
|||
|
|
var retMsg = new byte[256];
|
|||
|
|
var keyNum = Convert.ToInt32(data[2]);
|
|||
|
|
if ((len - keyNum) == 5)
|
|||
|
|
{
|
|||
|
|
IPEndPoint endpoint = Remote as IPEndPoint;
|
|||
|
|
//IPAddress ip = endpoint.Address;
|
|||
|
|
|
|||
|
|
var ipByte = endpoint.Address.GetAddressBytes();
|
|||
|
|
//var ipByte = new byte[] { 183, 12, 223, 210 };
|
|||
|
|
|
|||
|
|
retMsg[0] = data[0];
|
|||
|
|
retMsg[1] = data[1];
|
|||
|
|
retMsg[2] = data[2];
|
|||
|
|
retMsg[4] = Convert.ToByte(data[4].ToInt() + 160);
|
|||
|
|
|
|||
|
|
retMsg[5] = ipByte[0];
|
|||
|
|
retMsg[6] = ipByte[1];
|
|||
|
|
retMsg[7] = ipByte[2];
|
|||
|
|
retMsg[8] = ipByte[3];
|
|||
|
|
|
|||
|
|
var num = AddMsgByte(retMsg);
|
|||
|
|
retMsg[3] = (byte)((255 - num) & 0xFF);
|
|||
|
|
}
|
|||
|
|
return retMsg;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|