From 26b8caaf333d6a5198172c65adbed37e01998850 Mon Sep 17 00:00:00 2001 From: "DESKTOP-B25GA9E\\W35" <1733709035@qq.com> Date: Tue, 7 Mar 2023 11:12:37 +0800 Subject: [PATCH] =?UTF-8?q?udp=E9=80=9A=E8=AE=AF=E8=B0=83=E6=95=B4?= =?UTF-8?q?=EF=BC=8Csscom=E5=8A=A9=E6=89=8B=E6=B5=8B=E8=AF=95=E9=80=9A?= =?UTF-8?q?=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UDPClient/Script/UDPClientView.cs | 4 ++-- .../ExtendLinkModel/UDPClient/Script/UDPUtility.cs | 12 ++++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Assets/MsgTransmitTools/ExtendLinkModel/UDPClient/Script/UDPClientView.cs b/Assets/MsgTransmitTools/ExtendLinkModel/UDPClient/Script/UDPClientView.cs index 8ee32f6..39ca840 100644 --- a/Assets/MsgTransmitTools/ExtendLinkModel/UDPClient/Script/UDPClientView.cs +++ b/Assets/MsgTransmitTools/ExtendLinkModel/UDPClient/Script/UDPClientView.cs @@ -62,13 +62,13 @@ namespace JXSoft { } public void sendMsg() { - this.GetUtility().sendData(InputSendMsg.text); + this.GetUtility().sendData(InputSendMsg.text,"192.168.1.41", 8443); } public void initUDPService() { UDPAddress = "192.168.1.41"; - UDPPort = 52101; + UDPPort = 20000; this.RegisterLinkStateEvent((int)UDPLinkState.Linking, () => { Debug.Log("UDP开始链接"); this.GetModel().linkServer(); diff --git a/Assets/MsgTransmitTools/ExtendLinkModel/UDPClient/Script/UDPUtility.cs b/Assets/MsgTransmitTools/ExtendLinkModel/UDPClient/Script/UDPUtility.cs index cd610f3..6fc9306 100644 --- a/Assets/MsgTransmitTools/ExtendLinkModel/UDPClient/Script/UDPUtility.cs +++ b/Assets/MsgTransmitTools/ExtendLinkModel/UDPClient/Script/UDPUtility.cs @@ -19,6 +19,7 @@ public class UDPUtility : IUtility public string exceptionData = ""; public Thread reciveT; public bool isTimeOut = false; + private IPEndPoint RemoteIpEndPoint; public UDPUtility() { @@ -41,7 +42,8 @@ public class UDPUtility : IUtility udpPort = port; try { - udpClient = new UdpClient(udpAddress, udpPort); + RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, udpPort); + udpClient = new UdpClient(RemoteIpEndPoint); } catch (Exception e) { @@ -71,6 +73,13 @@ public class UDPUtility : IUtility Byte[] sendBytes = Encoding.Default.GetBytes(data); udpClient.Send(sendBytes, sendBytes.Length); } + public void sendData(string data,string ip,int port) + { + if (isOpenUDP == false) + return; + Byte[] sendBytes = Encoding.Default.GetBytes(data); + udpClient.Send(sendBytes, sendBytes.Length,ip,port); + } public void RecciveMsg() { @@ -80,7 +89,6 @@ public class UDPUtility : IUtility { try { - IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint); msg = Encoding.UTF8.GetString(receiveBytes); if (msg != "")