请问delphi怎样用socket发送、接受record类型的数据?(200分)

  • 主题发起人 主题发起人 age
  • 开始时间 开始时间
A

age

Unregistered / Unconfirmed
GUEST, unregistred user!
请问delphi怎样用socket发送、接受record类型的数据?谢谢了
 
SendBuf方法呀
 
socket发送的是一个流,你可以用sendbuf发送record的内容
例如:TApple = record
color:TColor;
weight:Integer;
end;
var
apple:TApple;
begin
apple.color := clRed;
apple.weight:= 12;
ClientSocket1.Socket.SendBuf(@apple,sizeof(TApple));
end;
接收可以用ClientSocket1.Socket.ReveiveBuf(@apple,sizeof(TApple));

 
Delphi里的例子写得很明白,一看就懂!
 
ting

我就不明白
接受后呢?
怎么处理
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, NMUDP;

type
TForm1 = class(TForm)
NMUDP1: TNMUDP;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
Tapple=Record
Color:string;
name:string;
end;
var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var aaa:Tapple;
begin
aaa.Color:='Red';
aaa.name:='****';
NMUDP1.SendBuffer(@aaa,length(aaa));
end;

end.


错误:aaa不是array and point
 
自己定义一个专门处理记录型数据的协议,

Tapple=Record
Color:string;
name:string;
end;
var aaa:Tapple;

aaa.Color:='Red';
aaa.name:='****';

发送时:
var str:string;
str:='aaa'+'#'+aaa.Color+'#'+aaa.name;
将str发送给对方,

接收时:
接收信息存放在str1 中,
取出变量名:Recname:=copy(str1, 1, pos('#',str1)-1);
取出name 值:Recname.name;=...
取出Color 值:Recname.Color:=...


 
晕,这样才行。。。
例如:TApple = record
color:TColor;
weight:Integer;
end;
var
apple:TApple;
begin
apple.color := clRed;
apple.weight:= 12;
ClientSocket1.Socket.SendBuf(apple,sizeof(TApple));
end;
接收可以用ClientSocket1.Socket.ReveiveBuf(apple,sizeof(TApple));
 
在程序客户端和服务器端建立同样的record类型,
例如:TApple = record
color:TColor;
weight:Integer;
name:string[20];
end;
var
apple:TApple;
begin
apple.color := clRed;
apple.weight:= 12;
apple.name:='jiajiajia';
ClientSocket1.Socket.SendBuf(apple,sizeof(TApple));
end;

服务器端使用socket.receivebuf(apple,sizeof(apple)接收到程序后,直接取类型中的值就可以了,如color:Tcolor,Weight:integer;
color=apple.color;不过要注意如果是字符串或字符,或字节型数据要定义长度,见上面name的定义。


 
后退
顶部