UDPServer.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using UnityEngine;
  2. using System.Threading;
  3. using System.Net.Sockets;
  4. using System.Text;
  5. using System.Net;
  6. using System;
  7. namespace Rokid.MRC
  8. {
  9. public class Define
  10. {
  11. public const int UTP_BroadPort = 8635;
  12. //ServerPort前后端不一致也没关系
  13. public const int UDP_MultiServerPort = 8630;
  14. //ClientPort就是前后端统一的发送和接收端口,必须一致
  15. public const int UDP_MultiClientPort = 8640;
  16. //多播组必须一致
  17. public const string UDP_MultiCastIP = "239.0.0.1";
  18. }
  19. //服务端广播消息
  20. public class UDPServer
  21. {
  22. private string castMsg = null;
  23. private byte[] sendData;
  24. private static IPEndPoint iPEndPoint;
  25. private static Socket socket;
  26. private Thread thread;
  27. public bool StopBroadcast = false;
  28. public int castInterval = 2000;
  29. public void Init(bool broadCast)
  30. {
  31. //创建发送数据
  32. castMsg = SDKManager.Instance.GetDeviceName();
  33. sendData = Encoding.ASCII.GetBytes(castMsg);
  34. StopBroadcast = false;
  35. if(broadCast)
  36. {
  37. BroadcastWithSocket();
  38. //BroadCastWithUDPClient();
  39. }
  40. else
  41. {
  42. MultiCast();
  43. }
  44. }
  45. public void OnDestroy()
  46. {
  47. StopBroadcast = true;
  48. }
  49. //创建UdpClient对象
  50. public UdpClient broadUdp = new UdpClient();
  51. public Thread broadCastThread;
  52. public void BroadCastWithUDPClient()
  53. {
  54. //调用UdpClient对象的Connect方法建立默认远程主机
  55. //127.0.0.1是回环地址,用于测试本机内连接
  56. // udp.Connect("127.0.0.1", 888);
  57. //192.168.1.255用来发到其他的主机上
  58. broadUdp.Connect("10.91.8.255", Define.UTP_BroadPort);
  59. broadCastThread = new Thread(() =>
  60. {
  61. while(true)
  62. {
  63. try
  64. {
  65. if(StopBroadcast)
  66. break;
  67. ////定义一个字节数组,用来存放发送到远程主机的信息
  68. //Byte[] sendBytes = Encoding.Default.GetBytes("(" + DateTime.Now.ToLongTimeString() + ")节目预报:八点有大型晚会,请收听");
  69. //Debug.Log("(" + DateTime.Now.ToLongTimeString() + ")节目预报:八点有大型晚会,请收听");
  70. ////调用UdpClient对象的Send方法将UDP数据报发送到远程主机
  71. //udp.Send(sendBytes, sendBytes.Length);
  72. broadUdp.Send(sendData, sendData.Length);
  73. Debug.Log(GetType() + " BroadCastWithUDPClient IP:" + castMsg);
  74. Thread.Sleep(castInterval);//线程休眠2秒
  75. }
  76. catch(Exception ex)
  77. {
  78. Console.WriteLine(ex.Message);
  79. }
  80. }
  81. broadUdp.Close();
  82. if(broadCastThread.ThreadState == ThreadState.Running)
  83. {
  84. Debug.Log(GetType() + " BroadCastWithUDPClient UDP Thread Closed");
  85. broadCastThread.Abort();
  86. }
  87. });
  88. broadCastThread.Start();//启动线程
  89. }
  90. //要通过其进行通信的本地端口号
  91. private UdpClient multiUdp = new UdpClient(Define.UDP_MultiServerPort);
  92. private Thread multiThread;
  93. private void MultiCast()
  94. {
  95. //将UdpClient添加到多播组
  96. IPAddress addr = IPAddress.Parse(Define.UDP_MultiCastIP);
  97. multiUdp.JoinMulticastGroup(addr);
  98. //目的地的IP地址和端口号
  99. IPEndPoint multicast = new IPEndPoint(IPAddress.Parse(Define.UDP_MultiCastIP), Define.UDP_MultiClientPort);
  100. Debug.Log(GetType() + " MultiCast Local IP:" + multiUdp.Client.LocalEndPoint.ToString());
  101. multiThread = new Thread(() =>
  102. {
  103. while(true)
  104. {
  105. try
  106. {
  107. if(StopBroadcast)
  108. {
  109. break;
  110. }
  111. //调用UdpClient对象的Send方法将UDP数据报发送到位于指定远程终结点的主机
  112. multiUdp.Send(sendData, sendData.Length, multicast);
  113. //Debug.Log(GetType() + " MultiCast Msg:" + castMsg);
  114. Thread.Sleep(castInterval);
  115. }
  116. catch(Exception ex)
  117. {
  118. Console.WriteLine(ex.Message);
  119. }
  120. }
  121. //if(addr != null)
  122. //{
  123. // multiUdp.DropMulticastGroup(addr);
  124. // addr = null;
  125. //}
  126. multiUdp.Close();
  127. if(multiThread.ThreadState == ThreadState.Running)
  128. {
  129. Debug.Log(GetType() + " MultiCast UDP Thread Closed");
  130. multiThread.Abort();
  131. }
  132. });
  133. multiThread.Start();//启动线程
  134. }
  135. //开启线程,持续广播自己的IP地址
  136. private void BroadcastWithSocket()
  137. {
  138. iPEndPoint = new IPEndPoint(IPAddress.Broadcast, Define.UTP_BroadPort);
  139. Debug.Log(iPEndPoint.AddressFamily);
  140. socket = new Socket(iPEndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
  141. socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
  142. Debug.Log(socket.EnableBroadcast);
  143. socket.EnableBroadcast = true;
  144. thread = new Thread(() =>
  145. {
  146. while(true)
  147. {
  148. if(StopBroadcast)
  149. break;
  150. Debug.Log(GetType() + " BroadcastWithSocket IP:" + castMsg);
  151. socket.SendTo(sendData, iPEndPoint);
  152. //暂停几秒再发送
  153. Thread.Sleep(castInterval);
  154. }
  155. socket.Close();
  156. if(thread.ThreadState == ThreadState.Running)
  157. {
  158. Debug.Log(GetType() + " BroadcastWithSocket UDP Thread Closed");
  159. thread.Abort();
  160. }
  161. });
  162. thread.Start();
  163. }
  164. }
  165. }