zoukankan      html  css  js  c++  java
  • IdTCPClient中memorystream 的接收问题!急+郁闷!一晚上没折腾出来!(indy9)

    来源:http://topic.csdn.net/t/20041111/00/3541598.html

      

    idTCPClient中memorystream 的接收问题!急+郁闷!一晚上没折腾出来!

      

      ms:   Tmemorystream;   

      
      Server端:  
      AThread.Connection.WriteStream(ms,true,true);  
       
      Client端:  
      IdTCPClient1.ReadStream(ms,   -1,   True);  
       
      为什么Client端停在这行就不动了   ?  
      我用wpe查看   数据已经过来了!  
       
      这里是不是必须判断MS的长度?  
      测试了几种参数都是阻塞到这就完了!搜索了一下也没找到答案!  
      那位给个例子     郁闷死了。。。。  


    如果是没有断开的情况你在客户端read的,把第三个参数设置为true的话,第二个参数设置为你传输stream的大小。  
       
      可以这样修改:  
      Server端:  
      AThread.Connection.WriteInteger(ms.size);     //先发送大小;  
      AThread.Connection.WriteStream(ms,true,true);  
       
      Client端:  
      size   :=   IdTCPClient1.ReadInteger;   //先读取大小。  
      IdTCPClient1.ReadStream(ms,   size   ,   False);  


    还有一个方法是,不发送大小,客户端读取buffer的大小,但是我没有试验成功。楼主可以试试。  
       
      Server端:  
      AThread.Connection.WriteStream(ms,true,true);  
       
      Client端:  
      size   :=   IdTCPClient1.InputBuffer.Size;   //先读取大小。  
      IdTCPClient1.ReadStream(ms,   size   ,   False);


    在服务端发送完毕后,关掉连接,客户端的ReadStream就会返回(不然就会停在那里,一直等待读取,当然指定流的长度也是一样的道理)。


    Server端:  
      AThread.Connection.OpenWriteBuffer;  
      AThread.Connection.WriteStream(ms,true,true);  
      AThread.FlushBuffer;  
      AThread.Connection.CloseWriteBuffer;  
       
      Client端:  
      IdTCPClient1.ReadStream(ms,   -1,   False);  
       
      酱紫……


    如果是断开的情况,用   WGYKING(修罗是谁?!)   的方法可以,  
       
      不断开还是要先接收大小。


    定义如下:  
      type  
          ARecord   =   packed   record  
              Command   :   Integer;  
              Content   :   String[30];  
          end;  
       
      var  
          CommBlock   :   ARecord;  
       
      服务端:  
      procedure   TForm1.IdTCPServer1Exception(AThread:   TIdPeerThread;  
          AException:   Exception);  
      var  
          ms   :   TMemoryStream;  
      begin  
          ms   :=   TMemoryStream.Create;  
          try  
              CommBlock.Command   :=   4003;  
              CommBlock.Content   :=   'Test   ReadStream';  
              ms.WriteBuffer(CommBlock,   sizeof(CommBlock));  
       
              AThread.Connection.WriteInteger(ms.Size);       //先发送大小  
              AThread.Connection.OpenWriteBuffer;  
              try  
                  AThread.Connection.WriteStream(ms);  
                  AThread.Connection.CloseWriteBuffer;  
              except  
                  AThread.Connection.CancelWriteBuffer;  
              end;  
          finally  
              FreeAndNil(ms);  
          end;  
      end;  
       
      客户端:用timer控件读取,Interval=500,也可以用线程。  
      procedure   TForm1.Timer1Timer(Sender:   TObject);  
      var  
          ms   :   TMemoryStream;  
          size   :   Integer;  
      begin  
          if   not   IdTCPClient1.Connected   then   Exit;  
          ms   :=   TMemoryStream.Create;  
          try  
              size   :=   IdTCPClient1.ReadInteger;         //先读取大小。  
              if   size   =   0   then  
              begin  
                  FreeAndNil(ms);  
                  Exit;  
              end;  
              IdTCPClient1.ReadStream(ms,   size,   True);  
              ms.Position   :=   0;  
              ms.ReadBuffer(CommBlock,   sizeof(CommBlock));  
              RichEdit1.Lines.Add(IntToStr(CommBlock.Command));  
              RichEdit1.Lines.Add(CommBlock.Content);  
          finally  
              FreeAndNil(ms);  
          end;  
      end;  
       
      其它的设置你自己应该能够搞定,如果还不可以,留下mail我把这个例子发给你。

      

    注意:  
       
      服务端:  
       
              ms.WriteBuffer(CommBlock,   sizeof(CommBlock));  
       
              //AThread.Connection.WriteInteger(ms.Size);       //先发送大小  
              AThread.Connection.OpenWriteBuffer;  
              try  
                  AThread.Connection.WriteStream(ms,   True,   True);     //和上面的区别,如果这样,上面发送大小就不要了。  
                  AThread.Connection.CloseWriteBuffer;  
              except  
                  AThread.Connection.CancelWriteBuffer;  
              end;  
          finally  
              FreeAndNil(ms);  


    搞成这才正常接收为什么?   IdTCPClient1.ReadStream(ms,   Size,   False);
  • 相关阅读:
    c/c++ linux 进程间通信系列7,使用pthread mutex
    c/c++ linux 进程间通信系列6,使用消息队列(message queue)
    c/c++ linux 进程间通信系列5,使用信号量
    eos 创建两对的公钥和私钥, 钱包,交易所转账到主网,主网到交易所
    c/c++ linux 进程间通信系列4,使用共享内存
    python基础-网络编程part02
    idea新建项目相关名词意义
    idea中当前模块资源目录上显示属于其他模块
    centos下安装rabbitmq
    JAVA中值传递,引用传递
  • 原文地址:https://www.cnblogs.com/railgunman/p/1814099.html
Copyright © 2011-2022 走看看