using UnityEngine; using System.Collections; //引入库 using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Collections.Generic; using LitJson; public class TClient : MonoBehaviour { string editString = "hello wolrd"; //编辑框文字 public delegate void OnMessagedDelegate(string data); public OnMessagedDelegate OnMessaged; Socket serverSocket; //服务器端socket IPAddress ip; //主机ip IPEndPoint ipEnd; string recvStr; //接收的字符串 string sendStr; //发送的字符串 byte[] recvData = new byte[1024]; //接收的数据,必须为字节 byte[] sendData = new byte[1024]; //发送的数据,必须为字节 int recvLen; //接收的数据长度 Thread connectThread; //连接线程 public static TClient Instance; public bool isClient=false; //初始化 public void InitSocket(string text = "127.0.0.1") { if(isClient) { SocketQuit(); } isClient = true; Debug.Log(text); //定义服务器的IP和端口,端口与服务器对应 ip = IPAddress.Parse(text); //可以是局域网或互联网ip,此处是本机 ipEnd = new IPEndPoint(ip, 50001); //开启一个线程连接,必须的,否则主线程卡死 connectThread = new Thread(new ThreadStart(SocketReceive)); connectThread.Start(); } Queue queue =new Queue(); public void SocketConnet() { if (serverSocket != null) serverSocket.Close(); //定义套接字类型,必须在子线程中定义 serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //连接 serverSocket.Connect(ipEnd); //输出初次连接收到的字符串 // recvLen = serverSocket.Receive(recvData); // recvStr = Encoding.ASCII.GetString(recvData, 0, recvLen); // print(recvStr); // if(queue!=null) // { // queue.Enqueue(recvStr); // } } public static byte[] int2Bytes(int i) { byte[] b = new byte[4]; b[0] = (byte)i; //将int类型的最后8位赋值给0号位。强转是将高位去掉 b[1] = (byte)(i >> 8); b[2] = (byte)(i >> 16); b[3] = (byte)(i >> 24); return b; } public void SocketSend(string sendStr) { //清空发送缓存 sendData = new byte[1024]; //数据类型转换 sendData = Encoding.ASCII.GetBytes(sendStr); byte[] bb = int2Bytes(sendData.Length); serverSocket.Send(bb, bb.Length, SocketFlags.None); //发送 serverSocket.Send(sendData, sendData.Length, SocketFlags.None); } void SocketReceive() { SocketConnet(); //不断接收服务器发来的数据 while (true) { byte[] recvBytes = new byte[4]; int bodyLength = serverSocket.Receive(recvBytes, 0, recvBytes.Length, SocketFlags.None); int baotou = TServerC.bytes2Int(recvBytes); //对data清零 recvData = new byte[baotou]; //获取收到的数据的长度 recvLen = 0; while (recvLen != recvData.Length) { // Debug.Log("准备接收==>" + recvLen); recvLen += serverSocket.Receive(recvData, recvLen, recvData.Length - recvLen, SocketFlags.None); // Debug.Log("server 接收到Client 长度==>" + recvLen); } if (recvLen == 0) { SocketQuit(); continue; } recvStr = Encoding.ASCII.GetString(recvData, 0, recvLen); queue.Enqueue(recvStr); } } public void SocketQuit() { //关闭线程 if (connectThread != null) { connectThread.Interrupt(); connectThread.Abort(); } //最后关闭服务器 if (serverSocket != null) serverSocket.Close(); isClient = false; } // Use this for initialization void Start() { Instance = this; //InitSocket(); } void OnGUI() { // editString = GUI.TextField(new Rect(10, 10, 100, 20), editString); // if (GUI.Button(new Rect(10, 30, 60, 20), "send")) // SocketSend(editString); } // Update is called once per frame void Update() { if (queue.Count>0) { string str = queue.Dequeue(); Debug.Log("Str接收===:"+str); if (OnMessaged != null) { OnMessaged(str); } } // SocketSend("连接上了12312321321"); } //程序退出则关闭连接 void OnApplicationQuit() { SocketQuit(); } }