1
0

TCPClient.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using UnityEngine;
  2. using System.Net.Sockets;
  3. using System.Text;
  4. using ToneTuneToolkit.Common;
  5. /// <summary>
  6. /// TCP发图片和文本
  7. /// </summary>
  8. public class TCPClient : SingletonMaster<TCPClient>
  9. {
  10. public string serverIP = "192.168.1.100"; // Windows服务器的IP地址
  11. public int serverPort = 1006;
  12. private TcpClient client;
  13. private NetworkStream stream;
  14. // ==================================================
  15. private void Start() => Init();
  16. private void OnDestroy() => UnInit();
  17. // ==================================================
  18. private void Init()
  19. {
  20. // Connect();
  21. }
  22. private void UnInit()
  23. {
  24. Disconnect();
  25. }
  26. // ==================================================
  27. public void SetServerIP(string value) => serverIP = value;
  28. public void SetServerPort(string value) => serverPort = int.Parse(value);
  29. // ==================================================
  30. // 连接到服务器
  31. public void Connect()
  32. {
  33. try
  34. {
  35. client = new TcpClient();
  36. client.Connect(serverIP, serverPort);
  37. stream = client.GetStream();
  38. Debug.Log("[TCP]Connected to server");
  39. }
  40. catch (System.Exception e)
  41. {
  42. Debug.LogError("[TCP]Connection error: " + e.Message);
  43. }
  44. }
  45. // 断开连接
  46. public void Disconnect()
  47. {
  48. if (stream != null) { stream.Close(); }
  49. if (client != null) { client.Close(); }
  50. Debug.Log("[TCP]Disconnected from server");
  51. }
  52. // 发送文本消息
  53. public void SendText(string message)
  54. {
  55. if (client == null || !client.Connected) { return; }
  56. try
  57. {
  58. byte[] textBytes = Encoding.UTF8.GetBytes(message);
  59. byte[] lengthBytes = System.BitConverter.GetBytes(textBytes.Length);
  60. byte[] dataType = new byte[] { 0 }; // 0 = text
  61. stream.Write(lengthBytes, 0, 4);
  62. stream.Write(dataType, 0, 1);
  63. stream.Write(textBytes, 0, textBytes.Length);
  64. Debug.Log("[TCP]Sent text: " + message);
  65. }
  66. catch (System.Exception e)
  67. {
  68. Debug.LogError("[TCP]Send text error: " + e.Message);
  69. }
  70. }
  71. // 发送图片
  72. public void SendImage(Texture2D texture)
  73. {
  74. if (client == null || !client.Connected) { Debug.LogWarning("Not connected to server"); return; }
  75. try
  76. {
  77. byte[] imageBytes = texture.EncodeToPNG();
  78. byte[] lengthBytes = System.BitConverter.GetBytes(imageBytes.Length);
  79. byte[] dataType = new byte[] { 1 }; // 1 = image
  80. stream.Write(lengthBytes, 0, 4);
  81. stream.Write(dataType, 0, 1);
  82. stream.Write(imageBytes, 0, imageBytes.Length);
  83. Debug.Log("[TCP]Sent image with size: " + texture.width + "x" + texture.height);
  84. }
  85. catch (System.Exception e)
  86. {
  87. Debug.LogError("[TCP]Send image error: " + e.Message);
  88. Disconnect();
  89. }
  90. }
  91. }