已经很久敲代码了,想找一段直接Delphi11 TCP简单文本传输,费劲!
其实非常简单,因为Delphi11已经基于宽字符串了,所以,老猿们会想多,不多说上代码:
服务端:
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;
客户端:文章来源:https://www.toymoban.com/news/detail-691907.html
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代码),好了就这些,去加个心跳。文章来源地址https://www.toymoban.com/news/detail-691907.html
到了这里,关于Delphi IdTcpServer IdTcpClient 传输简单文本的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!