帮我看看UDP接收与发送出错的地方 ( 积分: 50 )

  • 主题发起人 主题发起人 十夜
  • 开始时间 开始时间

十夜

Unregistered / Unconfirmed
GUEST, unregistred user!
这时客户端
unit uMain;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, cxControls, cxContainer, cxEdit, cxTextEdit,
cxMaskEdit, cxSpinEdit, IdAntiFreezeBase, IdAntiFreeze, IdBaseComponent,
IdComponent, IdUDPBase, IdUDPClient;

type
TSmsPack = packed record
ID:String;
Name:String;
Addr:String;
Phone:Integer;
end;


type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
cxSpinEdit1: TcxSpinEdit;
Button1: TButton;
IdUDPClient1: TIdUDPClient;
IdAntiFreeze1: TIdAntiFreeze;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
PaPack:TSmsPack;
procedure GetPack;
procedure SendPack;
public
{ Public declarations }
end;

var
Form1: TForm1;


implementation

{$R *.dfm}

procedure TForm1.GetPack;
begin
PaPack.ID := Trim(Edit1.Text);
PaPack.Name := Trim(Edit2.Text);
PaPack.Addr := Trim(Edit3.Text);
PaPack.Phone := cxSpinEdit1.Value;
end;

procedure TForm1.SendPack;
var
sReturnValue:String;
begin
IdUDPClient1.ReceiveTimeout := 5000;
IdUDPClient1.Port := 777;
IdUDPClient1.Active := True;
IdUDPClient1.SendBuffer(PaPack,SizeOf(PaPack));
sReturnValue := IdUDPClient1.ReceiveString();
ListBox1.Items.Add(sReturnValue);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
GetPack;
SendPack;
end;

end.




这是服务器端
unit uClient;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdComponent, IdUDPBase, IdUDPServer, IdBaseComponent,
IdAntiFreezeBase, IdAntiFreeze, ComCtrls, IdSocketHandle, StdCtrls ;

type
TSmsPack = packed record
ID:String;
Name:String;
Addr:String;
Phone:Integer;
end;

type
TfrmClient = class(TForm)
IdAntiFreeze1: TIdAntiFreeze;
IdUDPServer1: TIdUDPServer;
StatusBar1: TStatusBar;
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
private
{ Private declarations }
public
{ Public declarations }
end;

var
frmClient: TfrmClient;

implementation

{$R *.dfm}

procedure TfrmClient.FormCreate(Sender: TObject);
begin
IdUDPServer1.DefaultPort := 777;
IdUDPServer1.Active := True;

end;

procedure TfrmClient.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
UnPack : TSmsPack;
s:String;
s1,s2,s3:String;
begin
AData.ReadBuffer(UnPack,AData.Size);
//以下出错的地方
s1 := UnPack.ID; //出错
s2 := UnPack.Name; // 出错
s3 := UnPack.Addr; // 出错
s:='ok';
ABinding.SendTo(ABinding.PeerIP,ABinding.PeerPort,s[1],Length(s));
end;

end.

为什么把这个结构体发送到另外一个程序中的结构体时,赋值出错的地方。
出错的地方已注明出来。
 
这时客户端
unit uMain;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, cxControls, cxContainer, cxEdit, cxTextEdit,
cxMaskEdit, cxSpinEdit, IdAntiFreezeBase, IdAntiFreeze, IdBaseComponent,
IdComponent, IdUDPBase, IdUDPClient;

type
TSmsPack = packed record
ID:String;
Name:String;
Addr:String;
Phone:Integer;
end;


type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
cxSpinEdit1: TcxSpinEdit;
Button1: TButton;
IdUDPClient1: TIdUDPClient;
IdAntiFreeze1: TIdAntiFreeze;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
PaPack:TSmsPack;
procedure GetPack;
procedure SendPack;
public
{ Public declarations }
end;

var
Form1: TForm1;


implementation

{$R *.dfm}

procedure TForm1.GetPack;
begin
PaPack.ID := Trim(Edit1.Text);
PaPack.Name := Trim(Edit2.Text);
PaPack.Addr := Trim(Edit3.Text);
PaPack.Phone := cxSpinEdit1.Value;
end;

procedure TForm1.SendPack;
var
sReturnValue:String;
begin
IdUDPClient1.ReceiveTimeout := 5000;
IdUDPClient1.Port := 777;
IdUDPClient1.Active := True;
IdUDPClient1.SendBuffer(PaPack,SizeOf(PaPack));
sReturnValue := IdUDPClient1.ReceiveString();
ListBox1.Items.Add(sReturnValue);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
GetPack;
SendPack;
end;

end.




这是服务器端
unit uClient;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdComponent, IdUDPBase, IdUDPServer, IdBaseComponent,
IdAntiFreezeBase, IdAntiFreeze, ComCtrls, IdSocketHandle, StdCtrls ;

type
TSmsPack = packed record
ID:String;
Name:String;
Addr:String;
Phone:Integer;
end;

type
TfrmClient = class(TForm)
IdAntiFreeze1: TIdAntiFreeze;
IdUDPServer1: TIdUDPServer;
StatusBar1: TStatusBar;
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
private
{ Private declarations }
public
{ Public declarations }
end;

var
frmClient: TfrmClient;

implementation

{$R *.dfm}

procedure TfrmClient.FormCreate(Sender: TObject);
begin
IdUDPServer1.DefaultPort := 777;
IdUDPServer1.Active := True;

end;

procedure TfrmClient.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
UnPack : TSmsPack;
s:String;
s1,s2,s3:String;
begin
AData.ReadBuffer(UnPack,AData.Size);
//以下出错的地方
s1 := UnPack.ID; //出错
s2 := UnPack.Name; // 出错
s3 := UnPack.Addr; // 出错
s:='ok';
ABinding.SendTo(ABinding.PeerIP,ABinding.PeerPort,s[1],Length(s));
end;

end.

为什么把这个结构体发送到另外一个程序中的结构体时,赋值出错的地方。
出错的地方已注明出来。
 
那位大哥给指点一下
 
大哥,小弟在线等呀
 
type
TSmsPack = packed record
ID:String;
Name:String;
Addr:String;
Phone:Integer;
end;

这里面不要用String,换成ShortString
 
shortString最多可以接受多长字符
 
接受答案了.
 

Similar threads

后退
顶部