• Delphi XE IdTCPClient1 和 IdTCPServer1 数据的发送与接收(indy10)


    Delphi XE  IdTCPClient1 和 IdTCPServer1 数据的发送与接收(indy10)

    1、IdTCPClient1 端 发送数据

    1.1 发送结构体:

    //定义结构体
    TMData = record
          id:Integer;
          Name:Array[0..20] of Char;
          Age:Byte;
          UpdateTime:double;
      end;
    
    //发送
    procedure TForm2.Button2Click(Sender: TObject);
    var
      SendD: TMData;
    begin
      SendD.ID := 10;
    StrPCopy(SendD.Name, 'Delphi 您好');
    SendData.age := 18;
    SendD.UpdateTime := Now;
    IdTCPClient1.IOHandler.Write(#100); //提前发送一个标识符,用于区分数据 IdTCPClient1.IOHandler.Write(RawToBytes(SendD, SizeOf(SendD)));
    end;

    1.2 发送TStrings类型

    procedure TForm2.Button3Click(Sender: TObject);
    var
       sList:TStrings;
       I:Integer;
    begin
      sList := TStringList.Create;
      for I :=0  to 50 do
      begin
        sList.Add('数据Test' + IntToStr(i));
      end;
      IdTCPClient1.IOHandler.Write(#200);  
      IdTCPClient1.IOHandler.Write(sList.Count);
      IdTCPClient1.IOHandler.Write(ToBytes(sList.Text,TIdTextEncoding.UTF8));
    end;

    1.3 发送一行字符串数据

    procedure TForm2.Button4Click(Sender: TObject);
    begin
      IdTCPClient1.IOHandler.Write(#10);
      IdTCPClient1.IOHandler.Write('Delphi测试',TIdTextEncoding.UTF8);  
    end;
    

      

    2、IdTCPServer端 接收数据:

    procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
    var
       RData:TMData;
       buf:TIdBytes;
       sCmd:Char;
       sList:TStrings;
       I:Integer;
       ListCount:Integer;
    begin
       sCmd := AContext.Connection.IOHandler.ReadChar;  //先读取Char结构数据
       if sCmd = #100 then  //接收结构体
       begin
         AContext.Connection.IOHandler.ReadBytes(buf,SizeOf(RData));
         BytesToRaw(buf, RData, SizeOf(RData));
         with Memo1.lines do begin
           Add('ID:'+Inttostr(RData.Id));
           Add('Name:'+StrPas(RData.Name));
           Add('Age:'+Inttostr(ReadData.age));
           Add('UpdateTime:'+DateTimeToStr(RData.UpdateTime));
         end;
       end else if sCmd = #200 then  //接收 TStrings
       begin
          ListCount := AContext.Connection.IOHandler.ReadLongInt;  //ReadLongInt
          sList := TStringList.Create;
          try
            AContext.Connection.IOHandler.ReadStrings(sList,ListCount,TIdTextEncoding.UTF8);
            for I :=0  to sList.Count-1 do  begin
              Memo1.Lines.Add(sList.Strings[I]);
            end;
          finally
            sList.Free;
          end;
       end else if sCmd = #10 then
       begin
         Memo1.Lines.Add(AContext.Connection.IOHandler.ReadString(AContext.Connection.IOHandler.InputBuffer.Size,TIdTextEncoding.UTF8) );
       end else
         AContext.Connection.IOHandler.InputBuffer.Clear;  //清除
    end;
    

      

      

    创建时间:2020.06.23  更新

  • 相关阅读:
    必须要狠狠的喷一把苹果
    机械键盘四种轴试用体验
    linux终端快捷键
    挥别我在软件开发的第一个公司
    mysql 查询优化
    oracle 用户创建这个挺靠谱
    重置了下系统好多关于mysql密码的文章都很渣拷分好的备用
    关于WebView的复习
    Bpmx实施经验
    使用Nexus私服代理其他maven仓库(jitpack、jcenter)
  • 原文地址:https://www.cnblogs.com/guorongtao/p/13181802.html
Copyright © 2020-2023  润新知