通过
    DataThread.ListBuffer.Add(s);
    s := ClientSocket.Receiveln;
取不到数据,请问如何取得数据并转化显示啊!注接收是别人发送过来的XMl文件,如何诸个显示啊?

解决方案 »

  1.   

    要看对方是以什么方式发送过来的,还有发送的内容格式是什么样的
    比如是SendTo,SendBuf,Sendln还是SendStream,对应的接收方法也不同
      

  2.   

    不管用什么发送方式 你用 ReadBuffer都可以
      

  3.   

    是TIDTCPserver控件吧。   
     AThread.Connection.ReadBuffer (CommBlock, SizeOf (CommBlock));
      

  4.   

    procedure TServerFrmMain.ServerExecute(AThread: TIdPeerThread);
    var
      ActClient, RecClient: PClient;
      CommBlock, NewCommBlock: TCommBlock;
      RecThread: TIdPeerThread;
      i: Integer;  cmd: string; //接收到客户端的字符串信息
      ASize: 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': 消息
               // 'DIALOG':  对话框
               // 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  // 未知的命令类型
          if (CommBlock.Command = 'FILEDATETIME') then
          begin
            Protocol.Lines.Add (TimeToStr(Time)+' 得到文件时间:"'+CommBlock.MyUserName+'": '+CommBlock.Command);
            NewCommBlock.Command := 'FILEDATETIME';       // the message should popup on the client's screen
            NewCommBlock.MyUserName := '[Server]';  // the server's username
            NewCommBlock.Msg := ServerFrmMain.GetFileTime(ExtractFilePath(Application.ExeName)+'BMP\xxkbinfo.dat');  // 文件时间
            NewCommBlock.ReceiverName := '[return-to-sender]'; // unnecessary
            AThread.Connection.WriteBuffer (NewCommBlock, SizeOf (NewCommBlock), true);  // and there it goes...
          end
          else
          begin
             //----文件传输 start--
            {
            cmd := UpperCase(AThread.Connection.ReadLn); //客户端发送的命令字符串
                     
            if cmd = 'BEGIN' then //开始传输
            begin
              //告诉远程传输文件的大小和文件名
              AThread.Connection.WriteLn(Format('%d|%s', [AFileStream.Size, ExtractFileName(ExtractFilePath(paramstr(0))+'bmp\xxkbinfo.dat')]));          StatusBar1.SimpleText := '准备传输...';
              Exit;
            end;
            if cmd = 'END' then
            begin //传输完成
              AFileStream.Free; //释放文件流
              StatusBar1.SimpleText := '传输完成...';
              Exit;
            end;
            if cmd = 'CANCEL' then
            begin //传输取消
              StatusBar1.SimpleText := '传输取消...';
              //保持传输状态
              Exit;
            end;
            //按照指定位置传输文件
            AFileStream.Seek(StrToInT(cmd), soFromBeginning); //转到文件流传输的位置
            ASize := Min(AFileStream.Size - AFileStream.Position, AThread.Connection.RecvBufferSize);
            //计算需要发送的大小,Min()函数在Math单元
            AThread.Connection.OpenWriteBuffer; //准备发送缓冲
            AThread.Connection.WriteStream(AFileStream, false, false, ASize);
            //注意这个函数的参数。
            AThread.Connection.CloseWriteBuffer; //结束发送缓冲
            StatusBar1.SimpleText := Format('当前传输位置%s/大小%d', [cmd, AFileStream.Size]);
            //----文件传输 end---- }
              {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}
          end;
        end;
      end;
    end;
      

  5.   

    procedure TServerFrmMain.ServerExecute(AThread: TIdPeerThread);
    var
      ActClient, RecClient: PClient;
      CommBlock, NewCommBlock: TCommBlock;
      RecThread: TIdPeerThread;
      i: Integer;  cmd: string; //接收到客户端的字符串信息
      ASize: 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': 消息
               // 'DIALOG':  对话框
               // 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  // 未知的命令类型
          if (CommBlock.Command = 'FILEDATETIME') then
          begin
            Protocol.Lines.Add (TimeToStr(Time)+' 得到文件时间:"'+CommBlock.MyUserName+'": '+CommBlock.Command);
            NewCommBlock.Command := 'FILEDATETIME';       // the message should popup on the client's screen
            NewCommBlock.MyUserName := '[Server]';  // the server's username
            NewCommBlock.Msg := ServerFrmMain.GetFileTime(ExtractFilePath(Application.ExeName)+'BMP\xxkbinfo.dat');  // 文件时间
            NewCommBlock.ReceiverName := '[return-to-sender]'; // unnecessary
            AThread.Connection.WriteBuffer (NewCommBlock, SizeOf (NewCommBlock), true);  // and there it goes...
          end
          else
          begin
             //----文件传输 start--
            {
            cmd := UpperCase(AThread.Connection.ReadLn); //客户端发送的命令字符串
                     
            if cmd = 'BEGIN' then //开始传输
            begin
              //告诉远程传输文件的大小和文件名
              AThread.Connection.WriteLn(Format('%d|%s', [AFileStream.Size, ExtractFileName(ExtractFilePath(paramstr(0))+'bmp\xxkbinfo.dat')]));          StatusBar1.SimpleText := '准备传输...';
              Exit;
            end;
            if cmd = 'END' then
            begin //传输完成
              AFileStream.Free; //释放文件流
              StatusBar1.SimpleText := '传输完成...';
              Exit;
            end;
            if cmd = 'CANCEL' then
            begin //传输取消
              StatusBar1.SimpleText := '传输取消...';
              //保持传输状态
              Exit;
            end;
            //按照指定位置传输文件
            AFileStream.Seek(StrToInT(cmd), soFromBeginning); //转到文件流传输的位置
            ASize := Min(AFileStream.Size - AFileStream.Position, AThread.Connection.RecvBufferSize);
            //计算需要发送的大小,Min()函数在Math单元
            AThread.Connection.OpenWriteBuffer; //准备发送缓冲
            AThread.Connection.WriteStream(AFileStream, false, false, ASize);
            //注意这个函数的参数。
            AThread.Connection.CloseWriteBuffer; //结束发送缓冲
            StatusBar1.SimpleText := Format('当前传输位置%s/大小%d', [cmd, AFileStream.Size]);
            //----文件传输 end---- }
              {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}
          end;
        end;
      end;
    end;