Exemple #1
0
        public void TestECHO()
        {
            IServerConfig config = new ServerConfig
            {
                Name = "My Custom Server",
                Ip = "Any",
                Port = 100,
                Mode = SocketMode.Async,
                MaxConnectionNumber = 1
            };

            YourServer server = new YourServer();
            server.Setup(string.Empty, config, string.Empty);

            server.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);
                    }
                }
            }

            server.Stop();
        }
Exemple #2
0
        private void 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
            };

            server.Setup(string.Empty, config, string.Empty);

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

            try
            {
                server.Start();

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

                for (int i = 0; i < maxConnectionNumber; i++)
                {
                    Socket socket = new Socket(serverAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                    socket.Connect(serverAddress);
                    Stream socketStream = new NetworkStream(socket);
                    StreamReader reader = new StreamReader(socketStream, Encoding.Default, true);
                    reader.ReadLine();
                    sockets.Add(socket);
                }

                using (Socket trySocket = new Socket(serverAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
                {
                    Console.WriteLine("Start to connect try socket");
                    trySocket.Connect(serverAddress);
                    var innerSocketStream = new NetworkStream(trySocket);
                    innerSocketStream.ReadTimeout = 500;

                    using (StreamReader tryReader = new StreamReader(innerSocketStream, Encoding.Default, true))
                    {
                        Assert.Throws<IOException>(delegate
                        {
                            string welcome = tryReader.ReadLine();
                            Console.WriteLine(welcome);
                        });
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message + " " + e.StackTrace);
            }
            finally
            {
                sockets.ForEach(s =>
                {
                    s.Shutdown(SocketShutdown.Both);
                    s.Close();
                });

                server.Stop();
            }
        }