主题:请大家帮忙看下,关于idtcpclent和idtcpserver的简单问题
看过一个例子,关于客户端和服务器收发信息的。
照着例子小弟写了个简单的程序。
由于小弟是菜鸟,写的不好,实现不了功能。
现把代码贴上来,请大家帮忙看看
clent端
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
StdCtrls, Buttons;
type
Tuser = record
id:string;
end;
TForm1 = class(TForm)
BitBtn1: TBitBtn;
IdTCPClient1: TIdTCPClient;
Memo1: TMemo;
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.BitBtn1Click(Sender: TObject);
var
user:Tuser;
begin
user.id:='222';
IdTCPClient1.Host:='127.0.0.1';
IdTCPClient1.Port:=9999;
try
IdTCPClient1.Connect(3000);
IdTCPClient1.WriteBuffer(user,sizeof(Tuser));
IdTCPClient1.Disconnect;
except
on e: exception do
memo1.Lines.Add(e.Message);
end;
end;
end.
server端
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPServer;
type
Tuser= record
id:string;
end;
TForm1 = class(TForm)
IdTCPServer1: TIdTCPServer;
Memo1: TMemo;
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
user:TUser;
begin
with AThread.Connection do
begin
readbuffer(user,sizeof(Tuser));
memo1.Lines.Add(user.id ) ;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
self.IdTCPServer1.DefaultPort:=9999;
self.IdTCPServer1.Active:=true;
end;
end.
小弟是初学者 菜鸟,如果犯了低级错误,请大家包涵。
帮个忙吧。
谢谢大家了。
照着例子小弟写了个简单的程序。
由于小弟是菜鸟,写的不好,实现不了功能。
现把代码贴上来,请大家帮忙看看
clent端
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
StdCtrls, Buttons;
type
Tuser = record
id:string;
end;
TForm1 = class(TForm)
BitBtn1: TBitBtn;
IdTCPClient1: TIdTCPClient;
Memo1: TMemo;
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.BitBtn1Click(Sender: TObject);
var
user:Tuser;
begin
user.id:='222';
IdTCPClient1.Host:='127.0.0.1';
IdTCPClient1.Port:=9999;
try
IdTCPClient1.Connect(3000);
IdTCPClient1.WriteBuffer(user,sizeof(Tuser));
IdTCPClient1.Disconnect;
except
on e: exception do
memo1.Lines.Add(e.Message);
end;
end;
end.
server端
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPServer;
type
Tuser= record
id:string;
end;
TForm1 = class(TForm)
IdTCPServer1: TIdTCPServer;
Memo1: TMemo;
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
user:TUser;
begin
with AThread.Connection do
begin
readbuffer(user,sizeof(Tuser));
memo1.Lines.Add(user.id ) ;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
self.IdTCPServer1.DefaultPort:=9999;
self.IdTCPServer1.Active:=true;
end;
end.
小弟是初学者 菜鸟,如果犯了低级错误,请大家包涵。
帮个忙吧。
谢谢大家了。