Delphi IdTcpServer IdTcpClient 传输简单文本

Delphi IdTcpServer IdTcpClient 传输简单文本

  • [已经很久敲代码了,想找一段直接Delphi11 TCP简单文本传输,费劲!](#已经很久敲代码了,想找一段直接Delphi11 TCP简单文本传输,费劲!)
    • [FStringStream 、FStrStream : FStringStream:=TStringStream.Create('',TEncoding.UTF8);](#FStringStream 、FStrStream : FStringStream:=TStringStream.Create('',TEncoding.UTF8);)

已经很久敲代码了,想找一段直接Delphi11 TCP简单文本传输,费劲!

其实非常简单,因为Delphi11已经基于宽字符串了,所以,老猿们会想多,不多说上代码:

服务端:

python 复制代码
procedure TMainForm.IdTCPServer1Execute(AContext: TIdContext);
var
    s:String;
begin
    AContext.Connection.IOHandler.DefStringEncoding := IndyTextEncoding_UTF8;
    begin
        FStrStream.Clear;
        AContext.Connection.IOHandler.ReadStream(FStrStream, -1, False);
        FStrStream.Position := 0;
        ShowTip( FStrStream.DataString, 50000 );
    end;
end;

客户端:

python 复制代码
procedure TMainForm.SpeedButton7Click(Sender: TObject);
var Len:Int64;
    s : String;
begin
try
    s   := Trim(TipContent.text);
    s   := StringReplace( s, #10, '',  [rfReplaceAll, rfIgnoreCase] );
    s   := StringReplace( s, #13, '',  [rfReplaceAll, rfIgnoreCase] );
    s   := StringReplace( s, ' ', '',  [rfReplaceAll, rfIgnoreCase] );
    Len := length( s );
    if (Len > 32) then
    begin
      TipSendInfo.Caption := ' 超过32字未发送.';
      Exit;
    end else if ( Len < 1 ) then
             begin
                TipSendInfo.Caption := ' 无内容...';
                Exit;
             end
             else
                TipSendInfo.Caption:='';
    if not IdTCPClient1.Connected then
    begin
        IdTCPClient1.Host:=FShowTipIP;
        IdTCPClient1.Port:=FShowTipPort;
        IdTCPClient1.ConnectTimeout:=3000;
        IdTCPClient1.Connect;
    end;
    if not IdTCPClient1.Connected then
        TipSendInfo.Caption:='链接失败'
    else
    begin
        FStringStream.Clear;
        WriteStringToStream(FStringStream, Trim(s), enUTF8);
        IdTCPClient1.IOHandler.Write( FStringStream,0,true );
        TipSendInfo.Caption:=' 发送完成';
    end;
Except
    On E : Exception Do
    TipSendInfo.Caption := ' 异常:'+E.Message;
end;
end;

FStringStream 、FStrStream : FStringStream:=TStringStream.Create('',TEncoding.UTF8);

无握手协议,简单文本传输(差一点去翻原生Socket代码),好了就这些,去加个心跳。

相关推荐
知识中的海王1 分钟前
Python html 库用法详解
开发语言·python
獨枭19 分钟前
配置 macOS 上的 Ruby 开发环境
开发语言·macos·ruby
Code Warrior23 分钟前
【Linux】Linux基础指令3
linux·服务器
飞由于度24 分钟前
C#中清空DataGridView的方法
开发语言·c#
朝朝又沐沐1 小时前
基于算法竞赛的c++编程(18)string类细节问题
开发语言·c++·算法
光路科技1 小时前
TSN交换机正在重构工业网络,PROFINET和EtherCAT会被取代吗?
服务器·网络·重构
黄雪超1 小时前
JVM——对象模型:JVM对象的内部机制和存在方式是怎样的?
java·开发语言·jvm
半桔1 小时前
【Linux手册】探秘系统世界:从用户交互到硬件底层的全链路工作之旅
linux·运维·服务器·面试·centos
wanhengidc2 小时前
服务器中CC攻击的特点有哪些?
运维·服务器
小杜的生信筆記2 小时前
生信服务器 | 做生信为什么推荐使用Linux服务器?
linux·运维·服务器