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(); }
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(); }
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(); }
void Pipe() { _sockTcp = SockServer.CreateConnection(_kernel, _ip, _listenPort, null, this); if (_sockTcp != null){ while (_life){ var len = _sockTcp.Length(); if (len > 0){ const int tout = 3; //受信バイト数がわかっているので、ここでのタイムアウト値はあまり意味が無い var b = _sockTcp.Recv(len, tout, this); if (b != null){ _buffer = Bytes.Create(_buffer, b); } } if (_sockTcp.Length() == 0 && _sockTcp.SockState != SockState.Connect) break; } _sockTcp.Close(); } IsRecv = false; }
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(); }
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(); }
//�㏈�� protected override void OnRunThread() { //�{�� _kernel.View.SetColor();//�y�E�C���h�F�z //[C#] ThreadBaseKind = ThreadBaseKind.Running; if (_ip == new Ip(IpKind.V4_0)) { return; //���������s } while (IsLife()) { //TraceDlg traceDlg = null; Ssl ssl = null; var timeout = 3; _sockTcp = Inet.Connect(_kernel,_ip,_port,timeout,ssl); if (_sockTcp == null) { //isRun = false; _logger.Set(LogKind.Error, _sockTcp,4,string.Format("address={0} port={1}", _ip, _port)); //�Đڑ�����݂�̂́A2�b�� for (int i = 0; i < 20 && IsLife(); i++) { Thread.Sleep(100); } } else { _logger.Set(LogKind.Normal,_sockTcp,5,string.Format("address={0} port={1}",_ip,_port)); while (IsLife()) {//�ڑ��� if (_sockTcp.SockState != SockState.Connect){ //�ڑ����ꂽ�ꍇ�́A�����^�C�~���O��u���Ă���A�Đڑ������ɖ߂� //�Đڑ�����݂�̂́A1�b�� _sockTcp.Close(); for (int i = 0; i < 10 && IsLife(); i++) { Thread.Sleep(100); } break; } var o = RemoteData.Recv(_sockTcp,this); if (o==null) { Thread.Sleep(100); continue; } switch(o.Kind){ case RemoteDataKind.DatAuth://�F�؏��i�p�X���[�h�v���j var dlg = new PasswordDlg(_kernel); if (DialogResult.OK == dlg.ShowDialog()) { //�n�b�V��������̍쐬�iMD5�j string md5Str = Inet.Md5Str(dlg.PasswordStr + o.Str); //DAT_AUTH�ɑ���p�X���[�h(C->S) RemoteData.Send(_sockTcp,RemoteDataKind.CmdAuth, md5Str); } else { StopLife();//Ver5.8.4 �b�菈�u } break; case RemoteDataKind.DatVer://�o�[�W������� if (!_kernel.Ver.VerData(o.Str)) { //�T�[�o�ƃN���C�A���g�Ńo�[�W�����ɈႢ���L��ꍇ�A�N���C�A���g�@�\���~���� StopLife();//Ver5.8.4 �b�菈�u } else { IsConected = true;//�ڑ��� _kernel.View.SetColor();//�y�E�C���h�F�z //���O�C������ _logger.Set(LogKind.Normal,_sockTcp,10,""); } break; case RemoteDataKind.DatLocaladdress://���[�J���A�h���X LocalAddress.SetInstance(o.Str); //_kernel.LocalAddress = new LocalAddress(o.Str); break; case RemoteDataKind.DatTool://�f�[�^��M if (_toolDlg != null) { var tmp = o.Str.Split(new[] { '\t' }, 2); _toolDlg.CmdRecv(tmp[0],tmp[1]); } break; case RemoteDataKind.DatBrowse://�f�B���N�g������M if(_browseDlg != null) { _browseDlg.CmdRecv(o.Str); } break; case RemoteDataKind.DatTrace://�g���[�X��M _kernel.TraceDlg.AddTrace(o.Str); break; case RemoteDataKind.DatLog://���O��M _kernel.LogView.Append(new OneLog(o.Str));//���O�r���[�ւ̒lj� break; case RemoteDataKind.DatOption://�I�v�V�����̎�M //Option.ini���M����$remote.ini�ɏo�͂��� using (var sw = new StreamWriter(_optionFileName, false, Encoding.GetEncoding("Shift_JIS"))) { sw.Write(o.Str); sw.Close(); } _kernel.ListInitialize(); break; default: _logger.Set(LogKind.Error, null,999,string.Format("kind = {0}",o.Kind)); break; } } //err: _sockTcp.Close(); _sockTcp = null; IsConected = false;//�ڑ��f _kernel.Menu.InitializeRemote(_kernel.IsJp()); _kernel.View.SetColor(); _logger.Set(LogKind.Normal,null, 8,""); } } _logger.Set(LogKind.Normal,null, 7,"");//�����[�g�N���C�A���g��~ }
protected override void OnSubThread(SockObj sockObj) { _sockTcp = (SockTcp)sockObj; //************************************************************* // �p�X���[�h�F�� //************************************************************* var password = (string)Conf.Get("password"); if (password == ""){ Logger.Set(LogKind.Normal,_sockTcp,5,""); }else{//�p�X���[�h�F���K�v�ȏꍇ var challengeStr = Inet.ChallengeStr(10);//�`�������W������̐��� RemoteData.Send(_sockTcp, RemoteDataKind.DatAuth, challengeStr); //�p�X���[�h�̉����҂� var success = false;//Ver5.0.0-b14 while (IsLife() && _sockTcp.SockState == Bjd.sock.SockState.Connect) { var o = RemoteData.Recv(_sockTcp,this); if(o!=null){ if (o.Kind == RemoteDataKind.CmdAuth) { //�n�b�V��������̍쐬�iMD5�j var md5Str = Inet.Md5Str(password + challengeStr); if(md5Str != o.Str) { Logger.Set(LogKind.Secure,_sockTcp,4,""); //DOS�� 3�b�Ԃ͎��̐ڑ���t���Ȃ� //for (int i = 0; i < 30 && life; i++) { // Thread.Sleep(100); //} //tcpObj.Close();//���̐ڑ��͔j������� //return; } else { success = true;//Ver5.0.0-b14 } break; } }else{ Thread.Sleep(500); } } //Ver5.0.0-b14 if(!success) { //�F�؎��s�i�p�X���[�h�L�����Z���E�p�X���[�h�Ⴂ�E�����ؒf�j //DOS�� 3�b�Ԃ͎��̐ڑ���t���Ȃ� for(var i = 0;i < 30 && IsLife();i++) { Thread.Sleep(100); } _sockTcp.Close();//���̐ڑ��͔j������� return; } } //************************************************************* // �F�؊��� //************************************************************* Logger.Set(LogKind.Normal,_sockTcp,1,string.Format("address={0}",_sockTcp.RemoteAddress.Address)); //�o�[�W����/���O�C�������̑��M RemoteData.Send(_sockTcp, RemoteDataKind.DatVer, Kernel.Ver.VerData()); //kernel.LocalAddress��Remote���Ő������� RemoteData.Send(_sockTcp, RemoteDataKind.DatLocaladdress, LocalAddress.GetInstance().RemoteStr()); //�I�v�V�����̑��M var optionFileName = string.Format("{0}\\Option.ini",Kernel.ProgDir()); string optionStr; using (var sr = new StreamReader(optionFileName, Encoding.GetEncoding("Shift_JIS"))) { optionStr = sr.ReadToEnd(); sr.Close(); } RemoteData.Send(_sockTcp, RemoteDataKind.DatOption, optionStr); Kernel.RemoteConnect = new Bjd.remote.RemoteConnect(_sockTcp);//�����[�g�N���C�A���g�ڑ��J�n Kernel.View.SetColor();//�E�C���h�F�̏����� while (IsLife() && _sockTcp.SockState == Bjd.sock.SockState.Connect) { var o = RemoteData.Recv(_sockTcp,this); if (o==null) continue; //�R�}���h�́A���ׂăL���[�Ɋi�[���� _queue.Enqueue(o); if (_queue.Count == 0) { GC.Collect(); Thread.Sleep(500); } else { Cmd(_queue.Dequeue()); } } Kernel.RemoteConnect = null;//�����[�g�N���C�A���g�ڑ��I�� Logger.Set(LogKind.Normal, _sockTcp, 2, string.Format("address={0}", _sockTcp.RemoteAddress.Address)); Kernel.View.SetColor();//�E�C���h�F�̏����� _sockTcp.Close(); }