当前位置:   article > 正文

unity UDP 通信 判断字符串相等问题 ( unity UDP异步接收消息)_unity udp接收字符串

unity udp接收字符串

背景:跟上海的公司大佬一起开发一个项目,他用 c++ 写后端 我用 c# 写前端,用 udp 通信,我这边能收发消息,可就是怎么样都用收到的消息跟另一个字符判断不了相等,麻烦了一天多的 Bug,头都大佬

用 list 遍历出来接到的字节:

  1. byte[] data = state.UDPClient.EndReceive(ar, ref ipep);
  2. //这里可以做一个数据检查 如:void CheckData(data)
  3. string msgStr = Encoding.UTF8.GetString(data, 0, data.Length);
  4. List<char> list = new List<char>();
  5. foreach (char s in msgStr)
  6. {
  7. if (s != ' ')
  8. {
  9. list.Add(s);
  10. }
  11. }
  12. msgStr = list.ToString();
  13. foreach (var item in list)
  14. {
  15. print("list char" + item);
  16. }

原理:是c++ 在发送消息的时候 会在字节后面默认加一个 \0 的字节,如果用c#接收消息,可以将最后一个字符给截掉

  1. receiveData = client.Receive(ref remotePoint);//接收数据
  2. receiveString = Encoding.Default.GetString(receiveData);
  3. string s = receiveString.Substring(0, receiveString.Length - 1);

unity UDP 异步接收消息代码:

  1. using UnityEngine;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Threading;
  5. using System;
  6. using System.Text;
  7. using System.Collections.Generic;
  8. using UnityEngine.SceneManagement;
  9. public class UDPAsyncRecvive : MonoBehaviour
  10. {
  11. private bool ChangeScene;
  12. string receiveString = null;
  13. void Start()
  14. {
  15. ThreadRecvive();
  16. }
  17. void Update()
  18. {
  19. if (ChangeScene)
  20. {
  21. ChangeScene = false;
  22. receiveString = null;
  23. SceneManager.LoadScene("Scene0-ZLoadingScene");
  24. Debug.Log("切换场景");
  25. }
  26. }
  27. /// <summary>
  28. /// 开始线程接收
  29. /// </summary>
  30. private void ThreadRecvive()
  31. {
  32. //开一个新线程接收UDP发送的数据
  33. new Thread(delegate () //delegate()也可写成 ()=>
  34. {
  35. IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9000);
  36. UdpClient udp = new UdpClient(ipep);
  37. UDPState state = new UDPState(ipep, udp);
  38. udp.BeginReceive(CallBackRecvive, state); //异步接收
  39. })
  40. { IsBackground = true }.Start(); //设置为后台线程,并开启线程
  41. }
  42. /// <summary>
  43. /// 异步接收回调
  44. /// </summary>
  45. /// <param name="ar"></param>
  46. private void CallBackRecvive(IAsyncResult ar)
  47. {
  48. UDPState state = ar.AsyncState as UDPState;
  49. if (state != null)
  50. {
  51. IPEndPoint ipep = state.IPEP;
  52. //这里接收到数据后,应该做数据完整检查,这里只是单纯做一个接收
  53. byte[] data = state.UDPClient.EndReceive(ar, ref ipep);
  54. //这里可以做一个数据检查 如:void CheckData(data)
  55. string receiveString = Encoding.UTF8.GetString(data, 0, data.Length);
  56. string msgStr = receiveString.Substring(0, receiveString.Length - 1);
  57. if (msgStr.Trim().CompareTo("STATE-END") == 0)
  58. {
  59. ChangeScene = true;
  60. Debug.Log("切换场景");
  61. }
  62. //继续接收下一条消息
  63. state.UDPClient.BeginReceive(CallBackRecvive, state);
  64. }
  65. }
  66. }
  67. public class UDPState
  68. {
  69. private UdpClient udpClient;
  70. public UdpClient UDPClient
  71. {
  72. get { return udpClient; }
  73. }
  74. private IPEndPoint ipep;
  75. public IPEndPoint IPEP
  76. {
  77. get { return ipep; }
  78. }
  79. //构造函数
  80. public UDPState(IPEndPoint ipep, UdpClient udpClient)
  81. {
  82. this.ipep = ipep;
  83. this.udpClient = udpClient;
  84. }
  85. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/114152?site
推荐阅读
相关标签
  

闽ICP备14008679号