SocketClient.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using System.Threading;
  7. using UnityEngine;
  8. public class SocketClient
  9. {
  10. private static SocketClient _instance;
  11. public static SocketClient Instance
  12. {
  13. get
  14. {
  15. if (_instance == null)
  16. {
  17. _instance = new SocketClient();
  18. }
  19. return _instance;
  20. }
  21. }
  22. private Socket socketWatch = null;
  23. public Thread threadWatch = null;
  24. private bool isRec = true;
  25. public bool isAuthorSuccess = false;
  26. private string clientConfigFilePath = Path.Combine(Application.streamingAssetsPath, "serverconfig.json");
  27. public void StartConnect()
  28. {
  29. AuthorConfig authorConfig = JsonUtility.FromJson<AuthorConfig>(File.ReadAllText(clientConfigFilePath));
  30. socketWatch = new Socket(AddressFamily.InterNetwork , SocketType.Stream , ProtocolType.Tcp);
  31. IPAddress address = IPAddress.Parse(authorConfig.UsbConnectIp);
  32. IPEndPoint endPoint = new IPEndPoint(address , authorConfig.UsbConnectPort);
  33. socketWatch.Connect(endPoint);
  34. threadWatch = new Thread(RecMsg);
  35. threadWatch.IsBackground = true;
  36. threadWatch.Start();
  37. }
  38. private void RecMsg()
  39. {
  40. while (true)
  41. {
  42. byte[] arrRecMsg = new byte[1024 * 1024];
  43. int length = -1;
  44. try
  45. {
  46. length = socketWatch.Receive(arrRecMsg);
  47. if (length == 0)
  48. {
  49. InfoShow.Instance.LogNetError("已与VLSC断开连接,请重新连接或检查网络状况!");
  50. break;
  51. }
  52. string str = Encoding.UTF8.GetString(arrRecMsg , 0 , length);
  53. if (str.Contains("PNNI_SS_AuthorSucceeded"))
  54. {
  55. Debug.Log("授权席位获取成功!");
  56. isAuthorSuccess = true;
  57. AuthorConfig clientConfig = JsonUtility.FromJson<AuthorConfig>(File.ReadAllText(clientConfigFilePath));
  58. }
  59. else if (str.Contains("PNNI_SS_AuthorSeatFull"))
  60. {
  61. InfoShow.Instance.LogAuthorError("服务授权席位已满,请联系管理员添加授权席位!");
  62. break;
  63. }
  64. else if (str.Contains("PNNI_SS_IdentifierError"))
  65. {
  66. InfoShow.Instance.LogError("系统标识符错误,请联系管理员修改标识符!");
  67. break;
  68. }
  69. else
  70. {
  71. if (!str.Contains("收到信息"))
  72. InfoShow.Instance.LogError(str);
  73. }
  74. }
  75. catch (SocketException ex)
  76. {
  77. InfoShow.Instance.LogNetError("VLSC连接异常: " + ex.Message);
  78. break;
  79. }
  80. catch (Exception ex)
  81. {
  82. InfoShow.Instance.LogError("异常:" + ex.Message);
  83. break;
  84. }
  85. Thread.Sleep(100);
  86. }
  87. }
  88. public void SendMsg(string sendMsg)
  89. {
  90. byte[] strSendMsg = Encoding.UTF8.GetBytes(sendMsg);
  91. socketWatch.Send(strSendMsg);
  92. Debug.Log("客户端发出:" + DateTime.Now + " " + sendMsg);
  93. }
  94. /// <summary>
  95. /// Close the socket safely.
  96. /// </summary>
  97. /// <param name="socket">The socket.</param>
  98. public void SafeClose()
  99. {
  100. if (socketWatch == null)
  101. {
  102. return;
  103. }
  104. if (!socketWatch.Connected)
  105. {
  106. return;
  107. }
  108. try
  109. {
  110. isRec = false;
  111. threadWatch.Abort();
  112. }
  113. catch (Exception ex)
  114. {
  115. Debug.LogError(ex.Message.ToString());
  116. }
  117. try
  118. {
  119. socketWatch.Shutdown(SocketShutdown.Both);
  120. socketWatch.Close();
  121. }
  122. catch (Exception ex)
  123. {
  124. Debug.LogError(ex.Message.ToString());
  125. }
  126. }
  127. }