TServer.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using UnityEngine;
  2. using System.Collections;
  3. //引入库
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Collections.Generic;
  9. using LitJson;
  10. public class TServer : MonoBehaviour
  11. {
  12. public string ownerName;
  13. //以下默认都是私有的成员
  14. Socket serverSocket; //服务器端socket
  15. public List<TServerC> clientSockets =new List<TServerC>(); //客户端socket
  16. IPEndPoint ipEnd; //侦听端口
  17. string recvStr; //接收的字符串
  18. string sendStr; //发送的字符串
  19. byte[] recvData = new byte[1024]; //接收的数据,必须为字节
  20. byte[] sendData = new byte[1024]; //发送的数据,必须为字节
  21. int recvLen; //接收的数据长度
  22. Thread connectThread; //连接线程
  23. //初始化
  24. void InitSocket()
  25. {
  26. //定义侦听端口,侦听任何IP
  27. ipEnd = new IPEndPoint(IPAddress.Any, 50001);
  28. //定义套接字类型,在主线程中定义
  29. serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  30. //连接
  31. serverSocket.Bind(ipEnd);
  32. //开始侦听,最大10个连接
  33. serverSocket.Listen(10);
  34. //开启一个线程连接,必须的,否则主线程卡死
  35. connectThread = new Thread(new ThreadStart(SocketReceive));
  36. connectThread.Start();
  37. }
  38. //连接
  39. void SocketConnet()
  40. {
  41. //if (clientSocket != null)
  42. // clientSocket.Close();
  43. //控制台输出侦听状态
  44. Debug.Log("等待客户端连接....");
  45. //一旦接受连接,创建一个客户端
  46. Socket ss= serverSocket.Accept(); //服务器端socket
  47. //获取客户端的IP和端口
  48. IPEndPoint ipEndClient = (IPEndPoint)ss.RemoteEndPoint;
  49. TServerC tc = new TServerC();
  50. tc._socket = ss;
  51. tc.id = clientSockets.Count+1;
  52. //输出客户端的IP和端口
  53. Debug.Log("客户端 ===>" + tc.id + "连接。。。。");
  54. // SocketSend(sendStr);
  55. tc.ts = this;
  56. tc.init();
  57. clientSockets.Add(tc);
  58. SocketConnet();
  59. }
  60. public void SocketSend(string sendStr,int id)
  61. {
  62. for (int i = 0; i < clientSockets.Count; i++)
  63. {
  64. if(id!=clientSockets[i].id)
  65. {
  66. //发送
  67. clientSockets[i].SocketSend(sendStr);
  68. }
  69. }
  70. }
  71. public TServerC getRoomSocketById(int id)
  72. {
  73. for (int i = 0; i < clientSockets.Count; i++)
  74. {
  75. if (id == clientSockets[i].id)
  76. {
  77. return clientSockets[i];
  78. }
  79. }
  80. return null;
  81. }
  82. public JsonData getRoomList(int id)
  83. {
  84. JsonData datas = new JsonData();
  85. for (int i = 0; i < clientSockets.Count; i++)
  86. {
  87. if (id != clientSockets[i].id)
  88. {
  89. datas.Add(clientSockets[i].getInfo());
  90. }
  91. }
  92. return datas;
  93. }
  94. //服务器接收
  95. void SocketReceive()
  96. {
  97. SocketConnet();
  98. }
  99. //连接关闭
  100. void SocketQuit()
  101. {
  102. for (int i = 0; i < clientSockets.Count; i++)
  103. {
  104. //先关闭客户端
  105. if (clientSockets[i] != null)
  106. clientSockets[i]._socket.Close();
  107. }
  108. //再关闭线程
  109. if (connectThread != null)
  110. {
  111. connectThread.Interrupt();
  112. connectThread.Abort();
  113. }
  114. //最后关闭服务器
  115. serverSocket.Close();
  116. print("diconnect");
  117. }
  118. // Use this for initialization
  119. void Start()
  120. {
  121. InitSocket(); //在这里初始化server
  122. }
  123. // Update is called once per frame
  124. void Update()
  125. {
  126. }
  127. void OnApplicationQuit()
  128. {
  129. SocketQuit();
  130. }
  131. }