Example #1
0
 public void Init()
 {
     Debug.Log("TcpSocketServer Init");
     ClientDic    = new Dictionary <string, StateObject>();
     CloseClients = new List <string>();
     TcpLifeCycle = NetWorkLife.None;
     ListenNum    = 5;
     //allDone = new ManualResetEvent(false);
     CreateStateObjectPool();
 }
Example #2
0
        private void AcceptCallback(IAsyncResult ar)
        {
            TcpLifeCycle = NetWorkLife.Connected;
            allDone.Set();
            SettingListernStatus = null;
            Socket      listener = (Socket)ar.AsyncState;
            Socket      handler  = listener.EndAccept(ar);
            StateObject state    = StatePool.OutQuene();

            state.workSocket = handler;
            ClientDic.Add(handler.RemoteEndPoint.ToString(), state);
            ConnectSuccess(handler.RemoteEndPoint.ToString(), state);
            handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
        }
Example #3
0
 public void StartSever(string IP, int port = 50015)
 {
     if (SocketListenThread != null)
     {
         SocketListenThread.Abort();
     }
     SocketListenThread = new Thread(() =>
     {
         IPAddress ipAddress = IPAddress.Parse(IP);
         IPEndPoint endPoint = new IPEndPoint(ipAddress, port);
         severSocket         = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         severSocket.Bind(endPoint);
         severSocket.Listen(ListenNum);
         Debug.Log("IP地址为 :" + IP + " 端口为 :" + port + " 服务已建立");
         try
         {
             while (TcpLifeCycle != NetWorkLife.Destory)
             {
                 allDone.Reset();
                 TcpLifeCycle = NetWorkLife.Connecting;
                 if (severSocket != null && SettingListernStatus == null)
                 {
                     Debug.Log("等待客服端连接:" + SocketListenThread.ManagedThreadId);
                     SettingListernStatus = severSocket.BeginAccept(new AsyncCallback(AcceptCallback), severSocket);
                 }
                 allDone.WaitOne();
                 Thread.Sleep(100);
             }
         }
         catch (ThreadAbortException ex)
         {
             Debug.Log("Socket监听线程关闭 :");
             return;
         }
         catch (Exception e)
         {
             Debug.Log(e.Message);
         }
         Debug.Log("Socket监听线程正常退出 :");
     });
     SocketListenThread.Start();
 }
Example #4
0
        private void ReadCallback(IAsyncResult ar)
        {
            StateObject state     = (StateObject)ar.AsyncState;
            Socket      handler   = state.workSocket;
            int         bytesRead = handler.EndReceive(ar);

            if (bytesRead > 0)
            {
                ReceviceData(state.buffer, bytesRead, handler.RemoteEndPoint.ToString());
                handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
            }
            else
            {
                TcpLifeCycle         = NetWorkLife.Disconnected;
                SettingListernStatus = null;
                ClientConnectClose(state);
                ClientDic.Remove(state.workSocket.RemoteEndPoint.ToString());
                StatePool.EnQuene(state);
            }
        }
Example #5
0
 public void CloseServer()
 {
     try
     {
         SettingListernStatus = null;
         TcpLifeCycle         = NetWorkLife.Destory;
         severSocket.Close();
         severSocket = null;
         foreach (KeyValuePair <string, StateObject> item in ClientDic)
         {
             item.Value.workSocket.Close();
         }
         Debug.Log("尝试关闭线程 :" + SocketListenThread.ManagedThreadId);
         SocketListenThread.Abort();
         SocketListenThread = null;
     }
     catch (Exception ex)
     {
         Debug.Log("关闭Socket连接发生异常:" + ex.Message);
     }
 }