123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- 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;
- IPAddress 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 = IPAddress.Parse(text);
- ipEnd = new IPEndPoint(ip, 50001);
-
- connectThread = new Thread(new ThreadStart(SocketReceive));
- connectThread.Start();
- }
- Queue<string> queue =new Queue<string>();
- public void SocketConnet()
- {
- if (serverSocket != null)
- serverSocket.Close();
-
- serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
-
- serverSocket.Connect(ipEnd);
-
-
-
-
-
-
-
-
- }
- public static byte[] int2Bytes(int i)
- {
- byte[] b = new byte[4];
- b[0] = (byte)i;
- 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);
-
- recvData = new byte[baotou];
-
- recvLen = 0;
- while (recvLen != recvData.Length)
- {
-
- recvLen += serverSocket.Receive(recvData, recvLen, recvData.Length - recvLen, SocketFlags.None);
-
- }
- 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;
- }
-
- void Start()
- {
- Instance = this;
-
- }
- void OnGUI()
- {
-
-
-
- }
-
- void Update()
- {
- if (queue.Count>0)
- {
-
- string str = queue.Dequeue();
- Debug.Log("Str接收===:"+str);
- if (OnMessaged != null)
- {
- OnMessaged(str);
- }
- }
-
- }
-
- void OnApplicationQuit()
- {
- SocketQuit();
- }
- }
|