public void StopServer()
 {
     if (m_Server.IsRunning)
     {
         m_Server.Stop();
     }
 }
Пример #2
0
        public void TestCommandParser()
        {
            var server = new TestServer(new TestCommandParser());

            server.Setup(m_RootConfig, m_Config, SocketServerFactory.Instance);

            try
            {
                server.Start();

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

                using (Socket socket = CreateClientSocket())
                {
                    string command = string.Format("Hello World ({0})!", Guid.NewGuid().ToString());
                    socket.SendTo(m_Encoding.GetBytes("ECHO:" + command + Environment.NewLine), serverAddress);
                    string echoMessage = m_Encoding.GetString(ReceiveMessage(socket, serverAddress).ToArray());
                    Assert.AreEqual(command, echoMessage);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (server.IsRunning)
                {
                    server.Stop();
                }
            }
        }
Пример #3
0
 private void StopServer()
 {
     if (m_Server.IsRunning)
     {
         m_Server.Stop();
         Console.WriteLine("The UDP Socket server is stopped!");
     }
 }
Пример #4
0
        //[Test, Repeat(3)]
        //public void TestCommandParameterParser()
        //{
        //    var server = new TestServer(new TestCommandParser(), new TestCommandParameterParser());
        //    server.Setup(m_Config);

        //    try
        //    {
        //        server.Start();

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

        //        using (Socket socket = CreateClientSocket())
        //        {
        //            string[] arrParam = new string[] { "A1", "A2", "A4", "B2", "A6", "E5" };
        //            socket.SendTo(m_Encoding.GetBytes("PARA:" + string.Join(",", arrParam) + Environment.NewLine), serverAddress);

        //            List<string> received = new List<string>();

        //            foreach (var p in arrParam)
        //            {
        //                string r = m_Encoding.GetString(ReceiveMessage(socket, serverAddress).ToArray());
        //                Console.WriteLine("C: " + r);
        //                received.Add(r);
        //            }

        //            Assert.AreEqual(arrParam, received);
        //        }
        //    }
        //    catch (Exception e)
        //    {
        //        throw e;
        //    }
        //    finally
        //    {
        //        if (server.IsRunning)
        //            server.Stop();
        //    }
        //}

        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
            };

            server.Setup(m_RootConfig, config, SocketServerFactory.Instance);

            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 = CreateClientSocket();
                    socket.SendTo(m_Encoding.GetBytes(Guid.NewGuid().ToString() + Environment.NewLine), serverAddress);
                    Console.WriteLine("C: " + m_Encoding.GetString(ReceiveMessage(socket, serverAddress).ToArray()));
                    sockets.Add(socket);
                }

                using (Socket trySocket = CreateClientSocket())
                {
                    trySocket.SendTo(m_Encoding.GetBytes(Guid.NewGuid().ToString() + Environment.NewLine), serverAddress);
                    Thread thread = new Thread(new ThreadStart(() =>
                    {
                        Console.WriteLine("C: " + m_Encoding.GetString(ReceiveMessage(trySocket, serverAddress).ToArray()));
                    }));
                    thread.Start();
                    if (thread.Join(500))
                    {
                        //Assert.Fail("Current connection number: {0}, max connectionnumber: {1}", maxConnectionNumber + 1, maxConnectionNumber);
                        return(false);
                    }
                    else
                    {
                        thread.Abort();
                        return(true);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message + " " + e.StackTrace);
                return(false);
            }
            finally
            {
                server.Stop();
            }
        }
Пример #5
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
            };

            server.Setup(m_RootConfig, config, SocketServerFactory.Instance);

            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 = 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
            {
                server.Stop();
            }
        }
Пример #6
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();
            }
        }