123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- 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 TServer : MonoBehaviour
- {
- public string ownerName;
- //以下默认都是私有的成员
- Socket serverSocket; //服务器端socket
- public List<TServerC> clientSockets =new List<TServerC>(); //客户端socket
- IPEndPoint ipEnd; //侦听端口
- string recvStr; //接收的字符串
- string sendStr; //发送的字符串
- byte[] recvData = new byte[1024]; //接收的数据,必须为字节
- byte[] sendData = new byte[1024]; //发送的数据,必须为字节
- int recvLen; //接收的数据长度
- Thread connectThread; //连接线程
- //初始化
- void InitSocket()
- {
- //定义侦听端口,侦听任何IP
- ipEnd = new IPEndPoint(IPAddress.Any, 50001);
- //定义套接字类型,在主线程中定义
- serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- //连接
- serverSocket.Bind(ipEnd);
- //开始侦听,最大10个连接
- serverSocket.Listen(10);
- //开启一个线程连接,必须的,否则主线程卡死
- connectThread = new Thread(new ThreadStart(SocketReceive));
- connectThread.Start();
- }
- //连接
- void SocketConnet()
- {
- //if (clientSocket != null)
- // clientSocket.Close();
- //控制台输出侦听状态
- Debug.Log("等待客户端连接....");
- //一旦接受连接,创建一个客户端
- Socket ss= serverSocket.Accept(); //服务器端socket
- //获取客户端的IP和端口
- IPEndPoint ipEndClient = (IPEndPoint)ss.RemoteEndPoint;
- TServerC tc = new TServerC();
- tc._socket = ss;
- tc.id = clientSockets.Count+1;
- //输出客户端的IP和端口
- Debug.Log("客户端 ===>" + tc.id + "连接。。。。");
- // SocketSend(sendStr);
- tc.ts = this;
- tc.init();
- clientSockets.Add(tc);
- SocketConnet();
- }
- public void SocketSend(string sendStr,int id)
- {
- for (int i = 0; i < clientSockets.Count; i++)
- {
- if(id!=clientSockets[i].id)
- {
- //发送
- clientSockets[i].SocketSend(sendStr);
- }
- }
- }
- public TServerC getRoomSocketById(int id)
- {
- for (int i = 0; i < clientSockets.Count; i++)
- {
- if (id == clientSockets[i].id)
- {
- return clientSockets[i];
- }
- }
- return null;
- }
- public JsonData getRoomList(int id)
- {
- JsonData datas = new JsonData();
- for (int i = 0; i < clientSockets.Count; i++)
- {
- if (id != clientSockets[i].id)
- {
- datas.Add(clientSockets[i].getInfo());
- }
- }
- return datas;
- }
- //服务器接收
- void SocketReceive()
- {
- SocketConnet();
- }
- //连接关闭
- void SocketQuit()
- {
- for (int i = 0; i < clientSockets.Count; i++)
- {
- //先关闭客户端
- if (clientSockets[i] != null)
- clientSockets[i]._socket.Close();
- }
- //再关闭线程
- if (connectThread != null)
- {
- connectThread.Interrupt();
- connectThread.Abort();
- }
- //最后关闭服务器
- serverSocket.Close();
- print("diconnect");
- }
- // Use this for initialization
- void Start()
- {
- InitSocket(); //在这里初始化server
- }
- // Update is called once per frame
- void Update()
- {
- }
- void OnApplicationQuit()
- {
- SocketQuit();
- }
- }
|