Exemple #1
0
        /// <summary>
        /// 刷新用户列表
        /// </summary>
        /// <param name="commands"></param>
        private void list(string[] commands)
        {
            string msg = "LIST|" + GlobalVariable.getClientStr();

            sendMsg(this.currentSocket, msg);
            DelegateCollectionImpl.returnNameList(GlobalVariable.getClisentList());
        }
Exemple #2
0
 /// <summary>
 /// 连接
 /// 此时接收到的命令格式为:命令标志符(CONN)|发送者的用户名|,tokens[1]中保存了发送者的用户名
 /// </summary>
 /// <param name="commands"></param>
 /// <param name="status"></param>
 private void conn(string[] commands, ref bool status)
 {
     this.name = commands[1];
     if (GlobalVariable.tcpClients.ContainsKey(this.name))
     {
         string msg = "ERR|用户已存在";
         sendMsg(this.currentSocket, msg);
     }
     else
     {
         byte[] locker = new byte[0];
         //多线程还是用hashtable好一些
         lock (locker)
         {
             GlobalVariable.tcpClients.Add(name, currentSocket);
             string msg = "LIST|" + GlobalVariable.getClientStr();
             //遍历 向所有用户刷新用户列表
             foreach (var item in GlobalVariable.tcpClients)
             {
                 sendMsg(item.Value, msg);
             }
             DelegateCollectionImpl.returnNameList(GlobalVariable.tcpClients.Keys.ToList());
             this.name = commands[1];
         }
         status = true;
     }
 }
Exemple #3
0
 /// <summary>
 /// 向所有用户发消息
 /// 此时接收到的命令格式为:命令标志符(PUB)|发送者的用户名|msg
 /// </summary>
 /// <param name="commands"></param>
 private void pub(string[] commands)
 {
     foreach (var item in GlobalVariable.tcpClients)
     {
         sendMsg(item.Value, "CONT|" + item.Key + "|" + commands[2]);
     }
     DelegateCollectionImpl.returnStringMsg(this.name + ": " + commands[2]);
 }
Exemple #4
0
 /// <summary>
 /// 退出
 /// 命令标志符(EXIT)|发送者的用户名
 /// </summary>
 /// <param name="commands"></param>
 private void exit(string[] commands)
 {
     byte[] locker = new byte[0];
     lock (locker)
     {
         GlobalVariable.tcpClients.Remove(commands[1]);
         string msg = "LIST|" + GlobalVariable.getClientStr();
         //遍历 向所有用户刷新用户列表
         foreach (var item in GlobalVariable.tcpClients)
         {
             sendMsg(item.Value, msg);
         }
         DelegateCollectionImpl.returnNameList(GlobalVariable.tcpClients.Keys.ToList());
     }
     this.currentSocket.Close();
     Thread.CurrentThread.Abort();
 }
Exemple #5
0
        public override void getOut(string clientName)
        {
            Socket clientSocket;

            if (GlobalVariable.tcpClients.TryGetValue(clientName, out clientSocket))
            {
                serverClient.sendErrMsg(clientSocket, "您已被强制下线");
                base.sendMessage(TOSERVERCOMMAND.EXIT);
                string[] commands = msg.ToString().Split(new char[] { '|' });
                bool     status   = true;
                serverClient.decodeCommands(commands, ref status);
            }
            else
            {
                DelegateCollectionImpl.returnStringMsg("用户已下线");
            }
        }
Exemple #6
0
        /// <summary>
        /// 开始监听
        /// </summary>
        /// <param name="listener"></param>
        public override void startSocketListen(object listener)
        {
            listenFlag = true;
            //测试delegate  开始监听
            DelegateCollectionImpl.returnStringMsg("开始监听");
            TcpListener TcpListener = (TcpListener)listener;

            while (listenFlag)
            {
                try
                {
                    if (TcpListener.Pending())
                    {
                        Socket socket = TcpListener.AcceptSocket();
                        if (GlobalVariable.tcpClients.Count > MAX_NUM)
                        {
                            //DONE:通过委托对form中richTextbox添加注释  “超过最大连接数”
                            DelegateCollectionImpl.returnStringMsg("超过最大连接数,连接失败");
                            //DONE: 给socket发信息, “超过最大连接数” 并关闭socket
                            serverClient = new ServerClient(socket);
                            serverClient.sendErrMsg(socket, "超过最大连接数");
                            socket.Close();
                        }
                        else
                        {
                            //DONE: 开启一个新线程
                            serverClient = new ServerClient(socket);
                            Thread serverThread = new Thread(new ThreadStart(serverClient.runServer));
                            serverThread.Start();
                        }
                    }
                }
                catch (Exception err)
                {
                    listenFlag = false;
                    //Done: 通过委托对richTextbox添加注释 “err”,并修改监听按钮
                    DelegateCollectionImpl.returnStringErrMsg("出现异常" + err.Message);
                }
                Thread.Sleep(200);
            }
        }