using System; using System.Collections.Generic; using System.Text; using System.Net.Sockets; using System.Net; using System.Threading; using UnityEngine; using System.Net.NetworkInformation; namespace GeneralTools { public class UDPClient : SocketBase { IPEndPoint targetIPEnd = null; IPEndPoint selfIPEnd = null; Socket udpSocket = null; Thread threadClient = null; public UDPClient(string targetIP, int targetPort, int selfPort) { InitSocket(targetIP, targetPort, selfPort); } void InitSocket(string _ip, int _port, int _selfPort) { udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); IPAddress ip; if (string.IsNullOrEmpty(_ip)) { ip = IPAddress.Broadcast; } else { ip = IPAddress.Parse(_ip); } targetIPEnd = new IPEndPoint(ip, _port); if (_selfPort >= 0) { selfIPEnd = new IPEndPoint(IPAddress.Any, _selfPort); try { udpSocket.Bind(selfIPEnd); } catch (Exception se) { Debug.LogError(se.Message); return; } } threadClient = new Thread(RecMsg); threadClient.IsBackground = true; threadClient.Start(); } void RecMsg() { while (true) { if (socketClose) return; if (udpSocket.LocalEndPoint == null) continue; byte[] receiveByte = new byte[reveiceByteSize]; EndPoint endPoint = new IPEndPoint(IPAddress.Any, 0); int length = -1; try { length = udpSocket.ReceiveFrom(receiveByte, ref endPoint); // 接收数据,并返回数据的长度; } catch (Exception) { continue; } if (length == 0) { continue; } //保存接收到的数据 RecordReceiveMsg(receiveByte, length, udpSocket.LocalEndPoint, endPoint); } } public override void SendMsg(string strMsg) { //数据类型转换 byte[] sendByte = String2Bytes(strMsg,encoding); try { udpSocket.SendTo(sendByte, targetIPEnd); } catch (Exception se) { Debug.LogError(se.Message); udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); } } public override void SendMsg(byte[] byteMsg) { try { udpSocket.SendTo(byteMsg, targetIPEnd); } catch (Exception se) { Debug.LogError(se.Message); udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); } } public override void SocketQuit() { base.SocketQuit(); //关闭线程 if (threadClient != null) { threadClient.Interrupt(); threadClient.Abort(); } //最后关闭socket if (udpSocket != null) udpSocket.Close(); } /// /// 发送一次UDP报文 /// /// /// /// public static void SendMsgOnce(string _ip, int _port, string strMsg, Encoding _encoding = null) { //数据类型转换 byte[] sendByte = String2Bytes(strMsg, _encoding); IPEndPoint ipend = new IPEndPoint(IPAddress.Parse(_ip), _port); Socket tempSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); tempSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); try { tempSocket.SendTo(sendByte, ipend); } catch (Exception se) { Debug.LogError(se.Message); } tempSocket.Close(); } /// /// 发送一次UDP报文 /// /// /// /// public static void SendMsgOnce(string _ip, int _port, byte[] bytes) { IPEndPoint ipend = new IPEndPoint(IPAddress.Parse(_ip), _port); Socket tempSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); tempSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); try { tempSocket.SendTo(bytes, ipend); } catch (Exception se) { Debug.LogError(se.Message); } tempSocket.Close(); } } }