TIdTcpServer 到底怎么获取待读缓冲去的大小,这个AThread.Connection.ReadFromStack(false,100,false);老是有这个两个异常(‘Not Connected’和‘Disconnected’)。怎么办啊?

解决方案 »

  1.   

    获取之前判断是否连接啊figozhu是接分专业户????
      

  2.   

    我的接收线程处理
    procedure TRecThread.Execute;
    var
      ires,i,j : integer;
      recMsg : TMyMsg;
      stmp : String;
      sNode,ss : TTreeNode;
    begin
      { Place thread code here }
      FreeOnTerminate := True;  while ((not Terminated) and DM.tcpLog.Connected) do
      begin
        ires := DM.tcpLog.ReadFromStack(...);
        if ires > 0 then
        begin
      

  3.   

    To : dabaicai(不再做菜鸟)我的接收线程和你差不多。但是我服务端可能会主动断开,就会造成在这‘ires := DM.tcpLog.ReadFromStack(...);’产生异常。
    用的是9.00.10版本
      

  4.   

    我用的也是9,服务器端也可以强制断开,没出现异常
    不过我ires := DM.tcpLog.ReadFromStack(...)里面没加参数
      

  5.   

    To : dabaicai(不再做菜鸟) 
       ReadFromStack不要参数没有数据读的时候有‘Read Timeout’异常。
       我是用的9.00.10(Delphi7自带的)。我就是要实现取得待缓冲区的大小,然后一次读出来。
       你能不能把这方面的代码贴出来我参考一下,谢谢。QQ:69786714
      

  6.   

    我的是一个局域网内的聊天工具开发,没有带参数,接收信息是通过一个TMyMsg(record类型)
    procedure TRecThread.Execute;
    var
      ires,i,j : integer;
      recMsg : TMyMsg;
      stmp,tmpName,tmpRealName,tmpDept,pointDept : String;
      sNode,ss : TTreeNode;
    begin
      { Place thread code here }
      FreeOnTerminate := True;  while ((not Terminated) and DM.tcpLog.Connected) do
      begin
        ires := DM.tcpLog.ReadFromStack;
        if ires > 0 then
        begin
          DM.tcpLog.ReadBuffer(recMsg,sizeof(recMsg));
          case recMsg.iType of
          3 :                // 接收信息(说话)
            begin
      

  7.   

    不好意思,看错了,呵呵
    procedure Tfrm_ServerMain.TCPServerExecute(AThread: TIdPeerThread);
    var
      sMyMsg,sSendMsg,sChangeStatMsg,sFindFriend,sSelfMsg : TMyMsg;
      stmp : String[50];
      sRealName : String;
      i,iindex : integer;
      mythread : TIdPeerThread;
      friendsList,OnlineFriendList : TStringList;  MyFriendMsg : TFriendMsg;
      fileStream : TMemoryStream;
    begin  if (not AThread.Terminated) and (AThread.Connection.Connected)  then
      begin
        AThread.Connection.ReadBuffer(sMyMsg,sizeof(sMyMsg));
        case sMyMsg.iType of
        1 :                //登录
          begin
      

  8.   

    我现在就是要得到SERVER待读的大小,然后一次性读出来。
    现在就在得到缓冲区大小的异常,用这个(AThread.Connection.ReadFromStack(false,100,false))取大小的。我估计这个异常是我服务端强制断开客户端后才产生的。怎么能让它不产生异常呢?
      

  9.   

    我用这个(AThread.Connection.ReadFromStack(false,100,false))取得待读缓冲的大小。
    强制断开后可能还在OnExecute读大小,我想是不是这个原因哦?断开以后还是可以接收其他客户端的数据。
      

  10.   

    你不可能通过athread来断开,每个连接都要保存为连接线程,放到threadlist里面,断开的话也只是在threadlist里面选一个断开,不可能只通过athread就能全部实现
    看看我的连接和断开代码
    procedure Tfrm_ServerMain.TCPServerConnect(AThread: TIdPeerThread);
    begin
      iUserCount := iUserCount + 1;
      stbMain.Panels[0].Text := '连接用户个数:' + IntToStr(iUserCount);  MyList.LockList.Add(Pointer(AThread));
      MyList.UnlockList;
      AThread.FreeOnTerminate := True;
    end;procedure Tfrm_ServerMain.TCPServerDisconnect(AThread: TIdPeerThread);
    var
      i : integer;
      SetStatMsg : TMyMsg;
    begin
      iUserCount := iUserCount - 1;
      stbMain.Panels[0].Text := '连接用户个数:' + IntToStr(iUserCount);  if AThread.Data <> nil then
      begin   { qryDisconn.Close;
        qryDisconn.SQL.Text := 'Select * from tb_User a, tb_Friends b  '
            + 'where a.fUserName = b.fFriendsName  and a.fstat = 0 and b.fUsername = ' + QuotedStr(TFriendMsg(AThread.Data).UserName);
        qryDisconn.Open; }
        qryDisconn.Close;
        qryDisconn.SQL.Text := 'Select * from tb_User  where '
            + ' fstat <> 9 and fUsername <> ' + QuotedStr(TFriendMsg(AThread.Data).UserName);
        qryDisconn.Open; 
        if qryDisconn.RecordCount > 0 then
        begin
          qryDisconn.First;
          while not qryDisconn.Eof do
          begin
            SetStatMsg.iType := 2;
            SetStatMsg.iFile := 0;
            SetStatMsg.iSize := 0;
            SetStatMsg.istat := 9;
            SetStatMsg.Pwd := '';
            SetStatMsg.RecName := qryDisconn.FieldValues['fUserName'];
            SetStatMsg.UserName := TFriendMsg(AThread.Data).UserName;
            SetStatMsg.sIP := '';
            SetStatMsg.sMsg := '';
            i := lbxUser.Items.IndexOf(qryDisconn.FieldValues['fUserName']);
            TIdPeerThread(MyList.LockList.Items[i]).Connection.WriteBuffer(SetStatMsg,sizeof(SetStatMsg),True);
            MyList.UnlockList;
            
            qryDisconn.Next;
          end;
        end;    if qryMain.Locate('fUserName',TFriendMsg(AThread.Data).UserName,[]) then    //用户下线
        begin
          qryMain.Edit;
          qryMain.FieldValues['fStat'] := 9;
          qryMain.FieldValues['fLogIP'] := '';
          qryMain.Post;
        end;    AThread.Data := nil;
      end;    i := MyList.LockList.IndexOf(Pointer(AThread));
      MyList.UnlockList;
      MyList.LockList.Remove(Pointer(AThread));
      MyList.UnlockList;  //AThread.Free;
      lbxUser.Items.Delete(i);
    end;