首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

散分delphi7中TcpServer1Accept怎么接收缓冲区数据,一天26小时

2013-04-07 
散分delphi7中TcpServer1Accept如何接收缓冲区数据,一天26小时在线等通过DataThread.ListBuffer.Add(s)s

散分delphi7中TcpServer1Accept如何接收缓冲区数据,一天26小时在线等
通过
    DataThread.ListBuffer.Add(s);
    s := ClientSocket.Receiveln;
取不到数据,请问如何取得数据并转化显示啊!注接收是别人发送过来的XMl文件,如何诸个显示啊?
[解决办法]
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;
[解决办法]
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;

热点排行