indy

indy9:

//一个简单的客户端连接信息数据结构

type    PClient   = ^TClient;
  TClient   = record  // 客户端连接信息的类 
    clientip      :String[50];//ip
    Thread                : Pointer;//指向一个客户端连接的指针
end;

 

Clients : TThreadList; //客户端连接列表

 

//连接事件

procedure TFrmDataCom.IdTCPServerConnect(AThread: TIdPeerThread);
var
   NewClient: PClient;//新的连接
   buf :array[0..1] of byte;
begin
  new(NewClient);//分配内存
  NewClient.Thread      := AThread;
  AThread.Data :=TObject(NewClient);
  try
    Clients.LockList.Add(NewClient); //在列表中添加新的对象
  finally
    Clients.UnlockList;
  end;
end;

 

//断开事件

 

procedure TFrmDataCom.IdTCPServerDisconnect(AThread: TIdPeerThread);
var
  ActClient: PClient;
  id,index :integer;
begin
  ActClient := Pointer(AThread.Data);
  try
    Clients.LockList.Remove(ActClient); //从list中移除客户端
  finally
    Clients.UnlockList;
  end;
  try
    Dispose(ActClient);
    AThread.Data := nil;
  except
  end;

end;

 

//接收数据事件

 

procedure TFrmDataCom.IdTCPServerExecute(AThread: TIdPeerThread);

var

  recvResult :integer;

begin

  if not AThread.Terminated and AThread.Connection.Connected then
  begin
    try
      recvResult := AThread.Connection.Socket.Recv(tmprbuf,sizeof(tmprbuf));//接收数组

      if(recvResult = -1) or (recvResult=0) then
      begin
          AThread.Connection.Disconnect;
      end

      else

      begin

            //处理数据

      end;

   except

       AThread.Connection.Disconnect;

   end;

end;

 

 

 

//关闭所有的连接

procedure TFrmDataCom.CloseAllClient;
var
  i:Integer;
  AThread: TIdPeerThread;
  Client :PClient;
begin
  try
    with Clients.LockList do
    begin
      for i:=0 to Count-1 do
      begin
        try
          AThread :=TIdPeerThread(PClient(Items[i]).Thread);
          AThread.Connection.InputBuffer.Clear;
          AThread.Connection.Disconnect;
        except
        end;
      end;
    end;
  finally
    Clients.UnlockList;
  end;
  IdTCPServer.Active :=false;
end;

 

indy当有客户端在线时候还要自己去disconn,真是无语。。。。。。。。。。不知道是不是这个样子???

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
indy10.2.3 full 完全安装版 D7~2010 版本:indy10.2.3 经delphi7下的安装成功,delphi11和delphi6的安装没有试用 【自动安装】 Lib\Fulld7.bat 双击运行即可。 自动会把bpl文件拷到C:\windows\system32 重新打开delphi7会自动加载 dclIndyCore70.bpl dclIndyProtocols70.bpl 【手工安装】 1.windows的path路径中增加D7的路径 在启用delphi7时,要能加载dclIndyCore70.bpl这个文件 如果是自动安装,这个文件会拷到C:\windows\system32\dclIndyCore70.bpl,所以不用改path 2.delphi7菜单中Library路径中 添加indy10\LIB路径下的System、Core、Protocols下的三个目录 添加indey10\D7 因为这个目录下有bpl和dcu文件 3.删除delphi7目录Bin下的indy*.BPL文件,这是官方旧版本的文件,放心删除。 4.删除delphi7目录下的 Id*.DCU文件,一般都是在lib目录里。 5.打开indy10\lib目录,安装DPK包顺序如下: (1)编译 System\IndySystem70.dpk (只需要compile) (这个不是设计包,不需要install) (2)编译 Core\IndyCore70.dpk (只需要compile) 编译安装Core\dclIndyCore70.dpk (设计包,先complie后install) (3)编译 Protocols\IndyProtocols70.dpk (只需要compile) 编译安装Protocols\dclIndyProtocols70.dpk (设计包,先complie后install) 6.完成 【第二次手工安装】 第一步: indy10\D7目录下的5个文件 复制到delphiD:\Delphi7_Ent\Projects\Bpl IndySystem70.bpl IndyCore70.bpl IndyProtocols70.bpl dclIndyCore70.bpl dclIndyProtocols70.bpl 第二步: 打开delphi7菜单 Component/install packpages/ add dclIndyCore70.bpl 再 add dclIndyProtocols70.bpl 第三步 打开delphi7菜单中Library路径中 添加indy10\LIB路径下的System、Core、Protocols下的三个目录 完成!更简单。前提是要有现成bpl的文件,如果没有bpl文件,还得需要dpk编译
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值