123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- 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<string, Tcp_Connect> _clientDictionary =new Dictionary<string, Tcp_Connect>();
- 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 ();
- }
- /// <summary>
- /// 启动服务器=建立流式套接字+配置本地地址;
- /// </summary>
- 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 ();
- }
- }
- /// <summary>
- /// Accept Client Connect接受客户端连接;
- /// </summary>
- ///
- 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();
- }
-
- /// <summary>
- /// 给某个客户端发消息
- /// </summary>
- /// <param name="msg">Message.</param>
- /// <param name="client">Client.</param>
- public void Send(byte[] msg , string key)
- {
- if (_clientDictionary.ContainsKey (key)) {
- _clientDictionary [key].Send (msg);
- }
- }
- /// <summary>
- /// 给所有人发消息
- /// </summary>
- /// <param name="str">String.</param>
- 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();
- }
- }
- }
|