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代码),好了就这些,去加个心跳。

相关推荐
你好潘先生21 小时前
别再记命令了,用 yeero do 说句人话就能跑脚本,而且不烧 token
服务器·python·命令行
程序员老赵2 天前
服务器文件不想 SFTP 上传?Docker 跑个 File Browser,浏览器就能管理
服务器·docker·开源
vivo互联网技术2 天前
从 10 分钟到 1 秒:ES 深度分页任意跳页的三轮优化实战
服务器·数据库·redis·elasticsearch·深度分页
zzzzzz31013 天前
9K Star 炸裂开源!这个 C 语言写的代码知识图谱,把 Linux 内核索引压缩到了 3 分钟
linux·服务器·sql
大树8817 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
LDR00617 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术17 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
码云数智-园园17 天前
C++20 Modules 模块详解
java·开发语言·spring
小宇宙Zz17 天前
Maven依赖冲突
java·服务器·maven