Beispiel #1
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();
        }
Beispiel #2
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();
        }
Beispiel #3
0
 private void Tcp(SockTcp sockTcp)
 {
     while (IsLife() && sockTcp.SockState == Bjd.sock.SockState.Connect){
         Thread.Sleep(0); //これが無いと、別スレッドでlifeをfalseにできない
         var len = sockTcp.Length();
         if (0 < len){
             const int timeout = 10;
             var buf = sockTcp.Recv(len, timeout, this);
             sockTcp.Send(buf);
             break; //echoしたらセッションを閉じる
         }
     }
 }
Beispiel #4
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();
        }
Beispiel #5
0
        private int SendBinary(SockTcp sockTcp, String fileName)
        {
            var sb = new StringBuilder();
            sb.Append(string.Format("SendBinary({0}) ", fileName));

            int size = 0;

            using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)){
                using (var br = new BinaryReader(fs)){
                    var buf = new byte[3000000];
                    while (IsLife()) {
                        var len = br.Read(buf, 0, 3000000);
                        if (len <= 0) {
                            break;
                        }
                        //if (oneSsl != null) {
                        //}else{
                        sockTcp.Send(buf,len);
                        //}
                        //�g���[�X�\��
                        sb.Append(string.Format("Binary={0}byte ", len));
                        size += len;
                    }
                }
            }

            //noEncode = true; //�o�C�i���ł��鎖���������Ă���
            //Trace(TraceKind.Send, Encoding.ASCII.GetBytes(sb.ToString()), true); //�g���[�X�\��
            return size;
        }