Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Duda con TStream.Readbuffer (https://www.clubdelphi.com/foros/showthread.php?t=28819)

miguel_e 03-01-2006 19:14:59

Duda con TStream.Readbuffer
 
Hola a todos si alguien pudiera ayudarme con una duda que tengo me resolveria un gran problema, el asunto es que estoy haciendo un broadcast con los componentes UDP de los Indy, hasta ahi todo bien, el problema es cuando intento leer los datos, es un hecho que llega el mensaje a todas las maquinas de la red pues se dispara el evento de que llego un mensaje, bien pero el problema es que el parametro del evento que contiene los datos es un TStream, este tiene un metodo que se llama readbuffer el problema es que no se como utilizar este metodo y no puedo obtener la informacion???':confused:

espero haya podido quedar un poco claro si no entienen por favor diganmelo a ver si puedo intentar aclarar un poco esto disculpen la molestia

salu2
miguel_e

Northern 03-01-2006 19:37:43

TStream.WriteBuffer/ReadBuffer
 
Esta respuesta la da Peter Below de los TeamB a un usuario....no se si está permitido poner este tipo de mensajes
pero como soy nuevo lo pongo y si alguien tiene algo que decir que lo diga, que lo borre y todos contentos y felices :)


In article <3f7fbccf@newsgroups.borland.com>, Bob McKinnon wrote:
> I would like to write out an array of long strings along with other data to
> a file. I tried to create a file records like I normally do but that did
> not work since I was using a string. I wonder if streams is the way to go?
> To make it simple, if I have a data object that contains two integers and
> an array of strings it easy to stream that out to file? Sample code would
> be great.
>
> Test = class (Tobject)
> x, x1 : Integer;
> StList : array [1..100] of string;
> End;

Since strings are variable-length data you need to write the length of a
string in addition to the characters to the stream, otherwise you cannot read
them back conveniently.

Lets add two methods to your Test class:
Código Delphi [-]
procedure Test.SaveToStream( aStream: TStream );
var
  len, i: Integer;
begin
  Assert( Assigned( aStream ));
  // Write the two integer fields first
  aStream.WriteBuffer( x, sizeof(x));
  aStream.WriteBuffer( x1, sizeof(x1));
  // Now write the string array. Since its size is fixed we
  // do not need to write the number of elements first.
  for i:= Low( StList ) to High( StList ) do begin
    len := Length( StList[i] );
    aStream.WriteBuffer( len, sizeof(len));
    if len > 0 then
      aStream.WriteBuffer( StList[i][1], len );
  end;
end;

procedure Test.LoadFromStream( aStream: TStream );
var
  len, i: Integer;
begin
  Assert( Assigned( aStream ));
  // Read the two integer fields first
  aStream.ReadBuffer( x, sizeof(x));
  aStream.ReadBuffer( x1, sizeof(x1));
  // Now read the string array. Since its size is fixed we
  // do not need to read the number of elements first.
  for i:= Low( StList ) to High( StList ) do begin
    aStream.ReadBuffer( len, sizeof(len));
    SetLength( StList[i], len );
    if len > 0 then
      aStream.ReadBuffer( StList[i][1], len );
  end;
end;
Since ReadBuffer and WriteBuffer use untyped parameters handing them an
Ansistring (which is a pointer type) is a bit unintuitive: you have to pass
the first character of the string to get the correct address across.

--
Peter Below (TeamB)

miguel_e 04-01-2006 19:31:10

Hola, muchas gracias por tu repuesta, ya le di solucion al problema así

salu2
miguel_e

Código:

procedure TForm1.IdUDPServer1UDPRead(Sender: TObject; AData: TStream;
  ABinding: TIdSocketHandle);
var
  StrLen: Integer;
  PBuf: PChar;
  cadena : string;
begin
  StrLen := AData.Size;
  GetMem(PBuf, StrLen);
  try
    AData.ReadBuffer(PBuf^, StrLen);
    SetString(cadena, PBuf, StrLen);
  finally
    FreeMem(PBuf, StrLen);
  end;
  ShowMessage(cadena);
end;



La franja horaria es GMT +2. Ahora son las 11:20:58.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi