C
ChandlerBing
Unregistered / Unconfirmed
GUEST, unregistred user!
发送端
unit Unit13;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdUDPServer, IdBaseComponent, IdComponent, IdUDPBase,
IdUDPClient,IdSocketHandle;
type
TForm1 = class(TForm)
IdUDPClient1: TIdUDPClient;
SIPEdit: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
SenderEdit: TEdit;
SPortEdit: TEdit;
Button2: TButton;
NKEdit: TEdit;
Memo1: TMemo;
Label5: TLabel;
TypeEdit: TEdit;
Label6: TLabel;
Label4: TLabel;
PWEdit: TEdit;
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
type
Msg = class(TObject)
private
Sender : String[2];
MsgType : String[5];
protected
public
constructor Create ;
published
end;
type
Msg_FORID = class(Msg)
private
NickName : String[16];
Password : String[16];
protected
public
constructor Create ;
published
end;
constructor Msg.Create;
begin
inherited Create;
end;
constructor Msg_FORID.Create;
begin
inherited Create;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
RegMsg : Msg_FORID;
mStream : TMemoryStream;
begin
RegMsg := Msg_FORID.Create;
RegMsg.Sender := SenderEdit.Text;//运行时为'C_'
RegMsg.MsgType := TypeEdit.Text;//'FORID'
RegMsg.NickName := NKEdit.Text;//'aaaaaaaaaaaaaaaa'
RegMsg.Password := PWEdit.Text;//'1111111111111111'
IdUDPClient1.Host := SIPEdit.Text;//127.0.0.1
IdUDPClient1.Port := StrToInt(SPortEdit.Text);//5000
mStream := TMemoryStream.Create;
try
mStream.Write(RegMsg,RegMsg.InstanceSize);
IdUDPClient1.SendBuffer(mStream,mStream.Size);
memo1.Lines.Add('Send to server : '+RegMsg.Sender+RegMsg.MsgType+RegMsg.Ni
ckName+RegMsg.Password);
finally
mStream.Free;
end;
end;
end.
接收端
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdUDPBase, IdUDPServer,IdSo
cketHandle;
type
TForm1 = class(TForm)
IdUDPServer1: TIdUDPServer;
Memo1: TMemo;
procedure IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
type
Msg = class(TObject)
private
Sender : String[2];
MsgType : String[5];
protected
public
constructor Create ;
published
end;
type
Msg_FORID = class(Msg)
private
NickName : String[16];
Password : String[16];
protected
public
constructor Create ;
published
end;
constructor Msg.Create;
begin
inherited Create;
end;
constructor Msg_FORID.Create;
begin
inherited Create;
end;
procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
// RecieveMsg : Msg;
RegMsg : Msg_FORID;
begin
RegMsg := Msg_FORID.Create;
try
AData.Read(RegMsg,AData.Size);
Memo1.Lines.Add('Reseive Msg From '+ABinding.PeerIP+':4500 : '+RegMsg.Sen
der+RegMsg.MsgType+RegMsg.NickName);
finally
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
IdUDPServer1.DefaultPort := 5000;
IdUDPServer1.Active := true;
end;
end.
发送端可以成功发送出去,但是接收端读出数据时出错。
求教之,谢谢
unit Unit13;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdUDPServer, IdBaseComponent, IdComponent, IdUDPBase,
IdUDPClient,IdSocketHandle;
type
TForm1 = class(TForm)
IdUDPClient1: TIdUDPClient;
SIPEdit: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
SenderEdit: TEdit;
SPortEdit: TEdit;
Button2: TButton;
NKEdit: TEdit;
Memo1: TMemo;
Label5: TLabel;
TypeEdit: TEdit;
Label6: TLabel;
Label4: TLabel;
PWEdit: TEdit;
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
type
Msg = class(TObject)
private
Sender : String[2];
MsgType : String[5];
protected
public
constructor Create ;
published
end;
type
Msg_FORID = class(Msg)
private
NickName : String[16];
Password : String[16];
protected
public
constructor Create ;
published
end;
constructor Msg.Create;
begin
inherited Create;
end;
constructor Msg_FORID.Create;
begin
inherited Create;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
RegMsg : Msg_FORID;
mStream : TMemoryStream;
begin
RegMsg := Msg_FORID.Create;
RegMsg.Sender := SenderEdit.Text;//运行时为'C_'
RegMsg.MsgType := TypeEdit.Text;//'FORID'
RegMsg.NickName := NKEdit.Text;//'aaaaaaaaaaaaaaaa'
RegMsg.Password := PWEdit.Text;//'1111111111111111'
IdUDPClient1.Host := SIPEdit.Text;//127.0.0.1
IdUDPClient1.Port := StrToInt(SPortEdit.Text);//5000
mStream := TMemoryStream.Create;
try
mStream.Write(RegMsg,RegMsg.InstanceSize);
IdUDPClient1.SendBuffer(mStream,mStream.Size);
memo1.Lines.Add('Send to server : '+RegMsg.Sender+RegMsg.MsgType+RegMsg.Ni
ckName+RegMsg.Password);
finally
mStream.Free;
end;
end;
end.
接收端
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdUDPBase, IdUDPServer,IdSo
cketHandle;
type
TForm1 = class(TForm)
IdUDPServer1: TIdUDPServer;
Memo1: TMemo;
procedure IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
type
Msg = class(TObject)
private
Sender : String[2];
MsgType : String[5];
protected
public
constructor Create ;
published
end;
type
Msg_FORID = class(Msg)
private
NickName : String[16];
Password : String[16];
protected
public
constructor Create ;
published
end;
constructor Msg.Create;
begin
inherited Create;
end;
constructor Msg_FORID.Create;
begin
inherited Create;
end;
procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
// RecieveMsg : Msg;
RegMsg : Msg_FORID;
begin
RegMsg := Msg_FORID.Create;
try
AData.Read(RegMsg,AData.Size);
Memo1.Lines.Add('Reseive Msg From '+ABinding.PeerIP+':4500 : '+RegMsg.Sen
der+RegMsg.MsgType+RegMsg.NickName);
finally
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
IdUDPServer1.DefaultPort := 5000;
IdUDPServer1.Active := true;
end;
end.
发送端可以成功发送出去,但是接收端读出数据时出错。
求教之,谢谢