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

相关推荐
LyaJpunov7 分钟前
C++中move和forword的区别
开发语言·c++
pyliumy7 分钟前
rsync 全网备份
linux·运维·服务器
程序猿练习生12 分钟前
C++速通LeetCode中等第9题-合并区间
开发语言·c++·leetcode
一名路过的小码农22 分钟前
C/C++动态库函数导出 windows
c语言·开发语言·c++
m0_6312704024 分钟前
标准c语言(一)
c语言·开发语言·算法
万河归海42824 分钟前
C语言——二分法搜索数组中特定元素并返回下标
c语言·开发语言·数据结构·经验分享·笔记·算法·visualstudio
Messiah___30 分钟前
【论文阅读】Slim Fly: A Cost Effective Low-Diameter Network Topology 一种经济高效的小直径网络拓扑
开发语言·php
ggb199939 分钟前
【python的坑】vpn下,python request报错 check_hostname requires server_hostname
linux·运维·服务器
农民小飞侠1 小时前
python AutoGen接入开源模型xLAM-7b-fc-r,测试function calling的功能
开发语言·python
指尖流烟1 小时前
C#调用图表的使用方法
开发语言·c#