indy10 服务端如何向客户端发送数据

无栈程序猿 2008-03-04 01:51:16
indy10 服务端如何向客户端发送数据
unit Unit1;

interface

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

type
TForm1 = class(TForm)
IdTCPServer1: TIdTCPServer;
IdTCPClient1: TIdTCPClient;
ServerLog: TMemo;
ClientLog: TMemo;
Button2: TButton;
Button3: TButton;
Button1: TButton;
Button4: TButton;
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure IdTCPServer1Connect(AContext: TIdContext);
procedure IdTCPServer1Disconnect(AContext: TIdContext);
procedure IdTCPServer1Execute(AContext: TIdContext);
procedure Button1Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button2Click(Sender: TObject);
begin
IdTCPServer1.Active :=True;
Button2.Enabled :=false;
ServerLog.Lines.Add('服务器开启成功');
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
try
IdTCPClient1.Connect;
if IdTCPClient1.Connected then
ClientLog.Lines.Add('连接成功');
Button3.Enabled :=false;
except
ClientLog.Lines.Add('连接失败')
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
//初始化服务端 添加bindings
IdTCPServer1.Bindings.Add.IP := '127.0.0.1';
IdTCPServer1.Bindings.Add.Port := 7956;

//初始化客户端
IdTCPClient1.Host :='127.0.0.1';
IdTCPClient1.Port :=7956;
end;

procedure TForm1.IdTCPServer1Connect(AContext: TIdContext);
begin
ServerLog.Lines.Add('有客户连接!ip:'+AContext.Binding.PeerIP+'port:'+inttostr(AContext.Binding.PeerPort));
end;

procedure TForm1.IdTCPServer1Disconnect(AContext: TIdContext);
begin
//ServerLog.Lines.Add('有客户断开连接!ip:'+AContext.Binding.PeerIP+'port:'+inttostr(AContext.Binding.PeerPort));
end;

procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
var
a:string;
begin
a:=AContext.Connection.IOHandler.ReadLn;
ServerLog.Lines.Add(a);
AContext.Connection.Socket.WriteLn('123');
AContext.Connection.IOHandler.WriteLn('abc');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
IdTCPClient1.Disconnect;
Button1.Enabled :=False;

end;

procedure TForm1.Button4Click(Sender: TObject);
begin
IdTCPClient1.IOHandler.WriteLn('aa');
end;

end.
...全文
764 点赞 收藏 8
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
无栈程序猿 2008-03-08
AContext: TIdContext 是 uses IdContext
TWorkMode 需要申明哪个库 uses ?
回复
僵哥 2008-03-07
OnWork
AWorkMode=wmRead
回复
无栈程序猿 2008-03-07
以前好象有OnRead事件吧 现在怎么不见了?
回复
无栈程序猿 2008-03-07
放到OnSata 里程序直接死掉
回复
无栈程序猿 2008-03-07
客户端怎么接受消息呢?
我在OnWork 事件里 idtcpclient1.iohandle.readln;读取不出来
回复
僵哥 2008-03-05
向所有在线的用户发送(广播)数据。
var
I: Integer;
Context: TIdContext;
begin
with IdTCPServer1.Contexts.LockList do
try
for I := 0 to Count - 1 do
begin
Context := TIdContext(Items[I]);
Context.Connection.IOHandler.Write('Hello,everybody!');
end;
finally
IdTCPServer1.Contexts.UnlockList;
end;
end;


发送给特定的客户端(IP=192.168.10.90)
var
I: Integer;
Context: TIdContext;
begin
with IdTCPServer1.Contexts.LockList do
try
for I := 0 to Count - 1 do
begin
Context := TIdContext(Items[I]);
if Context.Binding.PeerIP <> '192.168.10.90' then
continue;
Context.Connection.IOHandler.Write('Hello!');
end;
finally
IdTCPServer1.Contexts.UnlockList;
end;
end;
回复
无栈程序猿 2008-03-04
- - 找到了 但是我的是 indy10.0.52 那些demo 都编译不了
回复
ydlchina 2008-03-04
学习 网上的INdy10的demo也很详细
回复
相关推荐
发帖
网络通信/分布式开发
创建于2007-08-02

1573

社区成员

Delphi 网络通信/分布式开发
申请成为版主
帖子事件
创建了帖子
2008-03-04 01:51
社区公告
暂无公告