Exemple #1
0
        public void TestECHO()
        {
            var bootstrap = new DefaultBootstrap();

            IServerConfig config = new ServerConfig
            {
                Name = "My Custom Server",
                Ip = "Any",
                Port = 100,
                Mode = SocketMode.Tcp,
                MaxConnectionNumber = 1
            };

            var rootConfig = new RootConfig();

            YourServer server = new YourServer();

            bootstrap.Initialize(rootConfig, new IAppServer[] { server }, new IServerConfig[] { config }, new ConsoleLogFactory());

            bootstrap.Start();

            EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), config.Port);

            using (Socket socket = new Socket(serverAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
            {
                socket.Connect(serverAddress);
                Stream socketStream = new NetworkStream(socket);
                using (StreamReader reader = new StreamReader(socketStream, Encoding.Default, true))
                using (StreamWriter writer = new StreamWriter(socketStream, Encoding.Default, 1024 * 8))
                {
                    //ignore welcome message
                    reader.ReadLine();

                    string command = "CMD:ECHO ";
                    string[] parameters = new string[] { "Kerry", "Jiang", "China", "Shanghai" };
                    string parameter = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Join(" ", parameters)));
                    writer.WriteLine(command + parameter);
                    writer.Flush();

                    foreach (var p in parameters)
                    {
                        string param = reader.ReadLine();
                        Console.WriteLine(param);
                        Assert.AreEqual(p, param);
                    }
                }
            }

            bootstrap.Stop();
        }
Exemple #2
0
        static void RunAsConsole()
        {
            IBootstrap bootstrap = new DefaultBootstrap();

            SocketServiceConfig serverConfig = ConfigurationManager.GetSection("socketServer") as SocketServiceConfig;
            if (!bootstrap.Initialize(serverConfig))
            {
                Console.WriteLine("Failed to initialize SuperSocket server! Please check error log for more information!");
                return;
            }

            var result = bootstrap.Start();

            switch(result)
            {
                case(StartResult.None):
                    Console.WriteLine("No server is configured, please check you configuration!");
                    break;

                case(StartResult.Success):
                    Console.WriteLine("The server has been started!");
                    break;

                case (StartResult.Failed):
                    Console.WriteLine("Failed to start SuperSocket server! Please check error log for more information!");
                    break;

                case (StartResult.PartialSuccess):
                    Console.WriteLine("Some server instances were started successfully, but the others failed to start! Please check error log for more information!");
                    break;
            }

            Console.WriteLine("Press key 'q' to stop the server.");

            while (Console.ReadKey().Key != ConsoleKey.Q)
            {
                Console.WriteLine();
                continue;
            }

            bootstrap.Stop();

            Console.WriteLine();
            Console.WriteLine("The server has been stopped!");
        }
Exemple #3
0
        private bool TestMaxConnectionNumber(int maxConnectionNumber)
        {
            var server = new TestServer();
            var defaultConfig = DefaultServerConfig;

            var config = new ServerConfig
            {
                Ip = defaultConfig.Ip,
                LogCommand = defaultConfig.LogCommand,
                MaxConnectionNumber = maxConnectionNumber,
                Mode = defaultConfig.Mode,
                Name = defaultConfig.Name,
                Port = defaultConfig.Port,
                Security = defaultConfig.Security,
                Certificate = defaultConfig.Certificate
            };

            var bootstrap = new DefaultBootstrap();

            bootstrap.Initialize(m_RootConfig, new IAppServer[] { server }, new IServerConfig[] { config });

            List<Socket> sockets = new List<Socket>();

            try
            {
                bootstrap.Start();

                EndPoint serverAddress = new IPEndPoint(IPAddress.Parse("127.0.0.1"), m_Config.Port);

                for (int i = 0; i < maxConnectionNumber; i++)
                {
                    Socket socket = CreateClientSocket();
                    socket.Connect(serverAddress);
                    Stream socketStream = GetSocketStream(socket);
                    StreamReader reader = new StreamReader(socketStream, m_Encoding, true);
                    reader.ReadLine();
                    sockets.Add(socket);
                }

                try
                {
                    using (Socket trySocket = CreateClientSocket())
                    {
                        trySocket.Connect(serverAddress);
                        var innerSocketStream = new NetworkStream(trySocket);
                        innerSocketStream.ReadTimeout = 500;

                        using (StreamReader tryReader = new StreamReader(innerSocketStream, m_Encoding, true))
                        {
                            string welcome = tryReader.ReadLine();
                            Console.WriteLine(welcome);
                            return true;
                        }
                    }
                }
                catch (Exception)
                {
                    return true;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message + " " + e.StackTrace);
                return false;
            }
            finally
            {
                bootstrap.Stop();
            }
        }