Delphi中record结构体定义发送接收传递使用

定义消息结构体TMsg:

type
PMsg = ^TMsg;
TMsg = record
FontName :string[20];
FontColor :Integer;
FontSize :Integer;
FontStyle :string[4];
Content :array[0..4096] of Char; //不能超过UDP包长
end;

通过UDP协议发送结构体及将结构体成员赋值给RichEdit控件属性:

procedure TForm1.btnSendClick(Sender: TObject);
var
Smsg:TMsg; //发送消息结构
OldCount:integer;
begin
//发送
Smsg.FontName:=RichEdit2.Font.Name;
Smsg.FontColor:=RichEdit2.Font.Color;
Smsg.FontSize:=RichEdit2.Font.Size;
Smsg.FontStyle:=DeStyle(RichEdit2.Font.Style);
StrPCopy(Smsg.Content,ConvertMsgToCmd(RichEdit2));
IdUDPServer1.SendBuffer(edtRemoteIP.Text,4001,Smsg,SizeOf(Smsg));
//本机显示
RichEdit1.SelStart:=MaxInt;
RichEdit1.Paragraph.FirstIndent:=0;
RichEdit1.SelAttributes.Color := clGreen;
RichEdit1.Lines.Add(DateTimeToStr(Now)+' : 你对 xxx 说 :');
RichEdit1.Paragraph.FirstIndent:=18;
OldCount:=Length(RichEdit1.Text);
RichEdit1.SelStart:=MaxInt;
RichEdit1.SelAttributes.Color :=Smsg.FontColor;
RichEdit1.SelAttributes.Name:=Smsg.FontName;
RichEdit1.SelAttributes.Size:=Smsg.FontSize;
RichEdit1.SelAttributes.Style:=EnStyle(Smsg.FontStyle);
ConvertMsgToFace(RichEdit1,Smsg.Content,OldCount);
end;

通过UDP协议接收结构体及将结构体成员赋值给RichEdit控件属性

procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
Rmsg:TMsg; //接收消息结构
OldCount:integer;
begin
//接收到的数据流直接可以传递给TMsg结构体,然后可以直接获取成员值
AData.ReadBuffer(Rmsg,AData.Size);
RichEdit1.SelStart:=MaxInt;
RichEdit1.Paragraph.FirstIndent:=0;
RichEdit1.SelAttributes.Color := clBlue;
RichEdit1.Lines.Add(DateTimeToStr(Now)+' : 来自 '+ABinding.PeerIP+' 的消息 :');
RichEdit1.Paragraph.FirstIndent:=18;
OldCount:=Length(RichEdit1.Text);
//showmessage(rmsg.FontName);
//showmessage(rmsg.FontStyle);
//showmessage(rmsg.Content);
RichEdit1.SelStart:=MaxInt;
RichEdit1.SelAttributes.Color :=Rmsg.FontColor;
RichEdit1.SelAttributes.Name:=Rmsg.FontName;
RichEdit1.SelAttributes.Size:=Rmsg.FontSize;
RichEdit1.SelAttributes.Style:=EnStyle(Rmsg.FontStyle);
ConvertMsgToFace(RichEdit1,Rmsg.Content,OldCount);
end;

 

posted @   IT情深  阅读(67)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示