小弟我刚学习indy控件使用,我在使用IndyTcpClient接收IndytcpServe传来的ASCII数据时,发现IndyTcpClient并没有数据到达触发事件,请问我该如何编写代码使当ASCII数据到达时,触发一事件?

解决方案 »

  1.   

    就向IndytcpServe控件中有数据连接到来时,就会触发OnExcuete事件一样,IndyTcpClient该如何做?
      

  2.   

    当server端用writeln()发送时,client端用readln()接受就可以了,详情见demo,可从网上下载
      

  3.   

    第一种方法:用timer控件来读取IndyTcpClient,设置为100毫秒读取一次。
    第二方法:采用多线程来读取IndyTcpClient。
      

  4.   

    写一个线程,客户端一点连接到服务器后就创建并运行该线程,
    线程里面:
      while ((not Terminated) and dm.tcpclient.Connected do
      begin
        ires := DM.tcpLog.ReadFromStack;
        if ires > 0 then
        begin
          //此处处理
        end;
      end;
      

  5.   

    多线程就参考dabaicai(菜鸟) 的代码。
    服务器端OnExcuete怎么读取,客户端的和服务器端的差不多,不过就是没有单独的事件而已,,如果你对多线程不了解,可以先用timer控件来写,再试着用多线程。方法就是这样,再不行,到网上搜索indy的例子,很多的。
      

  6.   

    IndyTcpClient是阻塞式的,在readln的时候程序会一直停在那,知道读到数据或超时,所以要用timer或thread,建议用thread好一些,timer自己学着玩可以,千万别实际应用。
      

  7.   

    最好用线程,tidtcpclient连接成功后就生成一个线程,来监控服务短发过来的数据
      

  8.   

    建议不要用timer接收
    多线程也不一定好,得看具体情况.
    1.推荐用阻断器吧,比如TIdLogEvent
    2.自己定义数据包,用  IdTCPClient1.ReadBuffer;,参考indydemo
      

  9.   

    下面的代码是实现从客户端发送一个命令给服务端,
    服务端接收到命令后截取屏幕并发送给客户端,客户端接收保存显示
    客户端:
        var
        ftmpStream : TFileStream;
        bitmap:Tbitmap;
    begin
    try
    with IdTCPClient do
        begin
        if connected then DisConnect;
        Host := edtServerHost.text;
        Port := StrToInt(edtServerPort.text);
        Connect;
        WriteLn('SRN');
        // delete if exists
        // in production situation you might store binary downloads like this in a cache folder
        if FileExists(ExtractFileDir(ParamStr(0)) + '\ServerScreen.bmp') then
            DeleteFile(ExtractFileDir(ParamStr(0)) + '\ServerScreen.bmp');
        ftmpStream := TFileStream.Create(ExtractFileDir(ParamStr(0)) + '\ServerScreen.bmp',fmCreate);
    //   bmpstream.Clear;
    //   bitmap:=Tbitmap.Create;
        while connected do
    //      ReadStream(bmpStream,-1,true);
            ReadStream(fTmpStream,-1,true);
        FreeAndNil(fTmpStream);
        Disconnect;
        imgMain.Picture.LoadFromFile(ExtractFileDir(ParamStr(0)) + '\ServerScreen.bmp');
    //      bitmap.LoadFromStream(bmpStream);
    //      imgMain.Picture.Bitmap:=bitmap;
        end;
    except
    on E : Exception do
        ShowMessage(E.Message);
    end;服务端:
    procedure TFMain.IdTCPServerExecute(AThread: TIdPeerThread);
    var
        s, sCommand, sAction : string;
        fStream : TFileStream;
        tBM : tbitmap;
    begin
    CS.Enter;
    try
    s := uppercase(AThread.Connection.ReadLn);
    sCommand := copy(s,1,3);
    sAction := copy(s,5,100);
    if sCommand = 'SRN' then
        begin
        // in production version you would use a unique file name such as one generated
        // from a tickcount plus clint IP / id etc.
        // take snapshot
        GetBitMap();
        // copy file stream to write stream
        AThread.Connection.OpenWriteBuffer;
        AThread.Connection.WriteStream(bmpStream);
        AThread.Connection.CloseWriteBuffer;
        // free the file stream
        bmpstream.Clear;    AThread.Connection.Disconnect;
        End
    else
    if (sCommand <> 'LST') and (sCommand <> 'PIC') and (sCommand <> 'SRN') then
        Begin
        AThread.Connection.WriteLn('ERR : Unknown command / action');
        AThread.Connection.Disconnect;
        end;
    except
    on E : Exception do
    ShowMessage(E.Message);
    End;
    CS.Leave;
    end;