请问 INDY 是如何实现服务器端发送数据,客户端接收数据的~!?谢谢~!
解决方案 »
- 在使用包的时候的一些问题
- MDI窗体,推出去的时候,怎么释放子窗体呀。
- 在Form上放一个Image,图片的形状不是矩形的,我想设置Form为透明,可以看到图片,请指教,谢谢!
- 学写学习再学习
- 如何作出象QQ一样可以自动隐藏鼠标滑入立即弹出显示
- paradox数据库的问题
- ?请问怎么出现设置目录的对话框,并且把路径保存edit中!!急急急急急急急(lxjgyl82)
- 有没有哪位大侠会delphi6访问java开发的CORBA对象,能不能举一个例子? (没有内容-_-)
- FieldByName("price"+IntToStr(pricetype)).
- jpg缩小保存的问题
- delphi
- 动态创建控件的标签问题,在线等
回复:月亮上的猪[ydlchina]
Indy10 demo中就只有从IdtcpClient发送数据,idTCPServer接收的例子啊~
还是不太明白,对等Socket是如何实现的谢谢~
begin
Write('');
end;
回复:失踪的月亮[hongqi162]
在Indy10中不是这样的~ 有好多都被修改了,比如说.AThread.Connection.WriteBuffer(...);就改掉了我到现在都还没有完全弄清楚呢~!
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, IdTCPServer, IdThreadMgr, IdThreadMgrDefault, IdBaseComponent,
IdComponent;type
PClient = ^TClient;
TClient = record // Object holding data of client (see events)
DNS : String[20]; { Hostname }
Connected, { Time of connect }
LastAction : TDateTime; { Time of last transaction }
Thread : Pointer; { Pointer to thread }
end; TServerFrmMain = class(TForm)
Server: TIdTCPServer;
CBServerActive: TCheckBox;
Protocol: TMemo;
IdThreadMgrDefault1: TIdThreadMgrDefault; procedure CBServerActiveClick(Sender: TObject);
procedure ServerConnect(AThread: TIdPeerThread);
procedure ServerExecute(AThread: TIdPeerThread);
procedure ServerDisconnect(AThread: TIdPeerThread);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction); private public
end;var
ServerFrmMain : TServerFrmMain;
Clients : TThreadList; // Holds the data of all clientsimplementationuses GlobalUnit;{$R *.DFM}procedure TServerFrmMain.CBServerActiveClick(Sender: TObject);
begin
Server.Active := CBServerActive.Checked;
end;procedure TServerFrmMain.ServerConnect(AThread: TIdPeerThread);
var
NewClient: PClient;begin
GetMem(NewClient, SizeOf(TClient)); NewClient.DNS := AThread.Connection.LocalName;
NewClient.Connected := Now;
NewClient.LastAction := NewClient.Connected;
NewClient.Thread :=AThread; AThread.Data:=TObject(NewClient); try
Clients.LockList.Add(NewClient);
finally
Clients.UnlockList;
end; Protocol.Lines.Add(TimeToStr(Time)+' Connection from "'+NewClient.DNS+'"');
end;procedure TServerFrmMain.ServerExecute(AThread: TIdPeerThread);
var
ActClient, RecClient: PClient;
CommBlock, NewCommBlock: TCommBlock;
RecThread: TIdPeerThread;
i: Integer;begin
if not AThread.Terminated and AThread.Connection.Connected then
begin
AThread.Connection.ReadBuffer (CommBlock, SizeOf (CommBlock));
ActClient := PClient(AThread.Data);
ActClient.LastAction := Now; // update the time of last action if (CommBlock.Command = 'MESSAGE') or (CommBlock.Command = 'DIALOG') then
begin // 'MESSAGE': A message was send - forward or broadcast it
// 'DIALOG': A dialog-window shall popup on the recipient's screen
// it's the same code for both commands... if CommBlock.ReceiverName = '' then
begin // no recipient given - broadcast
Protocol.Lines.Add (TimeToStr(Time)+' Broadcasting '+CommBlock.Command+': "'+CommBlock.Msg+'"');
NewCommBlock := CommBlock; // nothing to change ;-)) with Clients.LockList do
try
for i := 0 to Count-1 do // iterate through client-list
begin
RecClient := Items[i]; // get client-object
RecThread := RecClient.Thread; // get client-thread out of it
RecThread.Connection.WriteBuffer(NewCommBlock, SizeOf(NewCommBlock), True); // send the stuff
end;
finally
Clients.UnlockList;
end;
end
else
begin // receiver given - search him and send it to him
NewCommBlock := CommBlock; // again: nothing to change ;-))
Protocol.Lines.Add(TimeToStr(Time)+' Sending '+CommBlock.Command+' to "'+CommBlock.ReceiverName+'": "'+CommBlock.Msg+'"');
with Clients.LockList do
try
for i := 0 to Count-1 do
begin
RecClient:=Items[i];
if RecClient.DNS=CommBlock.ReceiverName then // we don't have a login function so we have to use the DNS (Hostname)
begin
RecThread:=RecClient.Thread;
RecThread.Connection.WriteBuffer(NewCommBlock, SizeOf(NewCommBlock), True);
end;
end;
finally
Clients.UnlockList;
end;
end;
end
else
begin // unknown command given
Protocol.Lines.Add (TimeToStr(Time)+' Unknown command from "'+CommBlock.MyUserName+'": '+CommBlock.Command);
NewCommBlock.Command := 'DIALOG'; // the message should popup on the client's screen
NewCommBlock.MyUserName := '[Server]'; // the server's username
NewCommBlock.Msg := 'I don''t understand your command: "'+CommBlock.Command+'"'; // the message to show
NewCommBlock.ReceiverName := '[return-to-sender]'; // unnecessary AThread.Connection.WriteBuffer (NewCommBlock, SizeOf (NewCommBlock), true); // and there it goes...
end;
end;
end;procedure TServerFrmMain.ServerDisconnect(AThread: TIdPeerThread);
var
ActClient: PClient;begin
ActClient := PClient(AThread.Data);
Protocol.Lines.Add (TimeToStr(Time)+' Disconnect from "'+ActClient^.DNS+'"');
try
Clients.LockList.Remove(ActClient);
finally
Clients.UnlockList;
end;
FreeMem(ActClient);
AThread.Data := nil;
end;procedure TServerFrmMain.FormCreate(Sender: TObject);
begin
Clients := TThreadList.Create;
end;procedure TServerFrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Server.Active := False;
Clients.Free;
end;end.