我用服务段idtcpserver,客户端用idtcpclient
服务段采集数据然后往客户端分发,经常会造成内存不能为读,然后就退出了,我是这样写的
用clients(tthreadlist)记录客户端连接信息
with Clients.LockList do
    try
      for i := 0 to Count-1 do  // iterate through client-list
      begin
        RecClient := Items[i];
        RecThread := RecClient.Thread;
        try
       
      if not recthread.Terminated and recthread.Connection.Connected then
      begin
            newcommblock.MyUserName :=recthread.Connection.Socket.Binding.PeerIP ;
            NewCommBlock.Command:='RECIVE';
            NewCommBlock.Msg:=GISValue;//GSMNO;J;W;State;Date
            if newcommblock.MyUserName <> '' then
                 RecThread.Connection.WriteBuffer(NewCommBlock, SizeOf(NewCommBlock), True);  // send the stuff      end;
        except
        end;
      end;
    finally
      Clients.UnlockList;
    end;
请高手看看,什么地方出毛病了,请教别人,有的人说不能用循环调用socket,必须开辟进程,用线程很可能造成内存占用太多,满的情况,造成内存不能为读。请教高手delphi应该怎么解决,请教代码

解决方案 »

  1.   

    有的人说不能用循环调用socket,必须开辟进程  --不对
    这是我写的用户登录后把给每个在线用户发个信息,用以改变那些用户好友的状态图标
            if qryDetail.RecordCount > 0 then
            begin
              qryDetail.First;
              while not qryDetail.Eof do
              begin
                iindex := lbxUser.Items.IndexOf(qryDetail.FieldValues['fUserName']);
                TIdPeerThread(MyList.LockList.Items[iindex]).Connection.WriteBuffer(sMyMsg,sizeof(sMyMsg),True);
                MyList.UnlockList;            qryDetail.Next;
              end;
    这是可以的,没有问题的。你的问题没见过,烦用with, 程序可读性太烂了,不要滥用
    问题可能在
    RecClient := Items[i];
    RecThread := RecClient.Thread;
      

  2.   

    最近我也遇到indy多线程的问题,和你的情况差不多。用的idtcpserver,要实现客户端的长连接不断开,用TCP/IP模拟工具模拟每100ms发侦,同时好几台机子连接,发送。过个30分钟左右,模拟端就自动断开了,有时直接客户段全部断开,关闭服务器时,提示读内存错误,关闭不掉。只能CTRL+ALT+DEL。有时还会出现关闭是提示“terminted thread error”.我QQ:37503769。希望能和你一块交流一下,高手们看到帮帮忙啊。