| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- using UnityEngine;
- using System.Net.Sockets;
- using System.Text;
- using ToneTuneToolkit.Common;
- /// <summary>
- /// TCP发图片和文本
- /// </summary>
- public class TCPClient : SingletonMaster<TCPClient>
- {
- public string serverIP = "192.168.1.100"; // Windows服务器的IP地址
- public int serverPort = 1006;
- private TcpClient client;
- private NetworkStream stream;
- // ==================================================
- private void Start() => Init();
- private void OnDestroy() => UnInit();
- // ==================================================
- private void Init()
- {
- // Connect();
- }
- private void UnInit()
- {
- Disconnect();
- }
- // ==================================================
- public void SetServerIP(string value) => serverIP = value;
- public void SetServerPort(string value) => serverPort = int.Parse(value);
- // ==================================================
- // 连接到服务器
- public void Connect()
- {
- try
- {
- client = new TcpClient();
- client.Connect(serverIP, serverPort);
- stream = client.GetStream();
- Debug.Log("[TCP]Connected to server");
- }
- catch (System.Exception e)
- {
- Debug.LogError("[TCP]Connection error: " + e.Message);
- }
- }
- // 断开连接
- public void Disconnect()
- {
- if (stream != null) { stream.Close(); }
- if (client != null) { client.Close(); }
- Debug.Log("[TCP]Disconnected from server");
- }
- // 发送文本消息
- public void SendText(string message)
- {
- if (client == null || !client.Connected) { return; }
- try
- {
- byte[] textBytes = Encoding.UTF8.GetBytes(message);
- byte[] lengthBytes = System.BitConverter.GetBytes(textBytes.Length);
- byte[] dataType = new byte[] { 0 }; // 0 = text
- stream.Write(lengthBytes, 0, 4);
- stream.Write(dataType, 0, 1);
- stream.Write(textBytes, 0, textBytes.Length);
- Debug.Log("[TCP]Sent text: " + message);
- }
- catch (System.Exception e)
- {
- Debug.LogError("[TCP]Send text error: " + e.Message);
- }
- }
- // 发送图片
- public void SendImage(Texture2D texture)
- {
- if (client == null || !client.Connected) { Debug.LogWarning("Not connected to server"); return; }
- try
- {
- byte[] imageBytes = texture.EncodeToPNG();
- byte[] lengthBytes = System.BitConverter.GetBytes(imageBytes.Length);
- byte[] dataType = new byte[] { 1 }; // 1 = image
- stream.Write(lengthBytes, 0, 4);
- stream.Write(dataType, 0, 1);
- stream.Write(imageBytes, 0, imageBytes.Length);
- Debug.Log("[TCP]Sent image with size: " + texture.width + "x" + texture.height);
- }
- catch (System.Exception e)
- {
- Debug.LogError("[TCP]Send image error: " + e.Message);
- Disconnect();
- }
- }
- }
|