Ejemplo n.º 1
0
        public void EchoサーバにlineSendで1行送信してlineRecvで1行受信する()
        {
            //setUp
            const string addr = "127.0.0.1";
            const int port = 9993;

            var sv = new EchoServer(addr, port);
            sv.Start();
            var sut = new SockTcp(new Kernel(), new Ip(addr), port, 100, null);
            sut.LineSend(Encoding.UTF8.GetBytes("本日は晴天なり"));
            Thread.Sleep(10);

            var expected = "本日は晴天なり\r\n";

            //exercise
            var bytes = sut.LineRecv(1, this);
            var actual = Encoding.UTF8.GetString(bytes);

            //verify
            Assert.That(actual, Is.EqualTo(expected));

            //tearDown
            sut.Close();
            sv.Stop();
        }
Ejemplo n.º 2
0
        public void Echoサーバに送信して溜まったデータサイズ_lengthを確認する()
        {
            //setUp
            const string addr = "127.0.0.1";
            const int    port = 9982;
            var          sv   = new EchoServer(addr, port);

            sv.Start();

            var       sut = new SockTcp(new Kernel(), new Ip(addr), port, 100, null);
            const int max = 1000;

            for (int i = 0; i < 3; i++)
            {
                sut.Send(new byte[max]);
            }

            Thread.Sleep(200);

            int expected = max * 3;

            //exercise
            var actual = sut.Length();

            //verify
            Assert.That(actual, Is.EqualTo(expected));

            //tearDown
            sut.Close();
            sv.Stop();
        }
Ejemplo n.º 3
0
        public void Echoサーバにsendしてlength分ずつRecvする()
        {
            //setUp
            const string addr = "127.0.0.1";
            const int port = 53;
            var echoServer = new EchoServer(addr, port);
            echoServer.Start();

            const int timeout = 3;

            const int max = 1500;
            const int loop = 10;
            var tmp = new byte[max];
            for (int i = 0; i < max; i++){
                tmp[i] = (byte) i;
            }

            var ip = new Ip(addr);
            for (var i = 0; i < loop; i++){
                var sockUdp = new SockUdp(new Kernel(), ip, port, null, tmp);
            //                while (sockUdp.Length() == 0){
            //                    Thread.Sleep(10);
            //                }
                var b = sockUdp.Recv(timeout);

                //verify
                for (var m = 0; m < max; m += 10){
                    Assert.That(b[m], Is.EqualTo(tmp[m])); //送信したデータと受信したデータが同一かどうかのテスト
                }
                sockUdp.Close();
            }

            //TearDown
            echoServer.Stop();
        }
Ejemplo n.º 4
0
        public void EchoサーバにlineSendで1行送信してlineRecvで1行受信する()
        {
            //setUp
            const string addr = "127.0.0.1";
            const int    port = 9993;

            var sv = new EchoServer(addr, port);

            sv.Start();
            var sut = new SockTcp(new Kernel(), new Ip(addr), port, 100, null);

            sut.LineSend(Encoding.UTF8.GetBytes("本日は晴天なり"));
            Thread.Sleep(10);

            var expected = "本日は晴天なり\r\n";

            //exercise
            var bytes  = sut.LineRecv(1, this);
            var actual = Encoding.UTF8.GetString(bytes);

            //verify
            Assert.That(actual, Is.EqualTo(expected));

            //tearDown
            sut.Close();
            sv.Stop();
        }
Ejemplo n.º 5
0
        public void EchoサーバにstringSendで1行送信してstringRecvで1行受信する()
        {
            //setUp
            const string addr = "127.0.0.1";
            const int    port = 9993;

            var sv = new EchoServer(addr, port);

            sv.Start();
            var sut = new SockTcp(new Kernel(), new Ip(addr), port, 100, null);

            sut.StringSend("本日は晴天なり", "UTF-8");
            Thread.Sleep(10);

            var expected = "本日は晴天なり\r\n";

            //exercise
            var actual = sut.StringRecv("UTF-8", 1, this);

            //verify
            Assert.That(actual, Is.EqualTo(expected));

            //tearDown
            sut.Close();
            sv.Stop();
        }
Ejemplo n.º 6
0
        public void OneServerを継承したEchoServer_TCP版_を使用して接続する()
        {
            const string addr = "127.0.0.1";
            const int port = 9999;
            const int timeout = 300;
            Ip ip = null;
            try{
                ip = new Ip(addr);
            } catch (ValidObjException ex){
                Assert.Fail(ex.Message);
            }
            var oneBind = new OneBind(ip, ProtocolKind.Tcp);
            var conf = TestUtil.CreateConf("OptionSample");
            conf.Set("port", port);
            conf.Set("multiple", 10);
            conf.Set("acl", new Dat(new CtrlType[0]));
            conf.Set("enableAcl", 1);
            conf.Set("timeOut", timeout);

            var echoServer = new EchoServer(conf, oneBind);
            echoServer.Start();

            //TCPクライアント

            const int max = 10000;
            var buf = new byte[max];
            buf[8] = 100; //CheckData
            for (int i = 0; i < 3; i++){
                var sockTcp = new SockTcp(new Kernel(), ip, port, timeout, null);

                sockTcp.Send(buf);

                while (sockTcp.Length() == 0){
                    Thread.Sleep(2);
                }

                var len = sockTcp.Length();
                if (0 < len){
                    var b = sockTcp.Recv(len, timeout, this);
                    Assert.That(b[8], Is.EqualTo(buf[8]));//CheckData
                }
                Assert.That(max, Is.EqualTo(len));

                sockTcp.Close();

            }

            echoServer.Dispose();
        }
Ejemplo n.º 7
0
        public void Echoサーバにsendで送信てtcpQueueのlength分ずつRecvする()
        {
            const string addr = "127.0.0.1";
            const int    port = 9981;

            var echoServer = new EchoServer(addr, port);

            echoServer.Start();

            const int timeout = 100;
            Ip        ip      = null;

            try{
                ip = new Ip(addr);
            } catch (ValidObjException ex) {
                Assert.Fail(ex.Message);
            }
            var sockTcp = new SockTcp(new Kernel(), ip, port, timeout, null);

            const int max  = 1000;
            const int loop = 3;
            var       tmp  = new byte[max];

            for (var i = 0; i < max; i++)
            {
                tmp[i] = (byte)i;
            }

            int recvCount = 0;

            for (var i = 0; i < loop; i++)
            {
                var len = sockTcp.Send(tmp);
                Assert.That(len, Is.EqualTo(tmp.Length));

                Thread.Sleep(10);

                var b = sockTcp.Recv(len, timeout, this);
                recvCount += b.Length;
                for (int m = 0; m < max; m += 10)
                {
                    Assert.That(b[m], Is.EqualTo(tmp[m])); //送信したデータと受信したデータが同一かどうかのテスト
                }
            }
            Assert.That(loop * max, Is.EqualTo(recvCount)); //送信したデータ数と受信したデータ数が一致するかどうかのテスト

            sockTcp.Close();
            echoServer.Stop();
        }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            Trace.Listeners.Add(new ConsoleTraceListener());

            // 加载服务器
            using (EchoServer server = new EchoServer())
            {
                server.Port = 8088;
                server.Start();

                Console.WriteLine("Press enter key to exit...");
                Console.ReadLine();
                server.Stop();
            }
        }
Ejemplo n.º 9
0
        static void Main(string[] args)
        {
            Logger.AddLogger(new ConsoleLogger());
            Logger.AddLogger(new FileLogger($"{DateTime.Now:yyyyMMdd}_EchoServerLog.txt"));
            Logger.EnableAutoFlush();

            if (Arguments.TryParse(args, out var arguments) == false)
            {
                Logger.LogError("Invalid arguments.\nUsage: Neti.EchoServer {Port}");
                Environment.Exit(1);
                return;
            }

            using (var echoServer = new EchoServer())
            {
                echoServer.Started        += () => Logger.LogInfo("# Server Started.");
                echoServer.Stopped        += () => Logger.LogInfo("# Server Stopped.");
                echoServer.SessionEntered += session =>
                {
                    var clientId = $"{session.RemoteAddress}:{session.RemotePort}";
                    Logger.LogInfo($"# Client Entered. ({clientId})");

                    session.Disconnected += () => Logger.LogInfo($"# Client Disconnected. ({clientId})");
                };

                Logger.LogInfo($"# Starting server... (Port: {arguments.Port})");
                echoServer.Start(arguments.Port);

                if (echoServer.IsActive == false)
                {
                    Logger.LogError("# Failed to start server.");
                    Environment.Exit(2);
                    return;
                }

                while (echoServer.IsActive)
                {
                    Thread.Sleep(17);
                }
            }

            Environment.ExitCode = 0;
        }
Ejemplo n.º 10
0
        public void Echoサーバにsendで送信てtcpQueueのlength分ずつRecvする()
        {
            const string addr = "127.0.0.1";
            const int port = 9981;

            var echoServer = new EchoServer(addr, port);
            echoServer.Start();

            const int timeout = 100;
            Ip ip = null;
            try{
                ip = new Ip(addr);
            } catch (ValidObjException ex){
                Assert.Fail(ex.Message);
            }
            var sockTcp = new SockTcp(new Kernel(), ip, port, timeout, null);

            const int max = 1000;
            const int loop = 3;
            var tmp = new byte[max];
            for (var i = 0; i < max; i++){
                tmp[i] = (byte) i;
            }

            int recvCount = 0;
            for (var i = 0; i < loop; i++){
                var len = sockTcp.Send(tmp);
                Assert.That(len, Is.EqualTo(tmp.Length));

                Thread.Sleep(10);

                var b = sockTcp.Recv(len, timeout, this);
                recvCount += b.Length;
                for (int m = 0; m < max; m += 10){
                    Assert.That(b[m], Is.EqualTo(tmp[m])); //送信したデータと受信したデータが同一かどうかのテスト
                }
            }
            Assert.That(loop*max, Is.EqualTo(recvCount)); //送信したデータ数と受信したデータ数が一致するかどうかのテスト

            sockTcp.Close();
            echoServer.Stop();
        }
Ejemplo n.º 11
0
        public void EchoサーバにstringSendで1行送信してstringRecvで1行受信する()
        {
            //setUp
            const string addr = "127.0.0.1";
            const int port = 9993;

            var sv = new EchoServer(addr, port);
            sv.Start();
            var sut = new SockTcp(new Kernel(),new Ip(addr), port, 100, null);
            sut.StringSend("本日は晴天なり", "UTF-8");
            Thread.Sleep(10);

            var expected = "本日は晴天なり\r\n";

            //exercise
            var actual = sut.StringRecv("UTF-8", 1, this);

            //verify
            Assert.That(actual, Is.EqualTo(expected));

            //tearDown
            sut.Close();
            sv.Stop();
        }
Ejemplo n.º 12
0
        public void Echoサーバに送信して溜まったデータサイズ_lengthを確認する()
        {
            //setUp
            const string addr = "127.0.0.1";
            const int port = 9982;
            var sv = new EchoServer(addr, port);
            sv.Start();

            var sut = new SockTcp(new Kernel(), new Ip(addr), port, 100, null);
            const int max = 1000;
            for (int i = 0; i < 3; i++){
                sut.Send(new byte[max]);
            }

            Thread.Sleep(200);

            int expected = max*3;

            //exercise
            var actual = sut.Length();

            //verify
            Assert.That(actual, Is.EqualTo(expected));

            //tearDown
            sut.Close();
            sv.Stop();
        }
Ejemplo n.º 13
0
        EchoServer StartServer(int port,int enableAcl,Dat acl)
        {
            var ip = TestUtil.CreateIp("127.0.0.1");
            const int timeout = 300;
            var oneBind = new OneBind(ip, ProtocolKind.Tcp);
            var conf = TestUtil.CreateConf("OptionSample");
            conf.Set("port", port);
            conf.Set("multiple", 10);
            conf.Set("acl", acl);
            conf.Set("enableAcl", enableAcl);
            conf.Set("timeOut", timeout);

            var sv = new EchoServer(conf, oneBind);
            sv.Start();
            return sv;
        }