using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Net.Sockets; using System.Threading; using System.Net; using System; public class Tcp_Server : MonoBehaviour { private static int tokenIndex = 0; public TextMesh textone; //配置相关; private string _ip; [SerializeField] private int _port = 50001; private Socket _server; private Thread _acceptClientConnectThread; private Dictionary _clientDictionary =new Dictionary(); private bool _isActive = true; // Use this for initialization void Awake () { //_ip = Network.player.ipAddress; //for (int i = 0; i < Dns.GetHostEntry(Dns.GetHostName()).AddressList.Length; i++) //{ // if (Dns.GetHostEntry(Dns.GetHostName()).AddressList[i].AddressFamily.ToString() == "InterNetwork") // { // _ip = Dns.GetHostEntry(Dns.GetHostName()).AddressList[i].ToString(); // } //} _ip = Tcp_Util.GetIP(); Debug.Log("ip " + _ip + ":" + _port); } // Update is called once per frame void Update () { } void OnApplicationQuit() { Close (); } /// /// 启动服务器=建立流式套接字+配置本地地址; /// public void StartUp() { try { _isActive = true; _server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); Debug.Log("ip " + _ip + ":" + _port); textone.text = _ip; EndPoint endPoint = new IPEndPoint(IPAddress.Parse(_ip), _port); _server.Bind(endPoint); _server.Listen(30); Debug.Log("Socket服务器监听启动......"); _acceptClientConnectThread = new Thread(AcceptClientConnect); _acceptClientConnectThread.Start(); } catch (Exception e) { Debug.Log (e.Message); Close (); } } /// /// Accept Client Connect接受客户端连接; /// /// public static bool isClient; public void AcceptClientConnect() { Tcp_Connect tc; string token; while (_isActive) { try { //接受客户端连接,接一次执行一次, 返回值为客户端的套接字; Socket clientSocket = _server.Accept (); Debug.Log("客户端已连接!local:" + clientSocket.LocalEndPoint + "<---Client:" + clientSocket.RemoteEndPoint); isClient=true; //维护一个客户端列表; tokenIndex++; token = "client_" + tokenIndex; tc = new Tcp_Connect (clientSocket, RemoveClient, token); tc.StartUp (); _clientDictionary.Add (token, tc); } catch (Exception e) { Debug.Log (e.Message); } _isActive = false; } _acceptClientConnectThread.Abort(); } /// /// 给某个客户端发消息 /// /// Message. /// Client. public void Send(byte[] msg , string key) { if (_clientDictionary.ContainsKey (key)) { _clientDictionary [key].Send (msg); } } /// /// 给所有人发消息 /// /// String. public void SendAll(byte[] msg) { var enumerator = _clientDictionary.GetEnumerator(); while (enumerator.MoveNext ()) { Send (msg, enumerator.Current.Key); } } public void RemoveClient(string token) { isClient = false; //Debug.Log ("duankai:==>"+token); if (_clientDictionary.ContainsKey(token)) { _clientDictionary ["token"].Close (); _clientDictionary.Remove (token); } } //最后关闭套接字; public void Close() { _isActive = false; //关闭客户端套接字相关; var enumerator = _clientDictionary.GetEnumerator(); while (enumerator.MoveNext ()) { enumerator.Current.Value.Close (); } //列表清除; _clientDictionary.Clear(); //关闭服务器套接字相关; if (_server != null) { _server.Close(); } } }