Example #1
0
        static void Main()
        {
            while (true)
            {
                try
                {
                    Console.WriteLine("Begin to Start TcpSvr SVN...");

                    TestTcpSvr tts = new TestTcpSvr();
                    tts.InitSys();
                    #region 定时器事件
                    System.Timers.Timer aTimer = new System.Timers.Timer();
                    aTimer.Elapsed += new ElapsedEventHandler(TimeEvent);
                    aTimer.Interval = 5 * 1000;    //配置文件中配置的秒数
                    aTimer.Enabled  = true;
                    #endregion
                    string ipaddr = getLocalIP();
                    Console.WriteLine(ipaddr);
                    ushort portNumber = 5632;
                    IniAc  ini        = new IniAc();
                    ipaddr = ini.ReadValue("NET", "ip");
                    Console.WriteLine(ipaddr);
                    string portStr = ini.ReadValue("NET", "port");
                    portNumber = System.Convert.ToUInt16(portStr);

                    //TcpSvr svr = new TcpSvr(9050,4);//默认使用Encoding.Default编码方式
                    TcpSvr svr = new TcpSvr(IPAddress.Parse(ipaddr), 5632, 80, new Coder(Coder.EncodingMothord.Unicode), "D:\\MyVidio");

                    svr.Resovlver = new DatagramResolver("]");//0x5d

                    //定义服务器的4个事件

                    //服务器满
                    svr.ServerFull += new NetEvent(tts.ServerFull);

                    //新客户端连接
                    svr.ClientConn += new NetEvent(tts.ClientConn);

                    //客户端关闭
                    svr.ClientClose += new NetEvent(tts.ClientClose);

                    //接收到数据
                    svr.RecvData += new NetEvent(tts.RecvData);

                    //  svr.LogIn += new NetEvent(this.LogIn);

                    svr.Start();

                    Console.WriteLine("Server is listen...{0}",
                                      svr.ServerSocket.LocalEndPoint.ToString());
                    //命令控制循环
                    #region

                    while (true)
                    {
                        Console.Write(">");

                        string cmd = Console.ReadLine();

                        //退出测试程序
                        if (cmd.ToLower() == "exit")
                        {
                            break;
                        }

                        //停止服务器程序
                        if (cmd.ToLower() == "stop")
                        {
                            svr.Stop();

                            Console.WriteLine("Server is Stop.");

                            continue;
                        }

                        //运行服务器程序
                        if (cmd.ToLower() == "start")
                        {
                            svr.Start();

                            Console.WriteLine("Server is listen...{0}",
                                              svr.ServerSocket.LocalEndPoint.ToString());

                            continue;
                        }

                        //察看服务器在线客户端数目和容量
                        if (cmd.ToLower() == "count")
                        {
                            Console.WriteLine("Current count of Client is {0}/{1}",
                                              svr.SessionCount, svr.Capacity);
                            continue;
                        }

                        //发送数据到客户端格式:send [Session] [stringData]
                        if (cmd.ToLower().IndexOf("send") != -1)
                        {
                            cmd = cmd.ToLower();

                            string[] para = cmd.Split(' ');

                            if (para.Length == 3)
                            {
                                Session client = (Session)svr.SessionTable[new SessionId(int.Parse
                                                                                             (para[1]))];

                                if (client != null)
                                {
                                    svr.SendText(client, para[2]);
                                }
                                else
                                {
                                    Console.WriteLine("The Session is Null");
                                }
                            }
                            else
                            {
                                Console.WriteLine("Error Command");
                            }

                            continue;
                        }

                        //从服务器上踢掉一个客户端
                        if (cmd.ToLower().IndexOf("kick") != -1)
                        {
                            cmd = cmd.ToLower();

                            string[] para = cmd.Split(' ');

                            if (para.Length == 2)
                            {
                                Session client = (Session)svr.SessionTable[new SessionId(int.Parse
                                                                                             (para[1]))];

                                if (client != null)
                                {
                                    svr.CloseSession(client);
                                }
                                else
                                {
                                    Console.WriteLine("The Session is Null");
                                }
                            }
                            else
                            {
                                Console.WriteLine("Error command");
                            }

                            continue;
                        }

                        //列出服务器上所有的客户端信息
                        if (cmd.ToLower() == "list")
                        {
                            int i = 0;

                            foreach (Session Client in svr.SessionTable.Values)
                            {
                                if (Client != null)
                                {
                                    i++;
                                    string info = string.Format("{0} Client:{1} connected server Session:{2}. Socket Handle:{3}",
                                                                i,
                                                                Client.ClientSocket.RemoteEndPoint.ToString(),
                                                                Client.ID,
                                                                Client.ClientSocket.Handle);

                                    Console.WriteLine(info);
                                }
                                else
                                {
                                    i++;

                                    string info = string.Format("{0} null Client", i);
                                    Console.WriteLine(info);
                                }
                            }

                            continue;
                        }

                        Console.WriteLine("Unkown Command");
                    }//end of while
                    #endregion
                    Console.WriteLine("End service");
                }
                catch (Exception ex)
                {
                    string    sql      = "UPDATE device SET Device_Status = 0,Sockets = ''";
                    IDataBase database = MySqlDataBase.getInstance();
                    database.Open();
                    database.ExcuteNonQuery(sql);
                    database.Close();

                    Console.WriteLine(ex.ToString());
                }
            }
        }
Example #2
0
        public static void Main()
        {
            try
            {
                Console.WriteLine("TcpSvr is beginning to start...");

                myTcpServer tts = new myTcpServer();
                TcpSvr      svr = new TcpSvr(8848, 1024, new Coder(Coder.EncodingMothord.Default));

                svr.Resovlver = new DatagramResolver("]}");
                //处理客户端连接数已满事件
                svr.ServerFull += new NetEvent(tts.ServerFull);
                //处理新客户端连接事件
                svr.ClientConn += new NetEvent(tts.ClientConn);
                //处理客户端关闭事件
                svr.ClientClose += new NetEvent(tts.ClientClose);
                //处理接收到数据事件
                svr.RecvData += new NetEvent(tts.RecvData);

                while (true)
                {
                    Console.Write(">");
                    string cmd = Console.ReadLine();
                    //退出服务
                    if (cmd.ToLower() == "exit")
                    {
                        break;
                    }
                    //暂停服务
                    if (cmd.ToLower() == "stop")
                    {
                        svr.Stop();
                        Console.WriteLine("Server is Stop.");
                        continue;
                    }
                    //开启服务
                    if (cmd.ToLower() == "start")
                    {
                        svr.Start();
                        Console.WriteLine("Server is listen...{0}", svr.ServerSocket.LocalEndPoint.ToString());
                        continue;
                    }
                    //连接数
                    if (cmd.ToLower() == "count")
                    {
                        Console.WriteLine("Current count of Client is {0}/{1}", svr.SessionCount, svr.Capacity);
                        continue;
                    }
                    //发送数据到客户端格式:send [Session] [stringData]
                    if (cmd.ToLower().IndexOf("send") != -1)
                    {
                        cmd = cmd.ToLower();
                        string[] para = cmd.Split(' ');
                        if (para.Length == 3)
                        {
                            Session client = (Session)svr.SessionTable[new SessionId(int.Parse(para[1]))];

                            if (client != null)
                            {
                                svr.Send(client, para[2]);
                            }
                            else
                            {
                                Console.WriteLine("The Session is Null");
                            }
                        }
                        else
                        {
                            Console.WriteLine("Error Command");
                        }

                        continue;
                    }

                    //从服务器上踢掉一个客户端 格式: kick [Session]
                    if (cmd.ToLower().IndexOf("kick") != -1)
                    {
                        cmd = cmd.ToLower();

                        string[] para = cmd.Split(' ');

                        if (para.Length == 2)
                        {
                            Session client = (Session)svr.SessionTable[new SessionId(int.Parse(para[1]))];

                            if (client != null)
                            {
                                svr.CloseSession(client);
                            }
                            else
                            {
                                Console.WriteLine("The Session is Null");
                            }
                        }
                        else
                        {
                            Console.WriteLine("Error command");
                        }

                        continue;
                    }

                    //列出服务器上所有的客户端信息
                    if (cmd.ToLower() == "list")
                    {
                        int i = 0;

                        foreach (Session Client in svr.SessionTable.Values)
                        {
                            if (Client != null)
                            {
                                i++;
                                string info = string.Format("{0} Client:{1} connected server Session:{2}. Socket Handle:{3}",
                                                            i,
                                                            Client.ClientSocket.RemoteEndPoint.ToString(),
                                                            Client.ID,
                                                            Client.ClientSocket.Handle);

                                Console.WriteLine(info);
                            }
                            else
                            {
                                i++;
                                string info = string.Format("{0} null Client", i);
                                Console.WriteLine(info);
                            }
                        }
                        continue;
                    }
                    if (cmd.ToLower() == "reset")
                    {
                        continue;
                    }
                    Console.WriteLine("Unkown Command");
                }//end of while

                Console.WriteLine("End service");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }