TIdTCPServer的OnExecute中的AThread.Connection.WriteBuffer();是如何用的?在IdTCPClient1端是如何

  • 主题发起人 主题发起人 socool_100
  • 开始时间 开始时间
S

socool_100

Unregistered / Unconfirmed
GUEST, unregistred user!
TIdTCPServer的OnExecute中的AThread.Connection.WriteBuffer();是如何用的?在IdTCPClient1端是如何接收到该数据的呢?(100分)<br />你好,我在用TIdTCPServer时,
用AThread.Connection.WriteInteger(1)或
AThread.Connection.WriteLn('1');
都能在IdTCPClient1端获得,
我现在是想在TIdTCPServer端发个记录类型给IdTCPClient端接收,不知道如何发,而我找到了AThread.connecton.writeBuffer,但发了,在IdTCPClient端没反应了,
哪位兄弟能给个例子或提示一下好吗?
 
Socket2 用TIdTcpServer和TIdTcpClient做的文件传输的例子
http://www.wisdomcube.com/stuff/Socket2.rar
用TIdTcpServer和TIdTcpClient做的聊天室例子
http://www.wisdomcube.com/stuff/chat.rar
 
服务端
unit fMain;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, IdBaseComponent, IdComponent, IdTCPServer;

type
Direction = (dirLeft,dirRight);

type
MyRecord = Packed Record
MyInteger : Integer;
MyString : String[250];
MyBool : Boolean;
MyDirection : Direction;
end;


type
TfrmMain = class(TForm)
IdTCPServer: TIdTCPServer;
btnStart: TButton;
btnExit: TButton;
procedure btnExitClick(Sender: TObject);
procedure btnStartClick(Sender: TObject);
procedure IdTCPServerExecute(AThread: TIdPeerThread);
private
{ Private declarations }
public
{ Public declarations }
end;

var
frmMain: TfrmMain;

implementation

{$R *.DFM}

procedure TfrmMain.btnExitClick(Sender: TObject);
begin
if IdTCPServer.active then IdTCPServer.active := false;
application.terminate;
end;

procedure TfrmMain.btnStartClick(Sender: TObject);
begin
IdTCPServer.active := not IdTCPServer.active;

if btnStart.caption = 'Start' then btnStart.caption := 'Stop'
else if btnStart.caption = 'Stop' then btnStart.caption := 'Start';

end;

procedure TfrmMain.IdTCPServerExecute(AThread: TIdPeerThread);
var
MyRec : MyRecord;
begin
AThread.connection.ReadBuffer(MyRec,SizeOf(MyRec));
AThread.connection.WriteBuffer(MyRec,SizeOf(MyRec),true);
end;

end.
客户端
unit fMain;
interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient;

type
Direction = (dirLeft,dirRight);

type
MyRecord = Packed Record
MyInteger : Integer;
MyString : String[250];
MyBool : Boolean;
MyDirection : Direction;
end;

type
TfrmMain = class(TForm)
Label1: TLabel;
edtServerIP: TEdit;
Label2: TLabel;
edtServerPort: TEdit;
GroupBox1: TGroupBox;
Label3: TLabel;
cboSendDirection: TComboBox;
Label4: TLabel;
edtSendMyInteger: TEdit;
edtSendMyString: TEdit;
Label5: TLabel;
Label6: TLabel;
cboSendMyBoolean: TComboBox;
GroupBox2: TGroupBox;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
Label10: TLabel;
cboReceiveDirection: TComboBox;
edtReceiveMyInteger: TEdit;
edtReceiveMyString: TEdit;
cboReceiveMyBoolean: TComboBox;
Button1: TButton;
Button2: TButton;
IdTCPClient: TIdTCPClient;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
MySendingRecord : MyRecord;
MyReceivingRecord : MyRecord;
end;

var
frmMain: TfrmMain;

implementation

{$R *.DFM}

procedure TfrmMain.Button1Click(Sender: TObject);
begin
// init
with MySendingRecord do
begin
MyInteger := StrToInt(edtSendMyInteger.text);
MyString := edtSendMyString.text;
case cboSendDirection.ItemIndex of
0 : MyDirection := dirLeft;
1 : MyDirection := dirRight;
end;
case cboSendMyBoolean.ItemIndex of
0 : MyBool := true;
1 : MyBool := false;
end;
end;

cboReceiveDirection.ItemIndex := -1;
edtReceiveMyInteger.text := '';
edtReceiveMyString.text := '';
cboReceiveMyBoolean.ItemIndex := -1;

// send and receive
with IdTCPClient do
begin
Host := edtServerIP.text;
Port := StrToInt(edtServerPort.text);
Connect;
WriteBuffer(MySendingRecord,SizeOf(MySendingRecord),true);
ReadBuffer(MyReceivingRecord,SizeOf(MyReceivingRecord));
Disconnect;
end;

// Show record buffer data received back

case MyReceivingRecord.MyDirection of
dirLeft : cboReceiveDirection.ItemIndex := 0;
dirRight : cboReceiveDirection.ItemIndex := 1;
end;

edtReceiveMyInteger.text := IntToStr(MyReceivingRecord.MyInteger);
edtReceiveMyString.text := MyReceivingRecord.MyString;

case MyReceivingRecord.MyBool of
true : cboReceiveMyBoolean.ItemIndex := 0;
false : cboReceiveMyBoolean.ItemIndex := 1;
end;


end;

procedure TfrmMain.FormCreate(Sender: TObject);
begin
cboSendDirection.ItemIndex := 0;
cboSendMyBoolean.ItemIndex := 0;
cboReceiveDirection.ItemIndex := -1;
cboReceiveMyBoolean.ItemIndex := -1;
end;

procedure TfrmMain.Button2Click(Sender: TObject);
begin
application.terminate;
end;

end.
 
谢谢东兰梦舞朋友,达到了我的要求,我就需要这个效果。谢谢,不过能传文件就好。
同时app2001提供的demo给我也是很不错的,谢谢您们!谢谢!
 
后退
顶部