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 bool Recv(SockTcp sockTcp, int sec,Logger logger, ILife iLife) { var dtLast = DateTime.Now; //受信が20秒無かった場合は、処理を中断する while (iLife.IsLife()){ if (dtLast.AddSeconds(sec) < DateTime.Now){ return false; //タイムアウト } var len = sockTcp.Length(); if (len == 0){ continue; } var buf = sockTcp.Recv(len, sec, iLife); if (buf == null){ return false; //切断された } dtLast = DateTime.Now; var recvStatus = Append(buf); if (recvStatus == RecvStatus.Limit){ //サイズ制限 if (logger != null){ logger.Set(LogKind.Secure, sockTcp, 7, string.Format("Limit:{0}KByte", _sizeLimit)); } sockTcp.AsciiSend("552 Requested mail action aborted: exceeded storage allocation"); return false; } if (recvStatus == RecvStatus.Finish){ return true; } } return false; }
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; }
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したらセッションを閉じる } } }
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(); }
//�t�@�C����M�i�o�C�i���j private int RecvBinary(SockTcp sockTcp, String fileName) { var sb = new StringBuilder(); sb.Append(string.Format("RecvBinary({0}) ", fileName)); var fs = new FileStream(fileName, FileMode.Create); var bw = new BinaryWriter(fs); fs.Seek(0, SeekOrigin.Begin); var size = 0; const int timeout = 3000; while (IsLife()){ int len = sockTcp.Length(); if (len < 0){ break; } if (len == 0){ if (sockTcp.SockState != Bjd.sock.SockState.Connect){ break; } Thread.Sleep(10); continue; } byte[] buf = sockTcp.Recv(len, timeout, this); if (buf.Length != len){ throw new IOException("buf.length!=len"); } bw.Write(buf, 0, buf.Length); //�g���[�X�\�� sb.Append(string.Format("Binary={0}byte ", len)); size += len; } bw.Flush(); bw.Close(); fs.Close(); //noEncode = true; //�o�C�i���ł��鎖���������Ă��� //Trace(TraceKind.Send, Encoding.ASCII.GetBytes(sb.ToString()), true); //�g���[�X�\�� return size; }